< Prev
Next >

# Python For Loop

Python language gives us a powerful looping structure through which we could execute statements in a loop(again and again) while iterating over a sequence of values. These sequence of values could be given to us by -
• The range() function, or
• Collection data types of Python such as -list, tuple, dictionary or set, which are used to store a sequence/collection of values.

• ## The range() function

• We could use the for loop to execute the statements in a loop, while iterating over a sequence of values given by the range() function. The range() function returns a sequence of numbers, starting at a specified value(default is 0), each value is incremented by a specified value(default is 1) and ending at a specified value.

Syntax of range() function -
``range(start, end, increment)``

Where,
• The start is the starting integer value of the sequence and it is optional with default value 0.
• The end is the last integer value(minus 1) of the sequence and it is not optional.
• The increment is the integer number for incrementation, and it is optional with default value 1.

## Using for loop with range() function

Let us see how to use the for loop with the range() function.

``````# Python for loop with range() function

# This gives us a sequence of values starting from 2 to 4 and incrementing by 1
x = range(2,5)

print('First for loop :')

# Using for loop is used to print the sequence of values
for i in x:
print(i)

# This gives us a sequence of values starting from 0 to 5 and incrementing by 1
x = range(6)

print('Second for loop :')

# For loop is used to print Hello for each number in the sequence of values
for n in x:
print('Hello')
print(n)``````

## Output

``````First for loop:
2
3
4
Second for loop:
Hello
0
Hello
1
Hello
2
Hello
3
Hello
4
Hello
5``````

• ## Using for loop to iterate over a collection

• Collection data types of Python such as -list, tuple, dictionary or set, which are used to store a sequence/collection of values. We could use the for loop to execute the statements in a loop, while iterating over a sequence of values stored in a collection.

• ## for loop iterating over a list

• The elements of a set collection are declared within in square brackets i.e [ ]

``````#Python - for loop iterating over elements stored in a list

#A list
a_list = [1, 2, 3, 4, 5]

#for loop iterating over list
for x in a_list:
print("x =", x)
``````

## Output

``````x = 1
x = 2
x = 3
x = 4
x = 5``````

• ## for loop iterating over a tuple

• The elements of a set collection are declared within in curly braces i.e { }

``````#Python - for loop iterating over elements stored in a tuple

#A tuple
a_list = (1, 2, 3, 4, 5)

#for loop iterating over list
for x in a_list:
print("x =", x)
``````

## Output

``````x = 1
x = 2
x = 3
x = 4
x = 5``````

• ## for loop iterating over a set

• The elements of a set collection are declared within in curly braces i.e { }

``````#Python - for loop iterating over elements stored in a set

#A set
a_set = {1, 2, 3, 4, 5}

#for loop iterating over a set
for x in a_set:
print("x =", x)

``````

## Output

``````x = 1
x = 2
x = 3
x = 4
x = 5``````

• ## for loop iterating over a dictionary

• The elements of a dictionary collection are declared within in curly braces i.e { }. Each value in a dictionary collection is stored in a pair of key and value, where, using the value of a key, we can access its associated value stored in a dictionary.

The for loop, when iterating over a dictionary collection data type returns the key, using which we could access its associated value stored in the dictionary.

``````#Python - for loop iterating over elements stored in a list

#A dictionary
a_dictionary = {
"one":"1",
"two":"2",
"three":"3",
"four":"4",
"five":"5"
}

#for loop iterating over pairs of key and value stored in a dictionary
for x in a_dictionary:
print(x, a_dictionary[x])
``````

## Output

``````one 1
two 2
three 3
four 4
five 5``````