Theory.
The while loop in Python is like a repetitive task that continues as long as a certain condition remains True.
It’s similar to driving on a road until you reach a specific destination. Imagine you are on a road trip with your family and driving a car. Your wife wants have a conversation with you and your kids are constantly asking whether we already reached the hotel, and also don’t forget to periodically check the fuel level, otherwise you risk to delay your trip.
Here is the general concept of the While Loop visualized: The flow diagram describing the While loop in Python
Example #1
Let’s go to more practical side of learning the While Loop and write some simple Python code:
1
2
3
4
5
6
7
num = 0
while num < 5:
print('current num is: ', num)
num = num + 1
print('final num is: ', num)
In the above code:
- We created a variable
num
with initial value0
- In the While loop, using the comparison operator
<
we check whether the value of this variable is less than5
- If this expression results in True:
- print the current number
- increase it by 1
- go back to the line 3 and check the condition again.
Here is the source code from the first example visualized: The flow diagram describing the While loop in Python - Example #1
And here is the result
1
2
3
4
5
6
current num is: 0
current num is: 1
current num is: 2
current num is: 3
current num is: 4
final num is: 5
Example #2
The below part might be confusing, if the explanation is not clear, please leave your question in the comments below.
Technically, the while loop condition can just have the True object and NO condition check, however in this case you have to make sure that your code can break from the loop when a certain condition happens.
Always test that the break condition can happen, otherwise your program will stuck in endless execution
Let’s now write the source code:
1
2
3
4
5
6
7
8
9
num = 0
while True:
num = num + 1
if num == 5:
break
print('current num is: ', num)
print('final num is: ', num)
In the above code:
- We created a variable
num
with initial value0
- In the While Loop initial statement, the condition check is replaced by the True object
- Since this expression is True itself:
- increase current num value by 1
- check if the current num equals to 5:
- if that’s the case, exit from the loop immediately and don’t go back to the line #3 anymore.
- if not, continue executing the code which is still left inside the While Loop, and then go back to the beginning of the While Loop again.
- print the current number
Here is the source code from the second example visualized: The flow diagram describing the While loop in Python - Example #2
Practice.
- You’ll modify the previously created code to use the While loop now
- You want the calculator to always work and ask a user to perform calculations
- You don’t need an explicit exit condition
- The loop is going to end when the program is closed
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def add(number_1, number_2):
result = number_1 + number_2
print(result)
def subtract(number_1, number_2):
result = number_1 - number_2
print(result)
def multiply(number_1, number_2):
result = number_1 * number_2
print(result)
def divide(number_1, number_2):
result = number_1 / number_2
print(result)
while True:
number_1 = int(input('enter first number: '))
operator = input('enter math operator: +, -, *, / : ')
number_2 = int(input('enter second number: '))
if operator == '+':
add(number_1, number_2)
elif operator == '-':
subtract(number_1, number_2)
elif operator == '*':
multiply(number_1, number_2)
elif operator == '/':
divide(number_1, number_2)
else:
print('Unknown operator')