-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionaries.py
33 lines (24 loc) · 1.11 KB
/
dictionaries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Dictionaries store key-value pairs
student = {'name': 'John', 'age': 25, 'courses': ['Math', 'CompSci']}
print(student)
# To find the value of a key, access the key name
print(student['name'])
# If we try to access a key that doesn't exist, it will produce and error.
# To find if a key exists without creating an error, use the 'get' method
print(student.get('phone'))
print(student.get('phone', 'Not Found')) # Creates a default error message
# If we want to add a key that doesn't exist yet, we can easily
student['phone'] = '555-5555'
print(student.get('phone', 'Not Found'))
# If we want to update the dictionary, we can just assign a new value to a key
student['name'] = 'Jane'
print(student)
# But if we want to update multiple keys, better to use the 'update' method
# and another dictionary. We can even add keys with this method on the fly
student.update({'name': 'Jean', 'age': 26, 'email': '[email protected]'})
print(student)
# To get a list of dictionary items, use the 'items' method
print(student.items())
# To see key-item pairs, we can use a for loop
for key, value in student.items():
print(key, value)