π Fundamentals Of Digital Discipleship, Part XIII: The While Statement
The while statement is used for repeated execution as long as an expression is true. The break and continue keywords come in handy for granular control over the loop. While can also be used with an assignment expression which is new to Python version 3.8.
The Infinite Loop
# Infinite loop
while True:
print("ctrl+c to stop")
# Infinite Loop w/ counter
count = 0
while True:
print(f"[{count}] ctrl+c to stop")
count += 1
The Walrus Operator really makes our code much more clean and succinct; however, there seems to be a caveat when desiring to count from zero. An assignment expression assigns an expression to an identifier, while also returning the value of the expression.
# β οΈ Doesn't run because `count := count + 1` evaluates to 0
count = -1
while count := count + 1:
print(f"[{count}] press ctrl+c to stop")
# β οΈ Starts at 1 instead of 0
count = 0
while count := count + 1:
print(f"[{count}] press ctrl+c to stop")
If the count starts at -1, without the comparison, it will not run because count := count + 1
evaluates to 0 or while 0
which is the same thing as while False
; however, if we use a comparison (count := count + 1) > -1
this is essentially 0 > -1
or while True
.
# Infinite loop w/ walrus operator
count = -1
while (count := count + 1) > -1:
print(f"[{count}] press ctrl+c to stop")
Breaking Out Of The Loop
We can break out of the loop using the break keyword.
# Counter
# output: 0 1 2 3 4 5 6 7 8 9 10
count = 0
while True:
print(f"{count}", end=" ")
count += 1
# exit loop at count 10
if count > 10:
break
# Countdown
# output: 10 9 8 7 6 5 4 3 2 1 0
count = 10
while True:
print(f"{count}", end=" ")
count -= 1
# exit loop at 0
if count < 0:
break
The same result can be accomplished utilizing the whileβs condition instead of the break statement.
# Identical to above except uses
# while condition to break loop
# output: 0 1 2 3 4 5 6 7 8 9 10
count = 0
while count <= 10:
print(f"{count}", end=" ")
count += 1
# Countdown using while
# condition to break loop
# output: 10 9 8 7 6 5 4 3 2 1 0
count = 10
while count >= 0:
print(f"{count}", end=" ")
count -= 1
Here we encounter the same problem as described in the infinite loop section when trying to start with zero. We are forced to start with 1.
# counter w/ walrus operator
# β οΈ starts at 1 instead of 0
# output: 1 2 3 4 5 6 7 8 9 10
count = 0
while count := count + 1:
print(f"{count}", end=" ")
# same as count > 9
if count == 10:
break
# countdown w/ walrus operator
# β οΈ stops at 1 instead of 0
# output: 10 9 8 7 6 5 4 3 2 1
count = 10+1
while count := count - 1:
print(f"{count}", end=" ")
# This loop will stop at 1
# this break isn't needed
The solution is β once again β to use a comparison operator.
# simplifying by breaking with condition
# output: 0 1 2 3 4 5 6 7 8 9 10
count = -1
while (count := count + 1) <= 10:
print(f"{count}", end=" ")
# simplifying by breaking with condition
# output: 10 9 8 7 6 5 4 3 2 1 0
count = 10+1
while (count := count - 1) > -1:
print(f"{count}", end=" ")
Skipping An Iteration With Continue
The continue keyword can be used to skip code or the entire iteration.
# skips printing on 3
# output: 1 2 [skipped] 4 5 6 7 8 9 10
count = 0
while count := count + 1:
if count == 3:
print("[skipped]", end=" ")
continue
print(f"{count}", end=" ")
if count == 10:
break
# simplifying by breaking with condition
# output: 1 2 [skipped] 4 5 6 7 8 9 10
count = 0
while (count := count + 1) <= 10:
if count == 3:
print("[skipped]", end=" ")
continue
print(f"{count}", end=" ")
# 9 8 7 6 5 4 [skipped] 2 1 0
count = 10
while (count := count - 1) >= 0:
if count == 3:
print("[skipped]", end=" ")
continue
print(f"{count}", end=" ")