@@ -84,6 +84,19 @@ Running the program may look like this:
84
84
Or is it? (y=yes, n=no) n
85
85
It's not raining anymore.
86
86
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
+
87
100
We can also interrupt a loop even if the condition is still true using
88
101
the ` break ` keyword. In this case, we'll set condition to True and rely
89
102
on nothing but ` break ` to end the loop.
@@ -109,6 +122,17 @@ The program works like this:
109
122
Is it raining? (y=yes, n=no) n
110
123
It's not raining anymore.
111
124
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
+
112
136
## Until loops
113
137
114
138
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:
154
178
155
179
``` py
156
180
>> > 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
158
182
>> > index = 0
159
183
>> > while index < length_of_stuff:
160
184
... print (stuff[index])
@@ -168,8 +192,9 @@ how about you
168
192
>> >
169
193
```
170
194
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.
173
198
174
199
This is when for loops come in:
175
200
@@ -202,8 +227,12 @@ how about you
202
227
>> >
203
228
```
204
229
205
- There's only one big limitation with for looping over lists. You
206
- shouldn't modify the list in the for loop. If you do, the results can
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
235
+ shouldn't modify the list in the for loop. If we do, the results can
207
236
be surprising:
208
237
209
238
``` py
@@ -216,7 +245,7 @@ be surprising:
216
245
>> >
217
246
```
218
247
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.
220
249
221
250
``` py
222
251
>> > 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.
228
257
>> >
229
258
```
230
259
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 ) :
232
262
233
263
``` py
234
264
>> > stuff = [' hello' , ' hi' , ' how are you doing' , ' im fine' , ' how about you' ]
0 commit comments