for loop in Python
Hi Friends…lets continue our Python Tutorials journey with ‘for loop in Python’. Python for loop is used is when we want to iterate over an activity more than one time.
Although while loop also does the same thing but we use for loop, when we know the number of times we want our loop to execute. In while loop, we are not sure how many times we want to execute; the loop continues till the condition is True. Also, in for loop, we can use more than one variable to control the flow of the loop.
Syntax of for loop in Python:
for i in range(5): print(i) Output: 0,1,2,3,4
- Here first we have to use for keyword
- Then we have to mention the control variable of the for loop, i in this case and then the ‘in’ keyword to specify the range
- range() function generates all the distinct possible values of i in the loop. In our case, range(5) will generate 5 values starting from 0,1,2,3,4. So technically range() generates values starting from 0 till one number less than the value inside range hence 5 will not be printed. range(n) means than total n values will be generated starting from 0 till (n-1)
- at the end, put a colon(:) otherwise you will see error
- At last we will put the instruction of the loop. Remember that all the loops like for loop, while loop, if, else, elif – they all need at least one instruction inside them
- Indentation – instructions places inside for loop must be placed at same indentation level.
FAQ-Python for loop
A for loop is used to iterate over a piece of code for some fixed number of times. The syntax allows us to specify how many times we want to iterate over the loop.
To use for loop, first we need to mention the ‘for’ keyword
Then mention the variable and its range to iterate
Inside for loop write the instructions to execute.
Yes we can use any level of for loop nesting in Python.
Let’s see examples of for loop in Python:
1. for loop to print integers – This will print value from 0 to 5. Total 6 values starting from 0 to 6-1=5. 6 will not be printed
for i in range(6): print(i)
2. for loop to find sum of numbers – here we will find sum of numbers from 1 to 10
sum=0 for i in range(1,11): sum+=i print("Sum of first 10 numbers is:",sum)
3. for loop to check odd and even numbers – here we will check whether the number is odd or even number
for j in range(10): if j%2 ==0: print(j,"is even number") else: print(j, "is odd number")
4. Nested for loop – here we will print tables for number 2 and 3. The outer for loop will start from 2 and end at 3.
for i in range(2,4): print("Maths table for number",i,":") for j in range(1,11): print(i,"*",j,"=",i*j) print()
5. Nested for loop – here we will print factorial for numbers from 1 to 5. Here the outer for loop will run from 1 to 5. For each value of outer for loop, inner for loop will run from 1 to the value of i. Since in inner for loop, we want to consider value of i also in range, hence we have given i+1.
fac=1 for i in range(1,6): for j in range(1,i+1): fac=fac*j print("Factorial of",i,"is: ",fac) fac=1
6. using list in for loop – here we will take a list(array) and using for loop, we will print all the elements of the list. We will discuss lists in detailed in later sessions
list=[2,5,90,34,22,57,4,43] for i in list: print(i,end=',')
7. range() function with 2 arguments – we can change the starting value from which the integer will start. Here first argument tells that start printing from 2 and end at 9. 10 will not be printed
for i in range(2,10): print(i)
8. range() function with 3 arguments – Change the way we increment the integer. First argument tells that start integer from 3. Second argument tell end at 12(13-1). The third argument tells that Keep incrementing the integer at every step by 2.
for i in range(3,13,2): print(i)
Here the values generated will be:
3 -> 5(3+2) -> 7(5+2) -> 9(7+2) -> 11(9+2)
Now after this 11+2=13 will not be printed since the loop has to end at 12 not 13.
9. range() function with 3 arguments – In this example, the last value will be two values less than the second argument. Since we are incrementing by 2 at every step. So, after 8, next value will be 10 which will not be taken hence for loop will stop at 8
for i in range(0,10,2): print("Inside for loop",i)
10. range(3,3) having same values for both the arguments – Here we will see empty result because the starting value is 3 and last value will be 2(3-1). So, in this case starting value is greater last value and range() does not traverse in descending order.
for i in range(3,3): print(i)
11. range(5,4) having second argument less than first – In this case also there will be no output because start value 5 is greater than end value 3(4-1)
for i in range(5,4): print(i)
12. range(10,2,-1) traverse in descending direction – here start point is 10 and end point is 2. So how to make range function traverse in descending direction. To do that we need to use 3rd argument which tells how to decrement the integer to go to end point. This looks like point number 8. But the difference is that since we are going in descending direction, the end point will be 3(2+1) and third argument will be a negative number
for i in range(10,2,-1): print(i)
The output will start from 10 and end at 3(2+1)
13. range(5,1,-1) traversing in descending direction – here the values will be printed from 5 and end at 2
for i in range(5,1,-1): print(i)
14. processing strings in for loop – in the below program we created a string variable with value ‘python’. Now using for loop, we are traversing through each letter of string variable
str="python" for letter in str: print(letter)
15. Count the number of characters in a string – here we will count the number of letter O/o in the string using for loop
str="Python is good to learn" o_count=0 for letter in str: if letter=='o' or letter=='O': o_count+=1 print("Count of letter O/o in string is:",o_count)
16. using else statement with for loop – here we have use a for loop which starts value from 0 and increments by 2 at every step. Then the last value will be 8 since 8+2=10 is will not be part of for loop. The for loop stops now and control goes to else and prints 9 in else loop
for i in range(0,10,2): print("Inside for loop",i) else: print("Inside else loop",i)
Do not use more than one else statement with for loop otherwise the code will fail. As we already discussed, every else statement must be associated with an immediate preceding if, elif, while or for loop.
For more information on Python Documents: https://docs.python.org/3/tutorial/