20. For Loops
By Bernd Klein. Last modified: 08 Nov 2023.
Introduction
A for loop in Python is used for iterating over a sequence (such as a list, tuple, string, or range) or other iterable objects. It's a fundamental control structure in programming that allows you to repeat a block of code a specific number of times or iterate through the elements of a sequence.
So what about the while loop? Do we need another kind of a loop in Python? Can we not do everything with the while loop? Yes, we can rewrite 'for' loops as 'while' loops. But before we go on, you want to see at least one example of a for loop.
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
Syntax of the For Loop
As we mentioned earlier, the Python for loop is an iterator based for loop. It steps through the items of lists, tuples, strings, the keys of dictionaries and other iterables. The Python for loop starts with the keyword "for" followed by an arbitrary variable name, which will hold the values of the following sequence object, which is stepped through. The general syntax looks like this:
for <variable> in <sequence>:
<statements>
else:
<statements>
A first example of a for loop:
performance_levels = ('beginner', 'novice', 'intermediate', 'advanced', 'expert')
# Iterate through each level in the tuple
for level in performance_levels:
# Print the current level
print(level)
OUTPUT:
beginner novice intermediate advanced expert
The for loop is used to go through each element in the performance_levels
tuple one by one. In each iteration, the loop variable level
will take on the value of the current element in the tuple.
Inside the loop, the print()
function is used to display the current value of the level variable. This means that it will print each performance level to the screen, starting with "beginner," then "novice," and so on, until it reaches "expert."
The loop will continue to iterate until it has gone through all the elements in the performance_levels tuple.
To summarize:
The items of the sequence object are assigned one after the other to the loop variable; to be precise the variable points to the items. For each item the loop body is executed.
We mentioned before that we can rewrite a for loop as a while statement. In this case it looks like this:
performance_levels = ('beginner', 'novice', 'intermediate', 'advanced', 'expert')
# Initialize an index to 0
index = 0
# Use a while loop to iterate through each level in the tuple
while index < len(performance_levels):
# Get the current level from the tuple
level = performance_levels[index]
# Print the current level
print(level)
# Increment the index to move to the next level
index += 1
We can easily see that the for loop is more elegant and less error prone in this case.
Different Kinds of for Loops
If you are beginner in programming, you can or maybe you should even skip this subchapter, in which we will talk about different ways implementations of for loops in other programming languages.
There are hardly any programming languages without for loops, but the for loop exists in many different flavours, i.e. both the syntax and the semantics differs from one programming language to another.
Different kinds of for loops:

Countcontrolled for loop (Threeexpression for loop)
This is by far the most common type. This statement is the one used by C. The header of this kind of for loop consists of a threeparameter loop control expression. Generally it has the form: for (A; Z; I) A is the initialisation part, Z determines a termination expression and I is the counting expression, where the loop variable is incremented or dcremented. An example of this kind of loop is the forloop of the programming language C: for (i=0; i <= n; i++) This kind of for loop is not implemented in Python!

Numeric Ranges
This kind of for loop is a simplification of the previous kind. It's a counting or enumerating loop. Starting with a start value and counting up to an end value, like for i = 1 to 100 Python doesn't use this either.

Vectorized for loops
They behave as if all iterations are executed in parallel. That is, for example, all expressions on the right side of assignment statements get evaluated before the assignments.

Iteratorbased for loop
Finally, we come to the one used by Python. This kind of for loop iterates over an enumeration of a set of items. It is usually characterized by the use of an implicit or explicit iterator. In each iteration step a loop variable is set to a value in a sequence or other data collection. This kind of for loop is known in most Unix and Linux shells and it is the one which is implemented in Python.
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
Upcoming online Courses
More about the Python for Loop
Another Example of a simple for loop in Python. We will also use this example in the following:
languages = ["C", "C++", "Perl", "Python"]
for language in languages:
print(language)
OUTPUT:
C C++ Perl Python
The else block is special; while Perl programmer are familiar with it, it's an unknown concept to C and C++ programmers. Semantically, it works exactly as the optional else of a while loop. It will be executed only if the loop hasn't been "broken" by a break statement. So it will only be executed, after all the items of the sequence in the header have been used.
If a break statement has to be executed in the program flow of the for loop, the loop will be exited and the program flow will continue with the first statement following the for loop, if there is any at all. Usually break statements are wrapped into conditional statements, e.g.
edibles = ["bacon", "spam", "eggs", "nuts"]
for food in edibles:
if food == "spam":
print("No more spam please!")
break
print("Great, delicious " + food)
else:
print("I am so glad: No spam!")
print("Finally, I finished stuffing myself")
OUTPUT:
Great, delicious bacon No more spam please! Finally, I finished stuffing myself
Removing "spam" from our list of edibles, we will gain the following output:
$ python for.py Great, delicious bacon Great, delicious eggs Great, delicious nuts I am so glad: No spam! Finally, I finished stuffing myself $
Maybe, our disgust with spam is not so high that we want to stop consuming the other food. Now, this calls the continue statement into play . In the following little script, we use the continue statement to go on with our list of edibles, when we have encountered a spam item. So continue prevents us from eating spam!
edibles = ["bacon", "spam", "eggs","nuts"]
for food in edibles:
if food == "spam":
print("No more spam please!")
continue
print("Great, delicious " + food)
print("Finally, I finished stuffing myself")
OUTPUT:
Great, delicious bacon No more spam please! Great, delicious eggs Great, delicious nuts Finally, I finished stuffing myself
The range() Function
The builtin function range() is the right function to iterate over a sequence of numbers. It generates an iterator of arithmetic progressions: Example:
range(5)
OUTPUT:
range(0, 5)
This result is not selfexplanatory. It is an object which is capable of producing the numbers from 0 to 4. We can use it in a for loop and you will see what is meant by this:
for i in range(5):
print(i)
OUTPUT:
0 1 2 3 4
range(n) generates an iterator to progress the integer numbers starting with 0 and ending with (n 1). To produce the list with these numbers, we have to cast range() with the list(), as we do in the following example.
list(range(10))
OUTPUT:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range() can also be called with two arguments:
range(begin, end)
The above call produces the list iterator of numbers starting with begin
(inclusive) and ending with one less than the number end
.
Example:
range(4, 10)
OUTPUT:
range(4, 10)
list(range(4, 10))
OUTPUT:
[4, 5, 6, 7, 8, 9]
So far the increment of range() has been 1. We can specify a different increment with a third argument. The increment is called the step
. It can be both negative and positive, but not zero:
range(begin,end, step)
Example with step:
list(range(4, 50, 5))
OUTPUT:
[4, 9, 14, 19, 24, 29, 34, 39, 44, 49]
It can be done backwards as well:
list(range(42, 12, 7))
OUTPUT:
[42, 35, 28, 21, 14, 7, 0, 7]
The range() function is especially useful in combination with the for loop, as we can see in the following example. The range() function supplies the numbers from 1 to 100 for the for loop to calculate the sum of these numbers:
n = 100
sum = 0
for counter in range(1, n+1):
sum = sum + counter
print("Sum of 1 until %d: %d" % (n, sum))
OUTPUT:
Sum of 1 until 100: 5050
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
Calculation of the Pythagorean Numbers
Generally, it is assumed that the Pythagorean theorem was discovered by Pythagoras that is why it has his name. However, there is a debate whether the Pythagorean theorem might have been discovered earlier or by others independently. For the Pythagoreans,  a mystical movement, based on mathematics, religion and philosophy,  the integer numbers satisfying the theorem were special numbers, which had been sacred to them.
These days Pythagorean numbers are not mystical anymore. Though to some pupils at school or other people, who are not on good terms with mathematics, they may still appear so.
So the definition is very simple: Three integers satisfying a2+b2=c2 are called Pythagorean numbers.
The following program calculates all pythagorean numbers less than a maximal number. Remark: We have to import the math module to be able to calculate the square root of a number.
from math import sqrt
n = int(input("Maximal Number? "))
for a in range(1, n+1):
for b in range(a, n):
c_square = a**2 + b**2
c = int(sqrt(c_square))
if ((c_square  c**2) == 0):
print(a, b, c)
OUTPUT:
3 4 5 5 12 13 6 8 10 7 24 25 8 15 17 9 12 15 10 24 26 12 16 20 15 20 25 18 24 30 20 21 29 21 28 35
Iterating over Lists with range()
If you have to access the indices of a list, it doesn't seem to be a good idea to use the for loop to iterate over the lists. We can access all the elements, but the index of an element is not available. However, there is a way to access both the index of an element and the element itself. The solution lies in using range() in combination with the length function len():
fibonacci = [0, 1, 1, 2, 3, 5, 8, 13, 21]
for i in range(len(fibonacci)):
print(i,fibonacci[i])
print()
OUTPUT:
0 0 1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21
Remark: If you apply len() to a list or a tuple, you get the number of elements of this sequence.
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
List iteration with Side Effects
If you loop over a list, it's best to avoid changing the list in the loop body. Take a look at the following example:
colours = ["red"]
for i in colours:
if i == "red":
colours += ["black"]
if i == "black":
colours += ["white"]
print(colours)
OUTPUT:
['red', 'black', 'white']
To avoid these side effects, it's best to work on a copy by using the slicing operator, as can be seen in the next example:
colours = ["red"]
for i in colours[:]:
if i == "red":
colours += ["black"]
if i == "black":
colours += ["white"]
print(colours)
OUTPUT:
['red', 'black']
We still might have done something, we shouldn't have done. We changed the list "colours", but our change didn't have any effect on the loop. The elements to be looped remained the same during the iterations.
Exercises with for Loops
Exercise 1: Fibonacci Numbers
Generate and print the first n
numbers in the Fibonacci sequence using a for loop.
Exercise 2: Diamond Pattern
Create a program that prints a diamond pattern using asterisks (*) like the example below:
*
***
*****
*******
*********
*******
*****
***
*
Exercise 3: Prime Numbers
Write a program to find and print all prime numbers between 1 and 50 using a for loop.
Exercise 4: Fizz, Buzz, FizzBuzz
Print numbers from 1 to 42, but for multiples of 3, print "Fizz," and for multiples of 5, print "Buzz." For numbers that are multiples of both 3 and 5, print "FizzBuzz."
Exercise 5: RamanujanHardy number
This exercise is about the RamanujanHardy number. There is a little anecdote of the Mathematician G.H. Hardy when he visited Indian mathematician Srinivasa Ramanujan in hospital. It goes like this:
I remember once going to see him when he was ill at Putney. I had ridden in taxi cab number 1729 and remarked that the number seemed to me rather a dull one, and that I hoped it was not an unfavourable omen. "No," he replied, "it is a very interesting number; it is the smallest number expressible as the sum of two cubes in two different ways."
For this reason 1732 is known as the RamanujanHardy number.
Can you verify this with a Python program?
Exercise 6:
1729 is the lowest number which can be represented by a Loeschian quadratic form $a^2 + ab + b^2$ in four different ways, with positive integers a and b .
Exercise 7: Pascal's Trianlge
Write a Python program to generate and print the first n rows of Pascal's Triangle. Pascal's Triangle is a mathematical pattern of numbers where each number is the sum of the two numbers directly above it. The first few rows of Pascal's Triangle look like this:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Your program should take an integer n as input and print the first n rows of Pascal's Triangle. You'll need to use nested for loops to calculate and print each row.
This exercise can be quite challenging, but it's a great way to practice nested loops and pattern generation in Python.
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
Solutions
Solution to Exercise 1
# Get the value of n from the user
n = int(input("Enter the number of Fibonacci numbers to generate: "))
# Initialize the first two Fibonacci numbers
fibonacci_sequence = [0, 1]
# Generate and print the Fibonacci sequence using a for loop
for i in range(2, n):
next_number = fibonacci_sequence[1] + fibonacci_sequence[2]
fibonacci_sequence.append(next_number)
# Print the first n Fibonacci numbers
print("Fibonacci Sequence:")
for number in fibonacci_sequence[:n]:
print(number, end=" ")
OUTPUT:
Fibonacci Sequence: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Solution to Exercise 2
# Get the height of the diamond from the user
height = int(input("Enter the height of the diamond: "))
height_is_even = height % 2 == 0
middle = height // 2 if height_is_even else height // 2 + 1
# Upper half of the diamond
spaces = "*"
for counter in range(0, middle):
print(spaces.center(middle*2+1))
spaces += "**"
# second half of diamond:
spaces = spaces[:2]
if height_is_even:
# previous line has to be duplicated:
print(spaces.center(middle*2+1))
while len(spaces) > 1:
spaces = spaces[:2]
print(spaces.center(middle*2+1))
OUTPUT:
* *** ***** ******* ********* ********* ******* ***** *** *
5 % 2
OUTPUT:
1
Solution to Exercise 3
print("Prime numbers between 1 and 50:")
for number in range(1, 51):
if number <= 1:
is_prime = False
elif number <= 3:
is_prime = True
elif number % 2 == 0 or number % 3 == 0:
is_prime = False
else:
is_prime = True
i = 5
while i * i <= number:
if number % i == 0 or number % (i + 2) == 0:
is_prime = False
break
i += 6
if is_prime:
print(number, end=" ")
OUTPUT:
Prime numbers between 1 and 50: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Solution to Exercise 4
for number in range(1, 43):
# Check if the number is a multiple of both 3 and 5
if number % 3 == 0 and number % 5 == 0:
print("FizzBuzz")
# Check if the number is a multiple of 3
elif number % 3 == 0:
print("Fizz")
# Check if the number is a multiple of 5
elif number % 5 == 0:
print("Buzz")
# If none of the above conditions are met, simply print the number
else:
print(number)
OUTPUT:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz
Solution to Exercise 5
import math
number = 1729
n = int(number ** (1/3))
cubes = {}
for i in range(n+1):
for j in range(i):
result = i ** 3 + j ** 3
if result in cubes:
cubes[result].append((i, j))
else:
cubes[result] = [(i,j)]
if result > number:
break
for x in cubes:
if len(cubes[x]) > 1:
print(x, cubes[x])
OUTPUT:
1729 [(10, 9), (12, 1)]
Solution to Exercise 6
import math
number = 1729
n = int(number ** (1/2))
results = {}
for a in range(n+1):
for b in range(a):
result = a**2 + a*b + b**2
if result in results:
results[result].append((a, b))
else:
results[result] = [(a,b)]
if result > number:
break
for x in results:
if len(results[x]) > 3:
print(x, results[x])
OUTPUT:
1729 [(25, 23), (32, 15), (37, 8), (40, 3)]
Solution to Exercise 7
n = int(input("Enter the number of rows for Pascal's Triangle: "))
# Initialize Pascal's Triangle as a list of lists
triangle = []
# Generate Pascal's Triangle
for i in range(n):
# Create a new row
row = []
for j in range(i + 1):
if j == 0 or j == i:
# The first and last elements in each row are 1
row.append(1)
else:
# Calculate other elements as the sum of two above
previous_row = triangle[i  1]
row.append(previous_row[j  1] + previous_row[j])
triangle.append(row)
# Print Pascal's Triangle
for row in triangle:
print(" ".join(map(str, row)).center(n * 3))
OUTPUT:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
Upcoming online Courses