Table of Contents
While Loops
while condition:
# statements to execute
else:
# statements to execute
We first check a condition; if the condition is true, then the while block gets executed until the condition becomes false.
If the condition is false, the else block gets executed. Remember that if we hit a break or return inside the while block, then the else block is never executed.
Python While Loop Example
a = 0
while a <= 10:
print("Condition is True", a)
# increment the variable
a +=1
else:
print("a is greater than 10 -- current value of a is ", a)
Output:
Condition is True 0
Condition is True 1
Condition is True 2
Condition is True 3
Condition is True 4
Condition is True 5
Condition is True 6
Condition is True 7
Condition is True 8
Condition is True 9
Condition is True 10
a is greater than 10 -- current value of a is 11
We can also use the boolean conditions to our advantage here.
Suppose a string is empty, it yields false when we check the expression, so the else block would execute in the below code.
a=''
while a:
print("a is not an empty string")
else:
print("a is an empty string")
Output:
a is an empty string
We can use the same above check in empty lists, tuples or dictionaries. It will yield false, and the else statement would be executed.
For Loop in Python
We use for loop when we want to iterate through an iterable and execute a block of code until the iterables last member is iterated over.
An iterable is a python object capable of returning its members one at a time. Some examples are tuples, lists, strings, generators etc
Like while loop for loop also has else block, typically not used much but in for loop else executes after for loop completes normally, if a break or return statement is present, then else won’t work.
We will see an example where we check if any even number is present in a list, if not we will print a statement.
Python For Loop Example:
list_numbers= [33,11,15,67,89]
for i in list_numbers:
if i%2==0:
print(i)
else:
print("Could not find any even numbers")
set_even_flag = 0
Output:
Could not find any even numbers
Python DO WHILE
do {
# Execute the while block
} while condition
In Do while statements, code in the while loop executes first and then checks for the condition at the bottom, typical in all other programming languages that support this.
Python does not have Do While statements. But we can still replicate the same functionality with while and if statements, where we being with a truth condition and then execute the loop until a false condition is met and breaks out of the loop using a break statement.
Python DO WHILE Code Example:
i = 0
while True:
print("Execute while loop statements first, value of i--", i)
i = i + 1
if(i > 5):
break
Output:
Execute while loop statements first, value of i-- 0
Execute while loop statements first, value of i-- 1
Execute while loop statements first, value of i-- 2
Execute while loop statements first, value of i-- 3
Execute while loop statements first, value of i-- 4
Execute while loop statements first, value of i-- 5