Python for loop
Python for loop
A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other programming language like C or Pascal. It is used to traverse the Python data structure like list, dictionary, or tuple.
for iterating_var in sequence: statements
If a sequence holds an expression list, it is evaluated first. Then, the first item in the sequence is allocated to the iterating variable iterating_var. Then, the statements block is executed. Each item in the list is assigned to iterating_var; the statement block is executed until the entire sequence is terminated.
In the above flow diagram, for loop iterate over the sequence. It takes the first element of the sequence and executes the statement block. It moves on the next item in sequence until no item left in the sequence.
Examples of for loop
1. Write a program to print the square of two number from a given list
list = [1,2,3,4,5] for e in list: print(e**2)
1 4 9 16 25
2. Write a program to Character from a given string
str = 'Python' for e in str: print(e
P y t h o n
3. Write a program to find the sum of all number of given list
list = [25,42,32,12,33] sum = 0 for e in list: sum = sum+e print("The sum is:",sum)
The sum is: 144
Iterating by Sequence Index
We can iterate the sequence through the index of the element. First, let’s understand the range function.
The range () function:
The range () function is used to iterate over a given sequence of numbers. Generally, it takes three arguments (stop and step are optional). Python provides flexibility to define range function according to our requirement. For example:
- range(n)- Generate the numbers starting from start to n-1 range(5) will generate 0,1,2,3,
- range(start,stop)- Generate a set of numbers starting from start to stop-1 range(1,10) will generate 0,1,2,3,4,5,6,7,8,9
- range(start,stop,step)- We can generate numbers having difference of the given step-size . range(3,20,3) will generate 3,6,9,12,15,18
Example of for loop using range()
Write a program to find sum of first 1 to n natural number
n=int(input('Enter the number')) # Define a variable to stored number for specific range sum=0 for i in range(1,n+1): #As we know range(n) will generate n-1 that’s why we assign n+1 #it will generate n+1-1 or n numbers sum=sum+i # Sum of numbers stored in sum variable print(sum)
Enter the number 50
Write a program to check the given number is prime or not.
n = 12 # If given number n is greater than 1 if n>1: # Iterate from 2 to n // 2 for i in range(2, n // 2): # If n is divisible by any number between 2 and n // 2, it is not prime if (n % i) == 0: print(n,"is not a prime number") break else: print(n, "is a prime number") else: print(n, "is not a prime number")
23 is a prime number
Python Nested for loop
If a for loop exists inside another for loop, then it is called Nested for loop. Nested for loop is best to use in pattern programming. For example:
1. Write a program to print stars in a triangle shape.
n = int(input("Enter the number of lines")) # Take input for number of lines for i in range(n): #first for loop is using for print number of lines for j in range(i): #second for lop is using for print star(*) print("*",end='') print()
Enter the number of lines :10* ** *** **** ***** ****** ******* ******** *********
2. Write a program to find the prime number within a range
start = int(input("Enter the start range")) stop = int(input("Enter the stop range")) for n in range(start,stop+1): # If given number n is greater than 1 if n>1: # Iterate from 2 to n // 2 for i in range(2,n//2): # If n is divisible by any number between 2 and n //2, it is not prime if(n%i==0): break else: print(n) else: print("the number is not greater than 1")
Enter the start range:100
Enter the stop range:150
101 103 107 109 113 127 131 137 139 149
Using else statement with for loop
Python provides the optional ‘else’ statement associated with for loop. The ‘else’ statement executes only when the loop has completed all iteration. For example:
for i in range(6): print(i) else: print("Iteration is completed")
0 1 2 3 4 5 Iteration is completed