Python: For Loop – Explained with examples

In this article, we will learn the syntax and usage details of for loop in python. Then we will look at various examples of the for loop.

In python, we can iterate over a group of statements multiple times using a for loop. But the number of times these statements will be executed by a for loop depends upon a sequence.

In other words, for loop in python is useful to iterate over a sequence of elements.

Let’s have a look at the syntax of for loop in python,

for elem in sequence:
    statement 1
    statement 2
    .......
    .......
    statement n

“for” keyword is followed by a variable, then the “in” keyword, then a sequence, and in last, a colon. After the for statement starts the suite of for loop, i.e., a group of statements with one indent level, it is also called the block of for loop.

For loop will iterate over all the elements in a given sequence. For each element of the sequence, it will assign that element to variable elem and then execute the statements in-suite, i.e., statements in the block. Now, these statements can use the elem variable that contains an element of the sequence for that occurrence. Let’s understand with some examples,

Examples of for loop in python

Iterate over the characters of string using for loop

# Iterate over the characters of string using for loop
for elem in 'Sample':
    print('***')
    print('Character: ', elem)

Output:

***
Character:  S
***
Character:  a
***
Character:  m
***
Character:  p
***
Character:  l
***
Character:  e

We used a string as a sequence. Then using a for loop we iterated over all the characters in the string sequence.

Two statements in the block of for loop executed six times, because there were six characters in the string sequence. For each character in the sequence, for loop performed the following actions,

  • It assigned the value of that character to variable elem.
  • It executed the statements in the suite.

Use for loop to print numbers from 1 to 10 using range() function.

Function range() returns a sequence of numbers from start to end -1. Then using the for loop, we can iterate over each element of this sequence i.e.

# Use for loop to print numbers from
# 1 to 10 using range() function
for i in range(1, 11):
    print('Number: ', i)

Output:

Number:  1
Number:  2
Number:  3
Number:  4
Number:  5
Number:  6
Number:  7
Number:  8
Number:  9
Number:  10

For each element of the sequence, for loop performed the following actions,

  • It assigned the element to the variable “i”
  • It executed the lines in the block, i.e., suite of for loop.

Use for loop to display numbers in descending order

Function range() returns a sequence of numbers from start to end -1 with the given step size. For example,

range(10, 0, -1)

Will return a sequence in decreasing order i.e.

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Then using the for loop, we will iterate over each element of this sequence i.e.

# Use for loop to display
# numbers in descending order
for i in range(10, 0, -1):
    print('Number: ', i)

Output:

Number:  10
Number:  9
Number:  8
Number:  7
Number:  6
Number:  5
Number:  4
Number:  3
Number:  2
Number:  1

For each element of the sequence, for loop performed the following actions,

  • It assigned the element to the variable “i”
  • It executed the lines in the block, i.e., suite of for loop.

Use for loop to print elements in a list

We can iterate over all the items in a list easily using for loop,

list_of_numbers = [1, 2, 3, 4, 5]

# iterate over all items in list
# and print them one by one
for elem in list_of_numbers:
    print(elem)

Output:

1
2
3
4
5

For loop with else block

Along with a for loop, we can have an optional else block too i.e.

for elem in sequence:
    statement 1
    statement 2
    statement 3
else:
    statement 5
    statement 6

Once the for loop finishes executing the statements in the for block, then in last, it runs the statements in the else block. An important point to remember is that statements in the else block will execute only in last and that too only for one time.
Example of for loop with else block,

for elem in 'Sample':
    print('Character: ', elem)
else:
    print('<<<<')
    print('End of Loop')
    print('>>>>')

Output:

Character:  S
Character:  a
Character:  m
Character:  p
Character:  l
Character:  e
<<<<
End of Loop
>>>>

Conclusion:

We can use for loops in python to iterate over the elements of a sequence. In each iteration, we can execute certain statements.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top