- Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathForLoops.md.txt
282 lines (219 loc) · 9.83 KB
/
ForLoops.md.txt
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
---
jupyter:
jupytext:
text_representation:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.13.6
kernelspec:
display_name: Python 3
language: python
name: python3
---
<!-- #raw raw_mimetype="text/restructuredtext" -->
.. meta::
:description: Topic: Control flow with for-loops and while-loops, Difficulty: Easy, Category: Section
:keywords: for-loop, while-loop, break, control flow, basic programming
<!-- #endraw -->
# For-Loops and While-Loops
In this section, we will introduce the essential "for-loop" control flow paradigm along with the formal definition of an "iterable". The utility of these items cannot be understated. Moving forward, you will likely find use for these concepts in nearly every piece of Python code that you write!
<div class="alert alert-warning">
**Note**:
There are reading-comprehension exercises included throughout the text. These are meant to help you put your reading to practice. Solutions for the exercises are included at the bottom of this page.
</div>
<!-- #region -->
## For-Loops
A "for-loop" allows you to iterate over a collection of items, and execute a block of code once for each iteration. For example, the following code will sum up all the positive numbers in a tuple:
```python
total = 0
for num in (-22.0, 3.5, 8.1, -10, 0.5):
if num > 0:
total = total + num
```
The general syntax for a "for-loop" is:
```
for <var> in <iterable>:
block of code
```
Where `<var>` is any valid variable-identifier and `<iterable>` is any **iterable**. We will discuss iterables more formally in the next section; suffice it to know that every sequence-type object is an iterable. The `for`statement must end in a colon character, and the body the for-loop is [whitespace-delimited](https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Introduction.html#Python-Uses-Whitespace-to-Delimit-Scope).
The for-loop behaves as follows:
- Request the next member of the iterable.
- If the iterable is empty, exit the for-loop without running its body.
- If the iterable did produce a member, assign that member to `<var>` (if `<var>` was not previously defined, it becomes defined).
- Execute the enclosed body of code.
- Go back to the first step.
To be concrete, let's consider the example:
```python
# demonstrating a basic for-loop
total = 0
for item in [1, 3, 5]:
total = total + item
print(total) # `total` has the value 1 + 3 + 5 = 9
# `item` is still defined here, and holds the value 5
```
This code will perform the following steps:
1. Define the variable `total`, and assign it the value `0`
2. Iterate on the list, producing value `1`, define the variable `item` and assign it the value `1`
3. Assign `total` the value `0 + 1`
4. Iterate on the list, producing the value `3` and assigning it to `item`
5. Assign `total` the value `1 + 3`
6. Iterate on the list, producing the value `5` and assigning it to `item`
7. Assign `total` the value `4 + 5`
8. Iterate on the list. Having reached its end, a `StopIteration` signal it raised by the list, and the for-loop sequence is exited.
9. Print the value of `total` (9)
#### Potential Pitfall
Note that the variable `item` will persist after the for-loop block is exited. It will reference the last value from the for-loop iteration (in this case `item` has the value 5). That being said, *you should not write code that depends on the iterate-variable, outside of the context of the for-loop*. In the case that you try to loop over an *empty* iterable, the iterate-variable is never defined:
```python
for x in []: # the iterable is empty - the iterate-variable `x` will not be defined
print("Hello?") # this code is never executed
print(x) # raises an error because `x` was never defined
```
Because we are attempting to iterate over an empty list, `StopIteration` is raised immediately - before the variable `x` is even defined. Thus the code enclosed within the for-loop is never reached, and the subsequent `print(x)` statement will raise a `NameError`, because `x` was never defined!
<!-- #endregion -->
<div class="alert alert-info">
**Reading Comprehension: A basic for-loop**
Using a for-loop and an if-statement, print each letter in the string `"abcdefghij"`, if that letter is a vowel.
</div>
<!-- #region -->
## While-Loops
A "while-loop" allows you to repeat a block of code until a condition is no longer true:
```
while <condition>:
block of code
```
Where `<condition>` is an expression that returns `True` or `False`, or is any object on which `bool` can be called. The "body" of the while-loop is the code indented beneath the while-loop statement.
The while-loop behaves as follows:
- Call `bool(<condition>)` and execute the indented block of code if `True` is returned. Otherwise, "exit" the while-loop, skipping past the indented code.
- If the indented block code is executed, go back to the first step.
To be concrete, let's consider the example:
```python
# demonstrating a basic while-loop
total = 0
while total < 2:
total += 1 # equivalent to: `total = total + 1`
print(total) # `total` has the value 2
```
This code will perform the following steps:
1. Define the variable `total`, and assign it the value `0`
2. Evaluate `0 < 2`, which returns `True`: enter the enclosed code-block
3. Execute the code block: assign `total` the value `0 + 1`
4. Evaluate `1 < 2`, which returns `True`: enter the enclosed code-block
5. Execute the code block: assign `total` the value `1 + 1`
6. Evaluate `2 < 2`, which returns `False`: *skip* the enclosed code-block
7. Print the value of `total` (2)
Note that if we started off with `total = 3`, the condition-expression `3 < 2` would evaluate to `False` outright, and the indented body of code would never be reached.
<div class="alert alert-warning">
**Warning!**
It is possible to write a while-loop such that its conditional statement is always True, in which case your code will run ceaselessly! If this ever happens to you in a Jupyter notebook, either interrupt or restart your kernel.
</div>
<!-- #endregion -->
<div class="alert alert-info">
**Reading Comprehension: A basic while-loop**
Given a list of nonzero, positive numbers, `x`, append the sum of that list to the end of it. Do this until the last value in `x` is at least 100. Use a while-loop.
If you start with `x = [1]`, then by the end of your while-loop `x` should be `[1, 1, 2, 4, 8, 16, 32, 64, 128]`.
</div>
<!-- #region -->
## `break`, `continue`, & `else` clauses on loops
The `continue` and `break` statements can be used within the bodies of both for-loops and while-loops. They provide added means for "short-circuiting" or prematurely exiting a given loop, respectively.
Encountering `break` within a given loop causes that loop to be exited immediately:
```ipython
# breaking out of a loop early
>>> for item in [1, 2, 3, 4, 5]:
... if item == 3:
... print(item, " ...break!")
... break
... print(item, " ...next iteration")
```
```
1 ...next iteration
2 ...next iteration
3 ...break!
```
An `else` clause can be added to the end of any loop. The body of this else-statement will be executed *only if the loop was not exited via a `break` statement*.
```ipython
# including an else-clause at the end of the loop
>>> for item in [2, 4, 6]:
... if item == 3:
... print(item, " ...break!")
... break
... print(item, " ...next iteration")
... else:
... print("if you are reading this, then the loop completed without a 'break'")
```
```
2 ...next iteration
4 ...next iteration
6 ...next iteration
if you are reading this, then the loop completed without a 'break'
```
The `continue` statement, when encountered within a loop, causes the loop-statement to be revisited immediately.
```python
# demonstrating a `continue` statement in a loop
>>> x = 1
>>> while x < 4:
... print("x = ", x, ">> enter loop-body <<")
... if x == 2:
... print("x = ", x, " continue...back to the top of the loop!")
... x += 1
... continue
... x += 1
... print("--reached end of loop-body--")
```
```
x = 1 >> enter loop-body <<
--reached end of loop-body--
x = 2 >> enter loop-body <<
x = 2 continue...back to the top of the loop!
x = 3 >> enter loop-body <<
--reached end of loop-body--
```
<!-- #endregion -->
<div class="alert alert-info">
**Reading Comprehension: conducting flow in a loop**
Loop over a list of integers repeatedly, summing up all of its even values, and adding the content to a total. Repeat this process until the the total exceeds 100, or if you have looped over the list more than 50 times. Print the total only if it exceeds 100.
</div>
## Links to Official Documentation
- ['for' statement](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement)
- ['while' statement](https://docs.python.org/3/reference/compound_stmts.html#the-while-statement)
- ['break', 'continue', and 'else' clauses](https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops)
- ['pass' statment](https://docs.python.org/3/tutorial/controlflow.html#pass-statements)
## Reading Comprehension Exercise Solutions:
<!-- #region -->
**A basic for-loop: Solution**
```python
for letter in "abcdefghij":
if letter in "aeiou":
print(letter)
```
<!-- #endregion -->
<!-- #region -->
**A basic while-loop: Solution**
```python
while x[-1] < 100:
x.append(sum(x))
```
<!-- #endregion -->
<!-- #region -->
**Conducting flow in a loop: Solution**
```python
x = [3, 4, 1, 2, 8, 10, -3, 0]
num_loop = 0
total = 0
while total < 100:
for item in x:
# return to for-loop if
# `item` is odd-valued
if item % 2 == 1:
continue
else:
total += item
num_loop += 1
# break from while-loop if
# more than 50 items tallied
if 50 < num_loop:
break
else:
print(total)
```
<!-- #endregion -->