COMPUTER PYTHON REPORT
COMPUTER PYTHON REPORT
COMPUTER PYTHON REPORT
Create a List:
thislist = ["apple", "banana", "cherry"]
print (thislist)
The list is changeable, meaning that we can change, add, and
remove items in a list after it has been created.
Since lists are indexed, lists can have items with the same value
Range of Indexes
You can specify a range of
indexes by specifying
where to start and where
to end the range.
When specifying a range,
the return value will be a
new list with the specified
items.
Check if Item Exists
To determine if a specified item is present in a list use the in keyword
Change Item Value
To change the value of a specific item, refer to the index number:
Change a Range of Item Values
To change the value of items within a specific range, define a list with the new values, and refer to
the range of index numbers where you want to insert the new values:
Insert Items
To insert a new list item, without replacing any of the existing values, we can use the insert() method.
The insert() method inserts an item at the specified index:
Append Items
To add an item to the end of the list, use
the append() method:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
Extend List
To append elements from another list to the current list, use the extend() method.
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
print(x)
Unpacking a Tuple
When we create a tuple, we normally assign values to it. This is
called "packing" a tuple:
Packing a tuple:
fruits = ("apple", "banana", "cherry")
Unpacking a tuple
fruits = ("apple", "banana", "cherry")
print(green)
print(yellow)
fruits =
print(red)
("apple", "banana", "cherry", "strawberry", "raspber
Using Asterisk* ry")
If the number of variables is less than the
number of values, you can add an * to the (green, yellow, *red) = fruits
variable name and the values will be
assigned to the variable as a list: print(green)
print(yellow)
print(red)
Loop Through a Tuple
You can loop through the tuple items by using a for loop.
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
print(len(thisset))
thisset.add("orange")
print(thisset)
To add items from another set into the current set, use the update ( ) method
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
thisset.remove("banana")
print(thisset)
You can loop through the set items by using a for loop
for x in thisset:
print(x)
Dictionary
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered*, changeable and do not allow
duplicates.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Get Keys
The keys() method will return a list of all the keys in the dictionary.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
car["color"] = "white"
for x in thisdict:
print(x)
for x in thisdict:
print(thisdict[x])
myfamily = {
"child1" : {
Nested "name" : "Emil",
Dictionaries "year" : 2004
},
A dictionary can "child2" : {
contain dictionaries, "name" : "Tobias",
this is called nested "year" : 2007
},
dictionaries. "child3" : {
"name" : "Linus",
"year" : 2011
}
}
Arrays
Arrays are used to store multiple values in one single variable:
cars = ["Ford", "Volvo", "BMW"]
The Length of an Array
Use the len() method to return the length of an array (the number of elements in an array).
x = len(cars)
Looping Array Elements
You can use the for in loop to loop through all the elements of an array.
for x in cars:
print(x)