Skip to content

Commit b3a0219

Browse files
committed
more content was added to loops
1 parent c8c4dbf commit b3a0219

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

basics/loops.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,29 @@ immediately.
133133
>>>
134134
```
135135

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+
136159
## Until loops
137160

138161
Python doesn't have until loops. If we need an until loop, we can use
@@ -212,6 +235,7 @@ how about you
212235
>>>
213236
```
214237

238+
215239
Without the comments, that's only two simple lines, and one variable.
216240
Much better than anything else we tried before.
217241

@@ -232,6 +256,7 @@ Here the `in` keyword is just a part of the for loop and it has a
232256
different meaning than it would have if we had `thing in stuff` without
233257
a `for`. Trying to do `for (thing in stuff):` creates an error.
234258

259+
235260
Right now the while loop version might seem easier to understand for
236261
you, but later you'll realize that for loops are much easier to work
237262
with than while loops and index variables, especially in large projects.
@@ -260,9 +285,30 @@ c
260285
>>>
261286
```
262287

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+
263308
If we can for loop over something, then that something is **iterable**.
264309
Lists, tuples and strings are all iterable.
265310

311+
266312
There's only one big limitation with for looping over lists. We
267313
shouldn't modify the list in the for loop. If we do, the results can
268314
be surprising:

0 commit comments

Comments
 (0)