-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-Iteration.qmd
256 lines (165 loc) · 12.3 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
---
title: "Iteration over Data Structures"
format: html
editor: visual
---
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 (to be introduced in exercises)
- 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 interpretor 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).
## Conditional Statements
As we iterate through values in a data structure, it is quite common to want to have your code do different things depending on the value. For instance, suppose you are recoding `heartrates`, and the numerical values should be "low" if it is between 0 and 60, "medium" if it is between 60 and 100, "high" if it is above 100, and "unknown" otherwise (when it is below 0 or other data type).
Stepping back, we have been working with a *linear* way of executing code - we have unconditionally executing every line of code in our program. Here, we are create a "control flow" via **conditional statements** in which the your code will run a certain section *if* some conditions are met.
Here is how the syntax looks like for conditional statements:
```
if <expression1>:
block of code 1
elif <expression2>:
block of code 2
else:
block of code 3
block of code 4
```
There are three possible ways the code can run:
1. If `<expression1>` is evaluated as `True`, then `block of code 1` will be run. When done, it will continue to `block of code 4`.
2. If `<expression1>` is evaluated as `False`, then it will ask if `<expression2>` is `True` or not. If `True`, then `block of code 2` will be run. When done, it will continue to `block of code 4`.
3. If `<expression1>` and `<expression2>` are both evaluated as `False`, then `block of code 3` is run. When done, it will continue to `block of code 4`.
An important takeaway is that *only one block of code can be run*.
Let's see how this apply to the data recoding example. Let's just assume the data we want to recode is just a single value in a variable `rate`, not the entire list `heartrates`:
```{python}
rate = heartrates[0]
print(rate)
if rate > 0 and rate <= 60:
rate = "low"
elif rate > 60 and rate <= 100:
rate = "medium"
elif rate > 100:
rate = "high"
else:
rate = "unknown"
print(rate)
```
You don't always need multiple `if`, `elif`, `else` statements when writing conditional statements. In its simplest form, a conditional statement requires only an `if` clause:
```{python}
x = -12
if x < 0:
x = x * -1
print(x)
```
Then, you can add an `elif` or `else` statement, if you like. Here's `if`-`elif`:
```{python}
x = .25
if x < 0:
x = x * -1
elif x >= 0 and x < 1:
x = 1 / x
print(x)
```
Here's `if`-`else`. The `in` statement asks whether an element (102) is found in an iterable data structure `heartrates`, and returns `True` if so:
```{python}
if 102 in heartrates:
print("Found 102.")
else:
print("Did not find 102.")
```
Finally, let's put the data recoding example within a For-Loop:
```{python}
heartrates = [68, 54, 72, 66, 90, 102]
for index, rate in enumerate(heartrates):
if rate > 0 and rate <= 60:
heartrates[index] = "low"
elif rate > 60 and rate <= 100:
heartrates[index] = "medium"
elif rate > 100:
heartrates[index] = "high"
else:
heartrates[index] = "unknown"
print(heartrates)
```
Let's see this in action step by step:
<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%5D%0A%0Afor%20index,%20rate%20in%20enumerate%28heartrates%29%3A%0A%20%20if%20rate%20%3E%200%20and%20rate%20%3C%3D%2060%3A%0A%20%20%20%20heartrates%5Bindex%5D%20%3D%20%22low%22%0A%20%20elif%20rate%20%3E%2060%20and%20rate%20%3C%3D%20100%3A%0A%20%20%20%20heartrates%5Bindex%5D%20%3D%20%22medium%22%0A%20%20elif%20rate%20%3E%20100%3A%0A%20%20%20%20heartrates%5Bindex%5D%20%3D%20%22high%22%0A%20%20else%3A%0A%20%20%20%20heartrates%5Bindex%5D%20%3D%20%22unknown%22%0A%20%20%20%20%0Aprint%28heartrates%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, you can find it [here](https://pythontutor.com/render.html#code=heartrates%20%3D%20%5B68,%2054,%2072,%2066,%2090,%20102%5D%0A%0Afor%20index,%20rate%20in%20enumerate%28heartrates%29%3A%0A%20%20if%20rate%20%3E%200%20and%20rate%20%3C%3D%2060%3A%0A%20%20%20%20heartrates%5Bindex%5D%20%3D%20%22low%22%0A%20%20elif%20rate%20%3E%2060%20and%20rate%20%3C%3D%20100%3A%0A%20%20%20%20heartrates%5Bindex%5D%20%3D%20%22medium%22%0A%20%20elif%20rate%20%3E%20100%3A%0A%20%20%20%20heartrates%5Bindex%5D%20%3D%20%22high%22%0A%20%20else%3A%0A%20%20%20%20heartrates%5Bindex%5D%20%3D%20%22unknown%22%0A%20%20%20%20%0Aprint%28heartrates%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false)
## Exercises
Exercise for week 2 can be found [here](https://colab.research.google.com/drive/1kPVyALVVn7__x0q6kfE9PKRpGNQgtu0a?usp=sharing).