-
Notifications
You must be signed in to change notification settings - Fork 34
/
dictionaries.pyde
68 lines (47 loc) · 1.4 KB
/
dictionaries.pyde
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
student = ['Sam', 24]
student = {'name': 'Sam', 'age': 24}
# accessing dictionaries
print(student['age']) # displays: 24
print(student['name']) # displays: Sam
print(student) # {'name': 'Sam', 'age': 24}
if 'age' in student:
print(student['age'])
# modifying dictionaries
student['age'] = 25
print(student) # {'name': 'Sam', 'age': 25}
student['id'] = 19950501
print(student) # {'name': 'Sam', 'id': 19950501, 'age': 25}
del student['age']
print(student) # {'name': 'Sam', 'id': 19950501}
# nesting dictionaries and lists
students = {
'names': ['Sam', 'Lee'],
'ids': [19950501, 19991114]
}
print(students['names'][1]) # Lee
students = [
{'name': 'Sam', 'id': 19950501},
{'name': 'Lee', 'id': 19991114}
]
print(students[1]['name']) # Lee
# combining loops and dictionaries
courses = {
'game development': 'Prof. Smith',
'web design': 'Prof. Ncube',
'code art': 'Prof. Sato'
}
# iterating keys
#for course in courses:
for course in sorted(courses):
print(course)
print(sorted(courses.keys())) # ['code art', 'game development', 'web design']
# iterating values
for prof in courses.values():
print(prof)
# iterating items
print(courses.items())
for kv in courses.items():
print(kv)
#for course, prof in sorted(courses.items()):
for course, prof in reversed(sorted(courses.items())):
print('{} coordinates the {} course.'.format(prof, course))