How to Use For Loops in Python
for
loops in Python are used to iterate over a numerical range or any other iterable data type in a sequence. They are useful for repetitive operations and tasks that need to be looped a specific number of times.
In this tutorial, we will learn how to use for
loops in Python and explore some of the different ways we might use them.
The for Loop Syntax
for
loops use two variables, the iteration variable to be used inside the function and secondly the input value(s). The input can be any iterable data type; a string
, list
, tuple
or range
. A :
(colon) must be used to begin the function and all code inside the for
loop must be intended with a
(tab).
for iteration_variable in input_variable:
#do something
note - you can call the iteration_variable
anything you wish. For more readable code you could use a single-character variable, x
for example.
Loop Through a List
A common use of for
loops is to iterate through a Python list
. Let's define a list
and then loop through it and print
each element in the list
.
letters = ['a', 'b', 'c']
for l in letters:
print(l)
a
b
c
Loop Through a String
Python strings are iterable, therefore we can access each character of a string using a for
loop. In the example below, we are passing the string words
into the for
loop and printing each of its characters.
for c in "words":
print(c)
w
o
r
d
s
note - in the example, the iteration variable is c
- the only reason for that is it makes more sense since it contains a character.
Loop Through a Number Range
One way to loop through a range of numbers would be to create a list of numbers and iterate over them with a for
loop.
numbers = ['1', '2', '3', '4', '5']
However, what if you needed to loop 100 times? Creating a list of 100 numbers would be quite messy. Fortunately, Python has a function called range
to deal with this exact problem. It creates a temporary list of numbers between a specified range.
The first argument of range
is the starting number and second is the ending number in the range.
numbers = range(1, 5)
for n in numbers:
print(n)
1
2
3
4
Count Numbers in a List Using a for Loop
Let's say we had a list
of numbers and wanted to find out their total when they are added together. We could do this by setting a total
variable to 0
and then setting the total
variable to the current total
value plus the current iteration value inside a for
loop.
numbers = [25, 56, 44]
total = 0
for n in numbers:
total += n
print(total)
125
Break a For Loop
To stop a for
loop and continue running the rest of the program use break
. This is usually used in conjunction with an if
comparison. In the example below, we are iterating through the string food
and if the current character is equal to d
end the loop.
string = "food"
new = ""
for c in string:
if c == "d":
break
new = new + c
print(new)
foo
Continue to the Next Iteration of a Loop
The continue statement will tell Python to skip the rest of the current iteration but continue looping.
string = "food"
new = ""
for c in string:
if c == "o":
continue
new = new + c
print(new)
Else Clause
An else clause can be placed at the end of a for
loop to catch the completion of a loop that returns false
.
numbers = [1, 2, 3]
for n in numbers:
if n == 4:
break
else:
print("false")
in the example above if the number 4
is found the loop will break. 4
is not in the numbers
list
so the loop will complete and the else
statement will print false
.
Nested For Loops
Nesting for
loops in Python can be useful when iterating through multidimensional lists.
numbers = [1, 2, 3, [11, 22, 33]]
for n in numbers:
if type(n) is list:
for m in n:
print(m)
else:
print(n)
1
2
3
11
22
33
Conclusion
You now know how to use for loops in Python and how they can be applied to completing a variety of tasks.