Laboratory No. 5
Laboratory No. 5
COLLEGE OF ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
City of Malolos Bulacan
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"color": "red",
"capacity": "5 seater"
}
print(thisdict)
OUTPUT:
You can access the items of a dictionary by referring to its key name:
Get the value of the "model" key:
x = thisdict["model"]
There is also a method called get() that will give you the same result:
Get the value of the "model" key:
x = thisdict.get("model")
From the above dictionary and given sample output, write your code using the method get().
YOUR CODE HERE
1
To change the "year" to 2018:
thisdict["year"] = 2018
You can also use the values() function to return values of a dictionary:
for x in thisdict.values():
print(x)
Loop through both keys and values, by using the items() function:
for x, y in thisdict.items():
print(x, y)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
Dictionary Length
To determine how many items (key-value pairs) a dictionary have, use the len() method.
Print the number of items in the dictionary:
print(len(thisdict))
Adding Items
Adding an item to the dictionary is done by using a new index key and assigning a value to it:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
2
thisdict["rim size"] = 14
print(thisdict)
Removing Items
The popitem() method removes the last inserted item (in versions before 3.7, a random item is
removed instead):
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
The del keyword removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
The dict() Constructor
It is also possible to use the dict() constructor to make a dictionary:
3
thisdict = dict(brand="Ford", model="Mustang", year=1964)
# note that keywords are not string literals
# note the use of equals rather than colon for the assignment
print(thisdict)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.
IV. EXERCISES: Complete the given code and run it through python 2.7.14
Use the get method to print the value of the "model" key of the car dictionary.
car = {
OUTPUT:
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print (________________)
car = { OUTPUT:
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
______________________
4
Use the pop method to remove "model" from the car dictionary.
car = {
"brand": "Ford", OUTPUT:
"model": "Mustang",
"year": 1964
}
________________
car = {
OUTPUT:
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
________________
V. PROGRAMMING CHALLENGE
Given the dictionary and sample output, develop a python program. Write your code at
the back of this page.