12. Loops
Iteration with while loops
while loopsAnother powerful feature of programming languages, closely related to branching, is running one or more statements multiple times. This feature is often referred to as iteration on looping, and there are two ways to do this in Python: using while loops and for loops.
while loops have the following syntax:
while condition:
statement(s)Statements in the code block under while are executed repeatedly as long as the condition evaluates to True. Generally, one of the statements under while makes some change to a variable that causes the condition to evaluate to False after a certain number of iterations.
Let's try to calculate the factorial of 100 using a while loop. The factorial of a number n is the product (multiplication) of all the numbers from 1 to n, i.e., 1*2*3*...*(n-2)*(n-1)*n.
In [38]:
result = 1
i = 1
while i <= 100:
result = result * i
i = i+1
print(f'The factorial of 100 is: {result}')The factorial of 100 is: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000Here's how the above code works:
We initialize two variables,
resultand,i.resultwill contain the final outcome. Andiis used to keep track of the next number to be multiplied withresult. Both are initialized to 1 (can you explain why?)The condition
i <= 100holds true (sinceiis initially1), so thewhileblock is executed.The
resultis updated toresult * i,iis increased by1and it now has the value2.At this point, the condition
i <= 100is evaluated again. Since it continues to hold true,resultis again updated toresult * i, andiis increased to3.This process is repeated till the condition becomes false, which happens when
iholds the value101. Once the condition evaluates toFalse, the execution of the loop ends, and theprintstatement below it is executed.
Can you see why result contains the value of the factorial of 100 at the end? If not, try adding print statements inside the while block to print result and i in each iteration.
Iteration is a powerful technique because it gives computers a massive advantage over human beings in performing thousands or even millions of repetitive operations really fast. With just 4-5 lines of code, we were able to multiply 100 numbers almost instantly. The same code can be used to multiply a thousand numbers (just change the condition to
i <= 1000) in a few seconds.
You can check how long a cell takes to execute by adding the magic command %%time at the top of a cell. Try checking how long it takes to compute the factorial of 100, 1000, 10000, 100000, etc.
Here's another example that uses two while loops to create an interesting pattern.
Infinite Loops
Suppose the condition in a while loop always holds true. In that case, Python repeatedly executes the code within the loop forever, and the execution of the code never completes. This situation is called an infinite loop. It generally indicates that you've made a mistake in your code. For example, you may have provided the wrong condition or forgotten to update a variable within the loop, eventually falsifying the condition.
If your code is stuck in an infinite loop during execution, just press the "Stop" button on the toolbar (next to "Run") or select "Kernel > Interrupt" from the menu bar. This will interrupt the execution of the code. The following two cells both lead to infinite loops and need to be interrupted.
# INFINITE LOOP - INTERRUPT THIS CELL
result = 1
i = 1
while i <= 100:
result = result * i
# forgot to increment i---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-41-5234d8c241fc> in <module>
5
6 while i <= 100:
----> 7 result = result * i
8 # forgot to increment i
KeyboardInterrupt: In [42]:
# INFINITE LOOP - INTERRUPT THIS CELL
result = 1
i = 1
while i > 0 : # wrong condition
result *= i
i += 1---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-42-c4abf72fce4d> in <module>
5
6 while i > 0 : # wrong condition
----> 7 result *= i
8 i += 1
KeyboardInterrupt: break and continue statements
You can use the break statement within the loop's body to immediately stop the execution and break out of the loop (even if the condition provided to while still holds true).
In [43]:
i = 1
result = 1
while i <= 100:
result *= i
if i == 42:
print('Magic number 42 reached! Stopping execution..')
break
i += 1
print('i:', i)
print('result:', result)Magic number 42 reached! Stopping execution..
i: 42
result: 1405006117752879898543142606244511569936384000000000As you can see above, the value of i at the end of execution is 42. This example also shows how you can use an if statement within a while loop.
Sometimes you may not want to end the loop entirely, but simply skip the remaining statements in the loop and continue to the next loop. You can do this using the continue statement.
i = 1
result = 1
while i < 20:
i += 1
if i % 2 == 0:
print(f'Skipping {i}')
continue
print(f'Multiplying with {i}')
result = result * i
print('i:', i)
print('result:', result)Skipping 2
Multiplying with 3
Skipping 4
Multiplying with 5
Skipping 6
Multiplying with 7
Skipping 8
Multiplying with 9
Skipping 10
Multiplying with 11
Skipping 12
Multiplying with 13
Skipping 14
Multiplying with 15
Skipping 16
Multiplying with 17
Skipping 18
Multiplying with 19
Skipping 20
i: 20
result: 654729075In the example above, the statement result = result * i inside the loop is skipped when i is even, as indicated by the messages printed during execution.
Logging: The process of adding
Iteration with for loops
for loopsA for loop is used for iterating or looping over sequences, i.e., lists, tuples, dictionaries, strings, and ranges. For loops have the following syntax:
for value in sequence:
statement(s)The statements within the loop are executed once for each element in sequence. Here's an example that prints all the element of a list.
In [46]:
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
for day in days:
print(day)Monday
Tuesday
Wednesday
Thursday
FridayLet's try using for loops with some other data types.
# Looping over a string
for char in 'Monday':
print(char)M
o
n
d
a
yIn [48]:
# Looping over a tuple
for fruit in ['Apple', 'Banana', 'Guava']:
print("Here's a fruit:", fruit)Here's a fruit: Apple
Here's a fruit: Banana
Here's a fruit: GuavaIn [49]:
# Looping over a dictionary
person = {
'name': 'John Doe',
'sex': 'Male',
'age': 32,
'married': True
}
for key in person:
print("Key:", key, ",", "Value:", person[key])Key: name , Value: John Doe
Key: sex , Value: Male
Key: age , Value: 32
Key: married , Value: TrueNote that while using a dictionary with a for loop, the iteration happens over the dictionary's keys. The key can be used within the loop to access the value. You can also iterate directly over the values using the .values method or over key-value pairs using the .items method.
In [50]:
for value in person.values():
print(value)John Doe
Male
32
TrueIn [51]:
for key_value_pair in person.items():
print(key_value_pair)('name', 'John Doe')
('sex', 'Male')
('age', 32)
('married', True)Since a key-value pair is a tuple, we can also extract the key & value into separate variables.
for key, value in person.items():
print("Key:", key, ",", "Value:", value)Key: name , Value: John Doe
Key: sex , Value: Male
Key: age , Value: 32
Key: married , Value: TrueIterating using range and enumerate
The range function is used to create a sequence of numbers that can be iterated over using a for loop. It can be used in 3 ways:
range(n)- Creates a sequence of numbers from0ton-1range(a, b)- Creates a sequence of numbers fromatob-1range(a, b, step)- Creates a sequence of numbers fromatob-1with increments ofstep
Let's try it out.
In [53]:
for i in range(7):
print(i)0
1
2
3
4
5
6In [54]:
for i in range(3, 10):
print(i)3
4
5
6
7
8
9In [55]:
for i in range(3, 14, 4):
print(i)3
7
11break, continue and pass statements
Similar to while loops, for loops also support the break and continue statements. break is used for breaking out of the loop and continue is used for skipping ahead to the next iteration.
In [58]:
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']In [59]:
for day in weekdays:
print('Today is {}'.format(day))
if (day == 'Wednesday'):
print("I don't work beyond Wednesday!")
breakToday is Monday
Today is Tuesday
Today is Wednesday
I don't work beyond Wednesday!In [60]:
for day in weekdays:
if (day == 'Wednesday'):
print("I don't work on Wednesday!")
continue
print('Today is {}'.format(day))Today is Monday
Today is Tuesday
I don't work on Wednesday!
Today is Thursday
Today is FridayLike if statements, for loops cannot be empty, so you can use a pass statement if you don't want to execute any statements inside the loop.
In [61]:
for day in weekdays:
passNested for and while loops
Similar to conditional statements, loops can be nested inside other loops. This is useful for looping lists of lists, dictionaries etc.
In [62]:
persons = [{'name': 'John', 'sex': 'Male'}, {'name': 'Jane', 'sex': 'Female'}]
for person in persons:
for key in person:
print(key, ":", person[key])
print(" ")name : John
sex : Male
name : Jane
sex : Femaledays = ['Monday', 'Tuesday', 'Wednesday']
fruits = ['apple', 'banana', 'guava']
for day in days:
for fruit in fruits:
print(day, fruit)Monday apple
Monday banana
Monday guava
Tuesday apple
Tuesday banana
Tuesday guava
Wednesday apple
Wednesday banana
Wednesday guavaWith this, we conclude our discussion of branching and loops in Python.
Questions for Revision
Try answering the following questions to test your understanding of the topics covered in this notebook:
What is iteration or looping in programming languages? Why is it useful?
What are the two ways for performing iteration in Python?
What is the purpose of the
whilestatement in Python?What is the syntax of the
whitestatement in Python? Give an example.Write a program to compute the sum of the numbers 1 to 100 using a while loop.
Repeat the above program for numbers up to 1000, 10000, and 100000. How long does it take each loop to complete?
What is an infinite loop?
What causes a program to enter an infinite loop?
How do you interrupt an infinite loop within Jupyter?
What is the purpose of the
breakstatement in Python?Give an example of using a
breakstatement within a while loop.What is the purpose of the
continuestatement in Python?Give an example of using the
continuestatement within a while loop.What is logging? How is it useful?
What is the purpose of the
forstatement in Python?What is the syntax of
forloops? Give an example.How are for loops and while loops different?
How do you loop over a string? Give an example.
How do you loop over a list? Give an example.
How do you loop over a tuple? Give an example.
How do you loop over a dictionary? Give an example.
What is the purpose of the
rangestatement? Give an example.What is the purpose of the
enumeratestatement? Give an example.How are the
break,continue, andpassstatements used in for loops? Give examples.Can loops be nested within other loops? How is nesting useful?
Give an example of a for loop nested within another for loop.
Give an example of a while loop nested within another while loop.
Give an example of a for loop nested within a while loop.
Give an example of a while loop nested within a for loop.
Last updated