Python For Loop with Examples

By: On:

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

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

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

Python range() function

In python, to loop through a code we use range() function 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

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

Else in for loop

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

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

Leave a Comment