Python For Loop with Examples

In this Python tutorial, we will check Python for loop, we will see how to use for loop in python with examples.

Also, we will discuss:

  • For Loop in python
  • Loop over a string in python
  • Python range() function
  • Nested loops in python
  • Else in for loop
  • Python for loop backward

For Loop in python

For loop in Python is used to iterate over a items of any sequence such as list, string, tuples etc.

Example:

chocolate = ['Dairy Milk', 'Kit Kat', 'perk']
for a in chocolate:
print(a)

After writing the above code (for loop in python), Ones you will print ” a ” then the output will appear as a “Dairy Milk Kit Kat perk”.

Here, the loop executes a set of statements for each item present in the list. You can refer to the screenshot for the loop in python.

For Loop in python
For Loop in python

This is a For Loop in python example.

You may like to read, How to use Pandas drop() function in Python.

Loop over a string in python

In python, we can iterate over a word of string by using for loop and in operator in python.

Example:

my_string = "Python"
for a in my_string:
print(a)

After writing the above code (loop over a string in python), Ones you will print ” a ” then the output will appear as a “ P y t h o n ”.

Here, the loop iterate over all characters in the string ” Python “. You can refer to the below screenshot loop over a string in python.

Loop over a string in python
Loop over a string in python

Python range() function

In python, to loop through a code we use range() function in Python and it returns the sequence of number starting from 0 by default and it will end at a specified number.

Example:

for value in range(4):
print(value)

After writing the above code (python range() function), Ones you will print ” value ” then the output will appear as a “ 0 1 2 3 ”.

Here, we use range() function for a sequence of numbers and the for loop to iterate through numbers. You can refer to the screenshot python range() function

Python range() function
Python range() function

The above in an example of Python range() function.

Nested loops in Python

In python, the nested loop is a loop inside the loop it is used to repeat some action of the items in the outer loop.

Example:

numbers = ["5", "8", "10"]
colors = ["red", "blue", "black"]
for a in numbers:
for z in colors:
print(a,z)

After writing the above code (nested loops in python), Ones you will print ” a, z ” then the output will appear.

Here, the inner loop will execute one-time for each iteration of the outer loop. You can refer to the below screenshot nested loops in python.

Nested loops in python
Nested loops in python

This is an example, Nested loops in Python.

Else in for loop in Python

In python, else in for loop is used to specifies a block of code when the loop is finished. Else block appears after the body of the loop when iteration is completed.

Example:

for value in range(5):
print(value)
else:
print("Loop Ends")

After writing the above code (else in for loop), Ones you will print ” value ” then the output will appear ” 0 1 2 3 4 Loop Ends “.

Here, else block will be executed when all iterations are completed, the program exits the loop only after the else block. You can refer to the below screenshot else in for loop.

Else in for loop
Else in for loop

This is an example on Else in for loop in Python.

Python for loop backward

The backward for-loop iterates through a sequence backward, we will use the reversed() function to iterate over a sequence backward.

Example:

for val in reversed(range(5))
    print(val)

After writing the above code (python for loop backward), Ones you will print ” val ” then the output will appear ” 4 3 2 1 0 “. Here, we will use reversed(sequence) to print them in the reverse order.

You can refer to the below screenshot for python for loop backward

Python for loop backward
Python for loop backward

In this way, we can use For Loops in python.

You may like the following Python tutorials:

In this tutorial, we learned how to use For Loops in python with examples and also we have discussed:

  • For Loop in python
  • Loop over a string in python
  • Python range() function
  • Nested loops in python
  • Else in for loop
  • Python for loop backward