Skip to content

Commit c8346b6

Browse files
committed
Added lesson 7
1 parent 4c447fe commit c8346b6

1 file changed

Lines changed: 150 additions & 0 deletions

File tree

lesson07/dictionaries.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Dictionaries
2+
band = {
3+
"vocals": "Plant",
4+
"guitar": "Page"
5+
}
6+
7+
band2 = dict(vocals="Plant", guitar="Page")
8+
9+
print(band)
10+
print(band2)
11+
print(type(band))
12+
print(len(band))
13+
14+
# Access items
15+
print(band["vocals"])
16+
print(band.get("guitar"))
17+
18+
# list all keys
19+
print(band.keys())
20+
21+
# list all values
22+
print(band.values())
23+
24+
# list of key/value pairs as tuples
25+
print(band.items())
26+
27+
# verify a key exists
28+
print("guitar" in band)
29+
print("triangle" in band)
30+
31+
# Change values
32+
band["vocals"] = "Coverdale"
33+
band.update({"bass": "JPJ"})
34+
35+
print(band)
36+
37+
# Remove items
38+
print(band.pop("bass"))
39+
print(band)
40+
41+
band["drums"] = "Bonham"
42+
print(band)
43+
44+
print(band.popitem()) # tuple
45+
print(band)
46+
47+
# Delete and clear
48+
49+
band["drums"] = "Bonham"
50+
del band["drums"]
51+
print(band)
52+
53+
band2.clear()
54+
print(band2)
55+
56+
del band2
57+
58+
# Copy dictionaries
59+
60+
# band2 = band # create a reference
61+
# print("Bad copy!")
62+
# print(band2)
63+
# print(band)
64+
65+
# band2["drums"] = "Dave"
66+
# print(band)
67+
68+
band2 = band.copy()
69+
band2["drums"] = "Dave"
70+
print("Good copy!")
71+
print(band)
72+
print(band2)
73+
74+
# or use the dict() constructor function
75+
band3 = dict(band)
76+
print("Good copy!")
77+
print(band3)
78+
79+
# Nested dictionaries
80+
81+
member1 = {
82+
"name": "Plant",
83+
"instrument": "vocals"
84+
}
85+
member2 = {
86+
"name": "Page",
87+
"instrument": "guitar"
88+
}
89+
band = {
90+
"member1": member1,
91+
"member2": member2
92+
}
93+
print(band)
94+
print(band["member1"]["name"])
95+
96+
# Sets
97+
98+
nums = {1, 2, 3, 4}
99+
100+
nums2 = set((1, 2, 3, 4))
101+
102+
print(nums)
103+
print(nums2)
104+
print(type(nums))
105+
print(len(nums))
106+
107+
# No duplicate allowed
108+
nums = {1, 2, 2, 3}
109+
print(nums)
110+
111+
# True is a dupe of 1, False is a dupe of zero
112+
nums = {1, True, 2, False, 3, 4, 0}
113+
print(nums)
114+
115+
# check if a value is in a set
116+
print(2 in nums)
117+
118+
# but you cannot refer to an element in the set with an index position or a key
119+
120+
# Add a new element to a set
121+
nums.add(8)
122+
print(nums)
123+
124+
# Add elements from one set to another
125+
morenums = {5, 6, 7}
126+
nums.update(morenums)
127+
print(nums)
128+
129+
# you can use update with lists, tuples, and dictionaries, too.
130+
131+
# Merge two sets to create a new set
132+
one = {1, 2, 3}
133+
two = {5, 6, 7}
134+
135+
mynewset = one.union(two)
136+
print(mynewset)
137+
138+
# Keep only the duplicates
139+
one = {1, 2, 3}
140+
two = {2, 3, 4}
141+
142+
one.intersection_update(two)
143+
print(one)
144+
145+
# Keep everything except the duplicates
146+
one = {1, 2, 3}
147+
two = {2, 3, 4}
148+
149+
one.symmetric_difference_update(two)
150+
print(one)

0 commit comments

Comments
 (0)