generated from fhdsl/OTTR_Quarto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-Iteration.qmd
225 lines (137 loc) · 9.84 KB
/
02-Iteration.qmd
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Iteration
Today, we will look at how we iterate through data structures. Consider a the following list:
```{python}
heartrates = [68, 54, 72, 66, 90, 102, 49]
heartrates
```
If we want to compute some simple calculations on this list, we have some built-in functions to do that, such as `sum()`, `max()`, and so forth.
But what is going on behind the scenes in these functions? They all have to iterate through each element of the List. For `sum()`, the function has to iterate through each element of the list to add up the total sum. For `max()`, the function has to iterate through each element of the list and see if it has encountered a value bigger than it has seen before.
When we use a function on a data structure, we are almost always iterating through the elements of the data structure, and doing something with the elements as we go. So far in our journey in learning Python, we have left the iteration for the built-in function to carry out (which are optimized for performance). But this course will be focused on building custom functions that require us to iterate through the data on our own. This process will create a huge amount of flexibility and power in what you can do with your data!
## Iterable Data Structures
It turns out that we can iterate over *many* types of data structures in Python. These Data Structures are considered as **"Iterable"**:
- List
- Tuple
- String (yes, actually!)
- Series (but not recommended)
- DataFrame
- Ranges
- Dictionary
When a data structure is considered iterable, there are a few things you can do with it:
- Access elements or subset of the data structure via the bracket `[ ]` operator.
- Use the `in`, `not in` statements to check for presence of an element in the data structure.
- Examine the length via `len()`.
- Iterate through the data structure via a For-Loop.
You have seen examples of the first three actions already. Let's see how we can iterate through all of these iterable data structures via the For-Loop.
## For Loops
A "For-Loop" allows you to iterate over an iterable data structure, and execute a block of code once for each iteration. Here is what the syntax looks like:
```
for <variable> in <iterable>:
block of code
```
### Example: Iterating through a List
The following code will iterate through each element of the list `heartrates` and print out each element:
```{python}
heartrates = [68, 54, 72, 66, 90, 102]
for rate in heartrates:
print("Current heartrate:", rate)
```
Here is what the Python interpreter is doing:
1. Assign `heartrates` as a list.
2. Enter For-Loop: `rate` is assigned to the next element of `heartrates`. If it is the first time, `rate` is assigned as the first element of `heartrates`.
3. The "block of code" in the indented section is run, and the `rate` is printed.
4. Steps 2 and 3 are repeated until the last element of `heartrates`.
Now you see why it's called a "For-Loop": *for* an element of the iterable data structure, do the "block of code", and *loop* back to the top for the next element. You can have multiple lines of code in the indented section for the block of code.
### Example: Iterating through a List and summing its total
The following code will add up all the elements of a list:
```{python}
heartrates = [68, 54, 72, 66, 90, 102, 49]
total = 0
for rate in heartrates:
total = total + rate
print("Current total:", total)
print("Final total:", total)
```
We just reconstructed the `sum()` function!
Another way of seeing what happened is to use the following tool that allows you to step through Python code execution line-by-line and see the variables change.
<iframe width="800" height="500" frameborder="0" src="https://pythontutor.com/iframe-embed.html#code=heartrates%20%3D%20%5B68,%2054,%2072,%2066,%2090,%20102,%2049%5D%0Atotal%20%3D%200%0A%0Afor%20rate%20in%20heartrates%3A%0A%20%20total%20%3D%20total%20%2B%20rate%0A%20%20print%28%22Current%20total%3A%22,%20total%29%0A%20%20%0Aprint%28%22Final%20total%3A%22,%20total%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false">
</iframe>
If it doesn't load properly, here is the [link](https://pythontutor.com/render.html#code=heartrates%20%3D%20%5B68,%2054,%2072,%2066,%2090,%20102,%2049%5D%0Atotal%20%3D%200%0A%0Afor%20rate%20in%20heartrates%3A%0A%20%20total%20%3D%20total%20%2B%20rate%0A%20%20print%28%22Current%20total%3A%22,%20total%29%0A%20%20%0Aprint%28%22Final%20total%3A%22,%20total%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false).
### Example: Modifying each element while iterating
Sometimes you want to modify each element of an iterable data structure. However, if you modify the variable that is changing in the For-Loop, it won't change the original value in the data structure.
```{python}
import math
print("Before:", heartrates)
for rate in heartrates:
rate = math.log(rate)
print("After:", heartrates)
```
The code `rate = math.log(rate)` changes the value of rate, but it is not connected to `heartrates` anymore. Instead, we need to change `heartrates[index]`, where `index` is an integer that goes through all the indicies of `heartrates`.
We can do this with the `enumerate()` function:
```{python}
heartrates = [68, 54, 72, 66, 90, 102, 49]
print("Before:", heartrates)
for index, value in enumerate(heartrates):
print("Index:", index, " value:", value)
heartrates[index] = math.log(value)
#heartrates[index] = math.log(heartrates[index]) #this is okay also.
print("After:", heartrates)
```
What's going on here? The `enumerate()` function returns something that resembles a list of tuples for us to iterate through, where the first element of the tuple is the iteration index, and the second element of the tuple is the iteration element. We access this tuple through the short-hand `index, m` at the start of the For-Loop. Let's see what `enumerate()` looks like:
```{python}
print(list(enumerate(heartrates)))
```
Let's see this example step by step:
<iframe width="800" height="500" frameborder="0" src="https://pythontutor.com/iframe-embed.html#code=import%20math%0Aheartrates%20%3D%20%5B68,%2054,%2072,%2066,%2090,%20102,%2049%5D%0Aprint%28%22Before%3A%22,%20heartrates%29%0A%0Afor%20index,%20m%20in%20enumerate%28heartrates%29%3A%0A%20%20print%28%22Index%3A%22,%20index,%20%22%20%20%20m%3A%22,%20m%29%0A%20%20heartrates%5Bindex%5D%20%3D%20math.log%28m%29%0A%20%20%23heartrates%5Bindex%5D%20%3D%20math.log%28heartrates%5Bindex%5D%29%20%23this%20is%20okay%20also.%0A%20%20%0Aprint%28%22After%3A%22,%20heartrates%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false">
</iframe>
If it doesn't load properly, here is the [link](https://pythontutor.com/render.html#code=import%20math%0Aheartrates%20%3D%20%5B68,%2054,%2072,%2066,%2090,%20102,%2049%5D%0Aprint%28%22Before%3A%22,%20heartrates%29%0A%0Afor%20index,%20m%20in%20enumerate%28heartrates%29%3A%0A%20%20print%28%22Index%3A%22,%20index,%20%22%20%20%20m%3A%22,%20m%29%0A%20%20heartrates%5Bindex%5D%20%3D%20math.log%28m%29%0A%20%20%23heartrates%5Bindex%5D%20%3D%20math.log%28heartrates%5Bindex%5D%29%20%23this%20is%20okay%20also.%0A%20%20%0Aprint%28%22After%3A%22,%20heartrates%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false).
## For-Loops on other iterable data structures
### Tuple
You can loop through a Tuple just like you did with a List, but remember that you can't modify it!
### String
You can loop through a String by iterating on each letter within the String.
```{python}
message = "I am hungry"
for text in message:
print(text)
```
However, Strings are **immutable**, similar to Tuples. So if you iterate via `enumerate()`, you won't be able to modify the original String.
### Dictionary
When you loop through a Dictionary, you loop through the Keys of the Dictionary:
```{python}
sentiment = {'happy': 8, 'sad': 2, 'joy': 7.5, 'embarrassed': 3.6, 'restless': 4.1, 'apathetic': 3.8, 'calm': 7}
for key in sentiment:
print("key:", key)
```
The `.items()` method for Dictionary is similar to the `enumerate()` function: it returns a list of tuples, and within each tuple the first element is a key, and the second element is a value.
```{python}
sentiment.items()
```
```{python}
for key, value in sentiment.items():
print(key, "corresponds to ", value)
```
### Ranges
**Ranges** are a collection of sequential numbers, such as:
- 1, 2, 3, 4, 5
- 1, 3, 5
- 10, 15, 20, 25, 30
It seems natural to treat Ranges as Lists, but the neat thing about them is that only the bare minimum information is stored: the start, end, and step size. This could be a huge reduction in memory...if you need a sequence of numbers between 1 and 1 million, you can either store all 1 million values in a list, or you can just have a Range that holds the start: 1, the end: 1 million, and the step size: 1. That's a big difference!
You can create a Range via the following ways:
- `range(stop)` which starts at 0 and ends in `stop` - 1.
- `range(start, stop)` which starts at `start` and ends in `stop` - 1
- `range(start, stop, step)` which starts at `start` and ends in `stop` - 1, with a step size of `step`.
When you create a Range object, it just tells you what the input values you gave it.
```{python}
range(5, 50, 5)
```
Convert to a list to see its actual values:
```{python}
list(range(5, 50, 5))
```
To use Ranges in a For-Loop, it's straightforward:
```{python}
for i in range(5, 50, 5):
print(i)
```
## Exercises
Exercise for week 2 can be found [here](https://colab.research.google.com/drive/1kPVyALVVn7__x0q6kfE9PKRpGNQgtu0a?usp=sharing).