Skip to content

Commit 9cfac8d

Browse files
committed
explain stuff better
1 parent 71129fe commit 9cfac8d

1 file changed

Lines changed: 36 additions & 6 deletions

File tree

loops.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ Running the program may look like this:
8484
Or is it? (y=yes, n=no) n
8585
It's not raining anymore.
8686

87+
The while loop doesn't check the condition all the time, it only checks
88+
it in the beginning.
89+
90+
```py
91+
>>> its_raining = True
92+
>>> while its_raining:
93+
... its_raining = False
94+
... print("It's not raining, but the while loop doesn't know it yet.")
95+
...
96+
It's not raining, but the while loop doesn't know it yet.
97+
>>>
98+
```
99+
87100
We can also interrupt a loop even if the condition is still true using
88101
the `break` keyword. In this case, we'll set condition to True and rely
89102
on nothing but `break` to end the loop.
@@ -109,6 +122,17 @@ The program works like this:
109122
Is it raining? (y=yes, n=no) n
110123
It's not raining anymore.
111124

125+
Unlike setting the condition to False, breaking the loop ends it
126+
immediately.
127+
128+
```py
129+
>>> while True:
130+
... break
131+
... print("This is never printed.")
132+
...
133+
>>>
134+
```
135+
112136
## Until loops
113137

114138
Python doesn't have until loops. If you need an until loop, use
@@ -154,7 +178,7 @@ We could also create an index variable, and use a while loop:
154178

155179
```py
156180
>>> stuff = ['hello', 'hi', 'how are you doing', 'im fine', 'how about you']
157-
>>> length_of_stuff = len(stuff) # len(x) is the length of x, 5 in this case
181+
>>> length_of_stuff = len(stuff) # len(stuff) is 5
158182
>>> index = 0
159183
>>> while index < length_of_stuff:
160184
... print(stuff[index])
@@ -168,8 +192,9 @@ how about you
168192
>>>
169193
```
170194

171-
But there's `len()` and an index variable we need to increment. That's
172-
a lot of stuff to worry about for just printing each item.
195+
But there's `len()` and an index variable we need to increment and a
196+
while loop and many other things to worry about. That's a lot of work
197+
just for printing each item.
173198

174199
This is when for loops come in:
175200

@@ -202,7 +227,11 @@ how about you
202227
>>>
203228
```
204229

205-
There's only one big limitation with for looping over lists. You
230+
Right now the while loop version might seem easier to understand for
231+
you, but later you'll realize that for loops are much easier to work
232+
with than while loops and index variables, especially in large projects.
233+
234+
There's only one big limitation with for looping over lists. We
206235
shouldn't modify the list in the for loop. If you do, the results can
207236
be surprising:
208237

@@ -216,7 +245,7 @@ be surprising:
216245
>>>
217246
```
218247

219-
Instead, you can create a copy of stuff and loop over it.
248+
Instead, we can create a copy of stuff and loop over it.
220249

221250
```py
222251
>>> stuff = ['hello', 'hi', 'how are you doing', 'im fine', 'how about you']
@@ -228,7 +257,8 @@ Instead, you can create a copy of stuff and loop over it.
228257
>>>
229258
```
230259

231-
Or if you want to clear a list, just use the `.clear()` list method:
260+
Or if we just want to clear a list, we can use the `clear`
261+
[list method](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists):
232262

233263
```py
234264
>>> stuff = ['hello', 'hi', 'how are you doing', 'im fine', 'how about you']

0 commit comments

Comments
 (0)