Python for loop Statement Syntax Plus Examples – This is a Python tutorial to explain the syntax and use of for loop with different practical examples in Python scripts.
Python for Statement
We can use a Python for statement for two main puposes:
- Use a for loop to iterate over items in a sequence
- Using for loop to use as a counter loop
Use a for loop to iterate over items in a sequence
Python for statement is generally used to iterate over a sequence of items like on a list or string. Therefore, we can display all characters in a string, or we can show all items in a list of cities.
When we use a for statemnt to iterate over a sequence of items, the general form of for statement is as follows:
Syntax of Python for Statement
for <item> in <sequence>: block_of_statements
Example of python for statement
Display names of all cities from a list using for statement:
Python Code is as follows:
cities = ['Rahim Yar Khan','Sadikabad','Islamabad','Lahore'] for city in cities: print(city) The output will be as follows:
Rahim Yar Khan Sadikabad Islamabad Lahore
Using a Python for statement as a Counter Loop
As we know that a counter controlled loop is used to repeat a set of statements for a fixed number of time. We may use a for statement to repeat a block of statements for a counter control variables’s initial value to final value. That is the block of statement is repeatedly executed as long as the given loop counter variable reaches its final value, like 10 times from 1 to 10.
Use of range() function in for statement
This is because of Python range() function. A range() function is used in for loop for this purpose, as follows:
Syntax of Python for Statement using range() function
for var in range(initial_value,excluding_final_value,step_value): block_of_statements
Note: Example: range(1,11,1), first 1 indicates initial value, 11 indicates excluding_final_value, means last value will be 10 not 11. 11 is excluding in the range. And last 1 indicates step value to increment. We can ignore 1 as it is default step value.
Example of for loop using range() function
Display 1 to 10 numbers using for loop in Python
for i in range(1,11): print(i)
The output will be:
Why to Use 1 to 11 in range() function to get 1 to 10
We will use range(1,11) to display 1 to 10, because range function generates numbers from start to final excluding the final value. So if we use range(1,10), it will provide numbers from 1 to 9. Therefore to get numbers from 1 to 10, we use range(1,11) or range(1,10+1).
Another example of using Python for statement
Show all even numbers from 1 to 10 using Python for Statement
Note: range(2,11,2) from 2 to 11-1 and increment / step of 2.
The ouput will be: 2 4 6 8 10
Example: Python for loop
Display 10 down to 1 numbers using for loop and range() function
Another Syntax of for loop with else clause
Python for loop also contains an optional else clause. The block of statemnts related to else clause executed when for loop terminates normally completing its iteraions in a normal way.
Example: Show 1 to 5 using for loop else with clause
The output will be
Display 1 to 5 using Python for loop with else clause, 1 2 3 4 5 for loop terminated normally Note: that else cluase bloack of statements executed only onece after successful termination of loop.
If we use break Then else block not excuted
Note: Since when i==3 , break statement will terminate for loop abnormally, so else clause block will not execute. And last statement which is not part of for loop will be executeed. it will display ‘for loop terminated with break statement’
The output of program:
Display 1 to 5 using Python for loop with else clause, 1 2 for loop terminated with break statement
Normally in a programming language we use for loop statemnt as a counter loop. The for loop iterates over a sequence of numbers, for example, to show 1 to 10 numbers in C++, we use the for loop as follows:
for(i=1; i<=10; i++) cout<<i<<endl;
But in Python a for statement is generally used for iterating over
items in a sequence like a list or string etc. For exaple accessing, traversing or displaying all characters in a string.
Consider an other example, we have a list of colors. We may use python for loop to print every item in this list.
Python for Loop Syntax Plus Examples
You will like to read about the use of continue and break statement in for loop as well as while loop.