Skip to content

Commit fe550b6

Browse files
committed
Check m05 collections
1 parent 5b4033b commit fe550b6

6 files changed

Lines changed: 226 additions & 82 deletions

File tree

slides/content/m05_collections.md

Lines changed: 137 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,162 +3,217 @@
33
# 🐍 Collections
44
<!-- .element: class="headline" -->
55

6-
What **lists and arrays** are in Java, are just **lists** in Python.
7-
What **maps** are in Java, are **dictionaries** in Python.
8-
And yes, **sets** are called the same in Python and Java.
6+
We will only look into a few collection types of Python.
7+
Find them all at [docs.python.org/3/library/stdtypes.html](https://docs.python.org/3/library/stdtypes.html)
8+
9+
- **Lists and arrays** in Java ➡ are just `list` in Python (but, there is also `tuple`)
10+
- **Maps** in Java ➡ are `dict` in Python
11+
- **Sets** in Java ➡ are also `set` in Python
912

1013
---
1114

12-
## `list`
15+
## Lists 1/2
1316

1417
Lists hold multiple elements in a specific order.
1518

16-
```py
19+
```py [|3-5|8-9|11|13-14|16-17|]
20+
# ./python/m05_list_operations.py#L1-L15
21+
1722
numbers = [1, 2, 3, 4, 5]
18-
names = ["Max", "Alex", "Anton"]
23+
names = ["Janine", "Ali", "Alice"]
1924
mixed = [1, 2, "Max", 3.141]
20-
```
2125

22-
### Basic list operations
2326

24-
```py
25-
# get element at position
26-
names[0]
27+
names[0] # get element by index
28+
names[0] = "Peter" # set element at index
29+
30+
names[-1] # use negative indexes to count from the end
2731

28-
# use negative indexes to count from the end
29-
names[-1]
32+
names.append(-5) # add element to the end of the list
33+
names.insert(1, "Bob") # add element at specific index
3034

31-
# set element at position
32-
names[0] = "Peter"
35+
names.remove(-5) # remove the first occurrence from the list
36+
del names[0] # remove by index
37+
```
3338

34-
# add an element to the end of the list
35-
names.append(-5)
39+
---
3640

37-
# add at a specific position
38-
numbers.insert(1, 100)
41+
## Lists 2/2: More list operations
3942

40-
# remove the first occurrence from the list
41-
names.remove(-5)
43+
```py [|3-4|6-7|9-10|]
44+
# ./python/m05_list_operations.py#L18-L26
45+
46+
"Alice" in names # check for existence
47+
"Mario" not in names # ... and non-existence
48+
49+
numbers.count(1) # the number of times this item is in the list
50+
len(numbers) # total number of elements in the list
51+
52+
# merge two lists into one
53+
merged = [1, 2, 3] + [4, 5, 6]
4254

43-
# remove by index
44-
del names[0]
4555
```
4656

57+
<br />
58+
59+
Learn about all list operations at [docs.python.org/3/tutorial/datastructures.html#more-on-lists](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists).
60+
4761
---
4862

49-
### Check for existence of list elements
63+
## Tuples 1/2
5064

51-
```py
52-
# check for existence
53-
"Alex" in names
65+
Tuples hold two or more objects together in an efficient manner. They have NO direct equivalent in Java.
66+
Use them to group together small number of elements, e.g., when returning multiple values from a method.
5467

55-
# ... and not-existence
56-
"Mario" not in names
68+
```py
69+
pair = ("Jonas", 12) # create a tuple
70+
pair[0] # get element by index, similar to lists
5771
```
5872

59-
### Merge, count, and check the length of lists
73+
**Tuples are always immutable!**
74+
You can NOT modify, append, or delete anything from them after you created one.
6075

6176
```py
62-
# merging two lists
63-
merged = [1, 2, 3] + [4, 5, 6]
77+
# pair[1] = 13
78+
# 💥 TypeError: 'tuple' object does not support item assignment
79+
80+
# pair.append(123)
81+
# 💥 AttributeError: 'tuple' object has no attribute 'append'
82+
```
6483

65-
# the number of times this item is in the list
66-
numbers.count(1)
84+
However, you can convert tuples to lists.
6785

68-
# total number of elements in the list
69-
len(numbers)
86+
```py
87+
numbers = (10, 11, 12)
88+
list(numbers)
89+
90+
# and back again ...
91+
letters = ["A", "B", "C"]
92+
tuple(letters)
7093
```
7194

72-
### More list operations
95+
---
96+
97+
## Tuples 2/2: Collection Destructuring
98+
99+
**Destructuring** helps you to quickly retrieve elements from list-like types in Python.
100+
101+
```py [|3-6|9-14|17-22|]
102+
# ./python/m05_destructuring.py
103+
104+
x, y = 1, 2
105+
print(x, y)
106+
107+
# > 1 2
108+
109+
110+
numbers = [1, 2, 3]
111+
x, y, z = numbers
112+
113+
print(x, y, z)
73114

74-
Learn about all list operations at [docs.python.org/3/tutorial/datastructures.html#more-on-lists](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists)
115+
# > 1 2 3
116+
117+
118+
pair = ("Jonas", 12)
119+
name, age = pair
120+
121+
print(name, age)
122+
123+
# > Jonas 12
124+
125+
```
126+
127+
Actually, whenever you use a comma `,` in such contexts, a `tuple` is used implicitly.
75128

76129
---
77130

78-
## `dict`
131+
## Dictionaries
79132

80133
Dictionaries map keys to values.
81134

82-
```py
83-
my_grades = {
135+
```py [|3-10|13-15|17-19|21-22|]
136+
# ./python/m05_dict_operations.py
137+
138+
grades = {
84139
"math": 2,
85140
"programming": 1,
86141
"literature": 3
87142
}
88143

89144
# alternative syntax
90-
my_grades = dict(math=2, programming=1, literature=3)
91-
```
145+
grades = dict(math=2, programming=1, literature=3)
92146

93-
### Basic dict operations
94-
95-
```py
96-
# get elements by key
97-
my_grades["math"]
98147

99-
# set elements by key
100-
my_grades["math"] = 5
101-
102-
# add a new element
103-
my_grades["electronics"] = 4
148+
grades["math"] # get elements by key
149+
grades["math"] = 5 # set elements by key
150+
grades["electronics"] = 4 # add a new element
104151

105152
# remove an element (will raise an error if the key does not exist)
106-
if "math" in my_grades:
107-
del my_grades["math"]
108-
```
153+
if "math" in grades:
154+
del grades["math"]
109155

110-
---
156+
grades.keys() # get all the keys as a list
157+
grades.values() # get all the values as a list
111158

112-
### Inspect dict elements
113-
114-
```py
115-
# get all the keys as a list
116-
my_grades.keys()
117-
118-
# get all the values as a list
119-
my_grades.values()
120159
```
121160

122161
---
123162

124-
## `set`
163+
## Sets 1/2
125164

126165
Sets hold multiple elements, without duplicates, but also without order.
127166

128-
### Basic set operations
167+
```py [|3|5-6|8|10-12|]
168+
# ./python/m05_set_operations.py#L1-L10
129169

130-
```py
131-
# notice how the '1' is only appended once after all
132-
my_set = {1, 2, 3, 5}
170+
numbers = {1, 1, 2, 3, 5} # notice how the '1' is only appended once after all
133171

134-
# check for existence (much faster than with lists)
135-
1 in my_set
172+
numbers.add(7) # add new elements
173+
numbers.add(1) # add elements that already exist (no effect)
136174

137-
# add elements
138-
my_set.add(7)
175+
1 in numbers # check for existence (much faster than with lists)
139176

140177
# remove elements (will raise an error if the element does not exist)
141-
if 2 in my_set:
142-
my_set.remove(2)
143-
my_set
178+
if 2 in numbers:
179+
numbers.remove(2)
180+
```
181+
182+
**You can NOT retrieve elements by index from a set!**
183+
184+
```py
185+
# numbers[0]
186+
# 💥 TypeError: 'set' object is not subscriptable
144187
```
145188

146-
### Retrieve elements from sets
189+
You must iterate over them or convert the set to a list with `list(elements)`
147190

148191
```py
149-
# you can NOT retrieve elements by index from a set
150-
my_set[0]
192+
# ./python/m05_set_operations.py#L12-L14
151193

152-
# ... you could convert it to a list with `list(my_set)` or even better: use an iterator
194+
# iterate over set elements
195+
for val in numbers:
196+
print(val)
153197
```
154198

155-
### Set arithmetic
199+
---
200+
201+
## Sets 2/2: Set Arithmetic
202+
203+
Sets are handy when you want to apply operations from set theory.
156204

157205
```py
206+
# ./python/m05_set_arithmetic.py
207+
158208
a = {1, 2, 3, 4, 5}
159209
b = {4, 5, 6, 7, 8}
160210

161211
print("a | b =", a | b) # union
162212
print("a & b =", a & b) # intersection
163213
print("a - b =", a - b) # difference
214+
215+
# > a | b = {1, 2, 3, 4, 5, 6, 7, 8}
216+
# > a & b = {4, 5}
217+
# > a - b = {1, 2, 3}
218+
164219
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
x, y = 1, 2
2+
print(x, y)
3+
4+
# > 1 2
5+
6+
7+
numbers = [1, 2, 3]
8+
x, y, z = numbers
9+
10+
print(x, y, z)
11+
12+
# > 1 2 3
13+
14+
15+
pair = ("Jonas", 12)
16+
name, age = pair
17+
18+
print(name, age)
19+
20+
# > Jonas 12
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
grades = {
2+
"math": 2,
3+
"programming": 1,
4+
"literature": 3
5+
}
6+
7+
# alternative syntax
8+
grades = dict(math=2, programming=1, literature=3)
9+
10+
11+
grades["math"] # get elements by key
12+
grades["math"] = 5 # set elements by key
13+
grades["electronics"] = 4 # add a new element
14+
15+
# remove an element (will raise an error if the key does not exist)
16+
if "math" in grades:
17+
del grades["math"]
18+
19+
grades.keys() # get all the keys as a list
20+
grades.values() # get all the values as a list
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
numbers = [1, 2, 3, 4, 5]
2+
names = ["Janine", "Ali", "Alice"]
3+
mixed = [1, 2, "Max", 3.141]
4+
5+
6+
names[0] # get element by index
7+
names[0] = "Peter" # set element at index
8+
9+
names[-1] # use negative indexes to count from the end
10+
11+
names.append(-5) # add element to the end of the list
12+
names.insert(1, "Bob") # add element at specific index
13+
14+
names.remove(-5) # remove the first occurrence from the list
15+
del names[0] # remove by index
16+
17+
18+
"Alice" in names # check for existence
19+
"Mario" not in names # ... and non-existence
20+
21+
numbers.count(1) # the number of times this item is in the list
22+
len(numbers) # total number of elements in the list
23+
24+
# merge two lists into one
25+
merged = [1, 2, 3] + [4, 5, 6]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
a = {1, 2, 3, 4, 5}
2+
b = {4, 5, 6, 7, 8}
3+
4+
print("a | b =", a | b) # union
5+
print("a & b =", a & b) # intersection
6+
print("a - b =", a - b) # difference
7+
8+
# > a | b = {1, 2, 3, 4, 5, 6, 7, 8}
9+
# > a & b = {4, 5}
10+
# > a - b = {1, 2, 3}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
numbers = {1, 1, 2, 3, 5} # notice how the '1' is only appended once after all
2+
3+
numbers.add(7) # add new elements
4+
numbers.add(1) # add elements that already exist (no effect)
5+
6+
1 in numbers # check for existence (much faster than with lists)
7+
8+
# remove elements (will raise an error if the element does not exist)
9+
if 2 in numbers:
10+
numbers.remove(2)
11+
12+
# iterate over set elements
13+
for val in numbers:
14+
print(val)

0 commit comments

Comments
 (0)