For Loop in Python Programming with Examples
Python For Loop: For loop in python is mainly used for definite iteration over specific elements or statements again and again while the condition is True. Definite means limited intervals.
In C/C++ or other old programming languages, the syntax followed for for-loop is as follows: for (i=0; i<n; i++). But in python programming, the syntax used for for-loop is totally different.
Syntax:
#for-loop syntax for var in sequence: # statements
Here the var is just a variable name used for iterating over an element (sequence). The for loop continues iterating until it reaches the last element of the sequence.
Let’s see some example to understand the concept better
Example 1:
#for loop example sequence = [3,2,1,4,5] for var in sequence: print(i) # output # 3 # 2 # 1 # 4 # 5 #it iterated over the each elements of sequence
Below is the given Flow Chart for a better understanding
As discussed before, that in C/C++ or other old languages, the syntax followed for for-loop is as follows: for(start; end; steps). But in python programming the syntax is different. We use python’s inbuilt range function to meet the complete functionality of for loop.
Range() Function in Python
Python is popular for its large number of inbuilt functions and libraries. Range() is also one of its function. We can use the range() function in three ways:
(i). Range(n): If we want to generate numbers from 0 to 10. Then we can easily generate it by using the range() function. By default, it iterates over (n-1). So that if we set the range of n to 11 then it will return (n-1) result ie. output till 10.
print(range(10)) print(list(range(10)) # output # range(0, 10) # [0,1,2,3,4,5,6,7,8,9]
(ii). Range(start, stop): If we want to generate numbers from 2 to 10. Then we can easily generate it by using the range() function. But here the difference is we can use the starting as well as ending index. But in the previous one, we cannot use the starting index.
(iii). Range(start, stop, steps): Here in this function, we can set starting index, stop index, and step size.
Example:
#Range function example for i in range(2,10,2): print(i) # output # 2 # 4 # 6 # 8
Let’s solve one problem to understand it more deeply and also to understand where the use of for loop comes into use.
Exercise 1: Write a Python Program to print multiples of 2.
#Python Program to Print Multiples of 2 for i in range(0,11): result = i*2 print(result) #output # 0 # 2 # 4 # 6 # 7 # 8 # 9 # 10
Nested for loop in Python
We can also use nested for loop in python same as we already studied about nested if statements. Nested for loop is nothing, it just one or more for loops present inside other for loop.
Example:
#nested for loop example for x in range(0,3): for y in range(0, 3): print(x, "-", y) # output # 0 - 0 # 0 - 1 # 0 - 2 # 1 - 0 # 1 - 1 # 1 - 2 # 2 - 0 # 2 - 1 # 2 - 2
For Loop with else statement
We can also use else block with for loop. The else block is executed only if the elements in the for loop exhausts. The break keyword can also be used to stop the execution of for loop. Else part is executed only if no break occurs during execution of for loop.
Example 1:
num = [1,2,3] for i in num: print(i) else: print("num not found") # output # 1 # 2 # 3 # num not found
Example 2:
name = "Adams" entries = ["Sadm", "Downy", "Downs",] for x in entries: if x == name: print("Name Found") break else: print("Nothing found") # output # Nothing found