For Loop in Python is slightly different from other languages. It is used to iterate the elements present in a sequence of characters ( strings, lists, tuples etc ). By iteration, it means to visit every item in the sequence one by one in an order.
Unlike other languages, Python for loop does not have a condition. In every iteration, the for loop checks if the visited element is the last element in the sequence. As the last element is visited, the control exits the for loop.
for variable_name in sequence_name :
body of loop
There are four components of the for loop.
- for keyword
- variable_name: a variable is defined which refers to the value of element being visited in every iteration.
- sequence_name: it is the name of the sequence which is to be traversed.
- body of loop: it consists of a set of instructions. Indentation separates the body of loop from rest of the code.
Flow of control:
Let us create a very simple program to understand the syntax and working of Python for loop.
In this program, we will create a list and print the elements of the list one by one using the for loop.
number=[1,2,3,4,5] #creating a list print("Show the elements in the list one by one.") for N in number: print(N)
Show the elements in the list one by one. 1 2 3 4 5
- In the above program, a list named number is created which contains 5 elements i.e [1,2,3,4,5].
- In the for loop, a variable N is defined which is used to refer to the value being visited and the value is printed.
- After the last iteration, the control exits the loop body and the program ends.
Range() function in Python
This is a built-in function which is used to create sequences. The sequence produced by range() can be referred by a variable.
This function takes three arguments.
- start: From where to start the sequence.
- stop: Where to stop the sequence.
- step_size: Difference between the elements which is 1 by default. This parameter is optional.
range() function is helpful when you want to print a long sequence like numbers from 1 to 50.
range( start, stop, step_size )
a = list(range(1,51)) print(a)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
Note: The range() function does not include the last value. In the above output, 51 is not included.
- There is one more way to use range(). One argument x is passed to the function which creates a sequence starting from 0 upto (x-1).
[0, 1, 2, 3, 4]
For loop with range() function.
Let us create a program which will find out the sum of first 5 natural numbers.
natural=list(range(1,6)) sum=0 for x in natural: sum=sum+x print("Sum of first 5 natural numbers = ",sum)
Sum of first 5 natural numbers = 15
- In the above program, a variable natural is created which refers to a list. This list created using the range() function.
- In range() function, the first parameter is 1 and second parameter is 6. Therefore the elements in the list will start from 1 and end at 5.
- A variable sum is created which refers to the value 0.
- In the next line, the for loop is created. In the for loop, the variable x is used to refer to the elements of the list.
- In the first iteration, the interpreter visits the first element and variable x starts referring to the first element in the list. The value of x is added to sum.
- In the next iteration, the interpreter visits the second element and variable x refers to it. The value of x is again added to sum. Similarly, all the elements of the list are visited by the interpreter one by one and their value is added to sum.
- When the last element of the list is visited, the interpreter adds the value of the last element to sum and the control exits the loop.
- The next instruction prints the value of sum as the output.
Take a look at this diagram to understand the working of the Python for loop.
For loop with else block
Python allows you to add an else-block with the for loop. This else-block gets executed when the sequence runs out of elements. Although, if the for loop contains a break statement, the interpreter ignores the else-block.
print("TABLE OF 14") multiply = list(range(1,6)) for x in multiply: product = 14*x print(product) else: print("Only 5 multiples are calculated!")
TABLE OF 14 14 28 42 56 70 Only 5 multiples are calculated!