Uploaded by Colorful sky多彩的天空

CSC 1402 Lab 02 Manual (Lab 11)

advertisement
School of Science and Engineering
Computer Science Department
Lab 11
Python Programming
Dictionaries
1
School of Science and Engineering
Computer Science Department
Dictionaries:
Dictionaries are data type used to store data in the form of key: value pairs, we separate
between elements(keys+value) of a dictionary by “,”. you can use an integer, float, string, or
Boolean as a dictionary key.
My_information = {
“FirstName” : Saad,
“SecondName” : Driouech,
“year” : 2000,
“City” : Casablanca
}
We can print all keys and values of a dictionary using the keys() and values(), or both using
item()
print(My_information.keys())
print(My_information.values())
-dict_keys(['FirstName', 'SecondName', 'year', 'City'])
dict_values(['Saad', 'Driouech', 2000, 'Casablanca'])
dict_items([('FirstName', 'Saad'), ('SecondName', 'Driouech'), ('year', 2000), ('City',
'Casablanca')])
As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries
are unordered.
In a python dictionary, we can access each element value using it key:
print(My_information[“City”])
-Casablanca
We can change the value of an element of a dict just by calling it using it key:
My_information[“Year”] = 2022
We add new element to a dictionary just by assigning a value to its key:
My_information[“age”] = 22
{'FirstName': 'Saad', 'SecondName': 'Driouech', 'year': 2000, 'City': 'Casablanca', ‘age’ :
22}
We can remove an item from a dict just by call it using it key and the function pop():
My_information.pop('SecondName')
{'FirstName': 'Saad', 'year': 2000, 'City': 'Casablanca', ‘age’ : 22}
2
School of Science and Engineering
Computer Science Department
Objectives:
•
•
•
To understand how to define dictionaries.
How to access any element from a dictionary.
How to add and remove elements from a dictioanry.
3
School of Science and Engineering
Computer Science Department
Problems for Practice:
1. Write a Python program that counts the frequency of each element in a list and returns
the result as dictionary where keys are the list elements, and the values are their
frequencies.
2. Write a Python program to check if a specific key and a value exist in a dictionary.
3. Write a Python script to concatenate the following dictionaries to create a new one.
4. Write a Python script to generate and print a dictionary that contains a number (between
1 and n) in the form (x, x*x).
Sample input: n = 5
Expected output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
5. Write a Python program to map two lists into a dictionary. The dictionary keys should be
in the following manner: [first element of list 1, second element of list 2, third element of
list 1, …] and the values should be in the following manner [first element of list 2,
second element of list 1, third element of list 2, …]
Sample input: L1=[‘Hi’, 1, ‘Hello’, ‘Bye’] and L2=[‘Dear’, 33, 88, 2.566]
Expected output: {‘Hi’: ‘Dear’, 33: 1, ‘Hello’: 88, 2.566: ‘Bye’]
6. Write a Python program to get the keys maximum and minimum values of a dictionary
7. Write a Python program to combine two dictionary by adding values for common keys.
Sample input: d1 = {'a': 100, 'b': 200, 'c':300} d2 = {'a': 300, 'b': 200, 'd':400}
Sample output: {'a': 400, 'b': 400, 'd': 400, 'c': 300}
8. Write a Python program to swap the keys and values of a dictionary.
9. Write a Python program to find all keys in a dictionary that have the given value.
10. Write a Python program to transform a dictionary into a tuple of lists.
4
Download