Python For Loop

Python For Loop

Python for loop is a looping structure that repeats a set of instructions a certain number of times that is known to the programmer. It iterates over a sequence and executes a set of statements for each element of the sequence. A sequence can be any of the data structures of Python like list, tuple, set, dictionary, range iterables, etc.

Syntax of Python For Loop:

for iterating_var in sequence:
    statements

Flow Diagram of Python For Loop:

for-loop

 

For Loop over a Range

# Python program to exemplify for loop over a range

print("Result 1:")
for var1 in range(5):
  print(var1, var1 * 2)

print("\nResult 2:")
for var2 in range(2, 6):
  print(var2, var2 ** 3)

print("\nResult 3:")
for var3 in range(10, 2, -4):
  print(var3)

In the program provided above, three for loops are used to iterate over different type of ranges. In the first for loop, a range with only ending index(not included) is specified and this type of range syntax has 1 as default value for the step parameter. In the second for loop, a range with starting and ending index is specified and the last for loop has the starting and ending index along with the step parameter.

Output:

Result 1:
0 0
1 2
2 4
3 6
4 8

Result 2:
2 8
3 27
4 64
5 125

Result 3:
10
6

 

 

For Loop over a List

# Python program to exemplify for loop over a list

colors = ['Red', 'Blue', 'Pink']
for color in colors:
  print(color)

In the program provided above, a list named “colors” is created with three elements. A for loop is then used to iterate over the elements of the just mentioned list and are displayed using the print statement.

Output:

Red
Blue
Pink

 

 

For Loop over a Tuple

# Python program to exemplify for loop over a tuple

months = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
for month in months:
  print(month)

In the program provided above, a tuple named “months” is created with twelve elements. A for loop is then used to iterate over the elements of the just mentioned tuple and are displayed using the print statement.

Output:

January
February
March
April
May
June
July
August
September
October
November
December

 

 

For Loop over a Set

# Python program to exemplify for loop over a set

numbers = {1, 2, 3, 4, 5}
for number in numbers:
  print(number)

In the program provided above, a set named “numbers” is created with five elements. A for loop is then used to iterate over the elements of the just mentioned set and are displayed using the print statement.

Output:

1
2
3
4
5

 

 

For Loop over a String

# Python program to exemplify for loop over a string

word = 'StudyExperts!'
for letter in word:
  print(letter, end=" ")

In the program provided above, a string named “word” is created. A for loop is then used to iterate over the characters of the just mentioned string value and are displayed using the print statement.

Output:

S t u d y E x p e r t s ! 

 

 

For Loop over a Dictionary

# Python program to exemplify for loop over a dictionary

dict = {
  'year': '2022',
  'month': 'January',
  'date': 21
}

print("Result 1:")
for key in dict:
  print(key)

print("\nResult 2:")
for key, value in dict.items():
  print(key,value)

In the program provided above, a dictionary named “dict” is created with three elements. A for loop is then used to iterate over the keys of the just mentioned dictionary and are displayed using the print statement. One more for loop is used which iterates over the items(ley-value pairs) of it and display them.

Output:

Result 1:
year
month
date

Result 2:
year 2022
month January
date 21

 

Share this post

Leave a Reply

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