0% found this document useful (0 votes)
13 views5 pages

Laboratory No. 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Laboratory No. 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BULACAN STATE UNIVERSITY

COLLEGE OF ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
City of Malolos Bulacan

COMPUTER FUNDAMENTALS AND PROGRAMMING (PHYTON)


LABORATORY NO. 5
DICTIONARY
NAME: __________________________________________________ SCORE: __________________
C/Y/S: ___________________________________________________ DATE: __________________

I. Objective: To develop simple programs using dictionary.


II. Software: Python 2.7.14
III. Discussion/Procedures
A dictionary is a collection which is unordered, changeable and indexed. In Python
dictionaries are written with curly brackets, and they have keys and values.

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

Loop Through a Dictionary


You can loop through a dictionary by using a for loop.
When looping through a dictionary, the return value are the keys of the dictionary, but there are
methods to return the values as well.

Print all key names in the dictionary, one by one:


for x in thisdict:
print(x)
Print all values in the dictionary, one by one:
for x in thisdict:
print(thisdict[x])

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)

Check if Key Exists


To determine if a specified key is present in a dictionary use the in keyword:
Check if "model" is present in the dictionary:

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

There are several methods to remove items from a dictionary:


The pop() method removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)

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)

The clear() keyword empties the dictionary:

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)

The del keyword can also delete the dictionary completely:

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 (________________)

Change the "year" value from 1964 to 2018.

car = { OUTPUT:
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
______________________

Add the key/value pair "color" : "red" to the car dictionary.


car = {
"brand": "Ford", OUTPUT:
"model": "Mustang",
"year": 1964
}
________________________

4
Use the pop method to remove "model" from the car dictionary.

car = {
"brand": "Ford", OUTPUT:
"model": "Mustang",
"year": 1964
}
________________

Use the clear method to empty the car dictionary.

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.

birthdays = { 'Albert Einstein': '03/14/1879', 'Benjamin Franklin': '01/17/1706', 'Ada Lovelace':


'12/10/1815', 'Donald Trump': '06/14/1946', 'Rowan Atkinson': '01/6/1955'}

You might also like