@@ -133,6 +133,29 @@ immediately.
133
133
>> >
134
134
```
135
135
136
+ While with a condition
137
+
138
+ ``` python
139
+ >> > cont = 10
140
+ >> > while (cont >= 0 ):
141
+ ... print (cont)
142
+ ... cont -= 1
143
+ ...
144
+ 10
145
+ 9
146
+ 8
147
+ 7
148
+ 6
149
+ 5
150
+ 4
151
+ 3
152
+ 2
153
+ 1
154
+ 0
155
+ >> >
156
+ ```
157
+
158
+
136
159
## Until loops
137
160
138
161
Python doesn't have until loops. If we need an until loop, we can use
@@ -212,6 +235,7 @@ how about you
212
235
>> >
213
236
```
214
237
238
+
215
239
Without the comments, that's only two simple lines, and one variable.
216
240
Much better than anything else we tried before.
217
241
@@ -232,6 +256,7 @@ Here the `in` keyword is just a part of the for loop and it has a
232
256
different meaning than it would have if we had ` thing in stuff ` without
233
257
a ` for ` . Trying to do ` for (thing in stuff): ` creates an error.
234
258
259
+
235
260
Right now the while loop version might seem easier to understand for
236
261
you, but later you'll realize that for loops are much easier to work
237
262
with than while loops and index variables, especially in large projects.
260
285
>> >
261
286
```
262
287
288
+ You can print the multiplication tables.
289
+
290
+ ``` python
291
+ >> > table = 2
292
+ >> > for i in range (1 , 11 ):
293
+ ... print (table," x" , i, " =" ,(table* i))
294
+ ...
295
+ 2 x 1 = 2
296
+ 2 x 2 = 4
297
+ 2 x 3 = 6
298
+ 2 x 4 = 8
299
+ 2 x 5 = 10
300
+ 2 x 6 = 12
301
+ 2 x 7 = 14
302
+ 2 x 8 = 16
303
+ 2 x 9 = 18
304
+ 2 x 10 = 20
305
+ >> >
306
+ ```
307
+
263
308
If we can for loop over something, then that something is ** iterable** .
264
309
Lists, tuples and strings are all iterable.
265
310
311
+
266
312
There's only one big limitation with for looping over lists. We
267
313
shouldn't modify the list in the for loop. If we do, the results can
268
314
be surprising:
0 commit comments