Python for loop

In this tutorial, we will learn about the python loop. which is used to iterable or iterator to our python code

Python for loop

What is a for loop in Python?

A for loops in python is used to iterate over a sequence, a list, a tuple, a dictionary, a set, or a string. With for loop, we can execute a particular set of statements one at each time.

Iterating over a sequence is called traversal.

python for loop syntax

for variable in sequence:
Statement of for

Here, we are taking a variable with a value as an item inside the sequence on each iteration. Loop will continue until we reach the last item in the series.

For loop

Let’s take an example of a simple for loops python.

# Program to print all the items in a list
# List of Items
Items = [1,‘Two’, 3.1,“FOUR”,5]
# iterate over the list
for i in Items: # Assigning i as an variable
print(i) # Printing a variable

The output of for loop

1
Two
3.1
FOUR
5

We can also use a for loop with String using the following method.

Example of for loop using string.

# Program to print letters in a string using for loop.
text = “Python”
for i in text:
print(i)

The output of the above program.

P
y
t
h
o
n

Example of the addition of the list using for loop

# Program to find the sum of all numbers stored in a list
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
print(“The sum is”, sum)

Output:

The sum is 48

When we run this program, the loop will take each item in the list in the val variable and then make an addition with the variable sum until the sequence is completed.

Nested for loop in python

We can also add multiple loops inside a loop.

Syntax of nested for loop

for variable1 in sequence:
for variable1 in sequence:
Statement of for
Nested for loop

Let see an example of a nested loop.

adj = [“red”, “big”, “tasty”]
fruits = [“apple”, “banana”, “cherry”]
for x in adj:
for y in fruits:
print(x, y)

The output of nested for loops python.

red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry

for loop with else statement in python

We can also use an else statement in for loop just like if conditional statement. The else part is executed if the items in the sequence used in for loop exhausts.

Syntax of for loop with else

for variable in sequence:
Statement of for
else:
Statement of else
for loop with else statement

Example of for loop with else statement

# Program will print the statement inside else statement whenever the for will be exited.
# List of numbers
numbers = [1,4,6,0,1]
for i in numbers: #calling for loop by using i as an variable
print(“printing the number:”, i) # printing i variable
else:
print(“No items left, so else statement is executed”)

When we run the program, we get the following output:

printing the number: 1
printing the number: 4
printing the number: 6
printing the number : 0
printing the number: 1
No items left, so else statement is executed

As you can see, the program prints all the items in the list, and once the last item is printed and for loop has no sequence to execute, it will exit from the loop, and the else statements we executed.