Skip to content

Commit c032394

Browse files
committed
2 parents 49174bf + 29e1cd8 commit c032394

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

if.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ but it's not possible to create custom keywords. That's why keywords are
7373
usually used for "magic" things that would be difficult to do with just
7474
functions.
7575

76+
Also note that if statements check the condition once only, so if you
77+
set it to false later the if statement won't notice it.
78+
79+
```py
80+
>>> its_raining = True
81+
>>> if its_raining:
82+
... its_raining = False
83+
... print("It's not raining, but this runs anyway.")
84+
...
85+
It's not raining, but this runs anyway.
86+
>>>
87+
```
88+
7689
## Storing code in files
7790

7891
At this point it's easier to put your code into a file and use it
@@ -167,7 +180,20 @@ else:
167180
print("It's not raining.")
168181
```
169182

170-
By combining that with the input function we can make a program that
183+
The else part simply runs when the if statement doesn't run. It doesn't
184+
check the condition again.
185+
186+
```py
187+
>>> its_raining = True
188+
>>> if its_raining:
189+
... its_raining = False
190+
... else:
191+
... print("It's not raining, but this still doesn't run.")
192+
...
193+
>>>
194+
```
195+
196+
By combining `else` with the input function we can make a program that
171197
asks for a password and checks if it's correct.
172198

173199
```py

loops.md

Lines changed: 37 additions & 7 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,8 +227,12 @@ how about you
202227
>>>
203228
```
204229

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
207236
be surprising:
208237

209238
```py
@@ -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)