-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass Notes_03_24_2021.py
More file actions
119 lines (91 loc) · 1.94 KB
/
Class Notes_03_24_2021.py
File metadata and controls
119 lines (91 loc) · 1.94 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#####################
## Dalton Murray #
## 03/24/20201 #
## Class notes #
#####################
student = {
"name": "John Smith",
"age": "25",
"grades": [87, 90]
}
# Dictionary is mutable
# Can't have duplicate keys
print(student["name"])
print(10*"*")
# One type of dictionary has index and one doesn't
# One also has order and one doesn't
print(type(student))
print(10*"*")
student["name"] = "Rose Davidson"
print(student)
student.update({"age":48})
print(student["age"])
student["id"] = 110
student.update({"email":"John@gmail.com"})
print(student)
# student.popitem()
# print(student)
# del student["age"]
# print(student)
# student.clear()
# print(student)
print(10*"*")
for i in student:
print(i, student[i])
print(10*"*")
print(student.keys())
print(list(student.values()))
print(10*"*")
for i in student.keys():
print(i)
print(10*"*")
print(list(student.items()))
for k, v in student.items():
print(k, v)
print(10*"*")
# Must use .copy in order to make a real copy
# and not just a reference
new_student = student.copy()
new_student = dict(student)
student = {
"name":"John Smith",
"age":"25",
"grades":{"math":87, "art":100}
}
print(student["grades"]["math"])
print(student.get("grades"))
student.pop("name")
print(student.pop("name", "Key does not exist"))
print(student)
# Updating a dictionary
numbers = [1, 2, 3, 4, 5]
squares = {
"numbers":numbers
}
print(squares)
numbers2 = []
for i in range(1, 10):
numbers2.append(i)
squares["numbers"] = numbers2
print(squares)
print(10*"*")
# Notes
numbers = [1, 2, 3, 4, 5]
# Gets square if the number is divided by 2
# and remainder is 0 then it is even
squares = {i: i**2 for i in numbers if i % 2 == 0}
print(squares)
objects = ["door", "pen", "board", "orange"]
str_length = {i: len(i) for i in objects}
print(str_length)
dict1 = {
"A": 10,
"B": 20
}
dict2 = {
"A": 50,
"C": 30,
"D": 40
}
dict1.update(dict2)
print(dict1)