Introduction
In this chapter, we will discuss Python's
for a loop. Same as while loop, for loop, also control flow statement. In Python, for loop is used when we want to iterate over a sequence.
Python For Loop
Syntax - for IterationalVariable in sequence:
- Body of the loop
In Python, for loop works like C# foreach loop.
For loop with a list
The following example explains how can you iterate a list using for loop.
Example - list=['C','C++','Java','C#']
- print("This is our list ",list)
- i=1
- for val in list:
- print("List Item",i,"is",val)
- i=i+1
Output
For loop with tuple
The following example explains how can you iterate tuple using for loop.
Example - tuple=('C','C++','Java','C#','Python')
- print("This is our Tuple ",tuple)
- i=1
- for val in tuple:
- print("Tuple Item",i,"is",val)
- i=i+1
For loop with string
The following example explains how can you iterate string using for loop.
Example
- string="C-Sharp Corner"
- for val in string:
- print(val)
Output
Range Function
Sometimes we need to iterate loop in a given range and we cannot create a sequence. For example, if you want to print a number from one to 1000, so will you create a sequence or list from 1 to 1000? Of course, you cannot go like this. So in Python, we have a range function, in which we will decide range inside the function.
Syntax of range function - range([start],end,[step])
In Python range function has 3 arguments In which two are optional, which are the following:
- start- This is an optional argument. This argument decides from where you want to start your loop. If you do not provide this value then its value will be zero(0) by default.
- end- This argument decides the end of the loop.
- step- How much you want to increment after one iteration. This is also an optional argument. So if you do not give this argument then it will increment by 1 step.
Example 1
- for val in range(11):
- print(val)
The above example of the range contains only one parameter so that it will be the end parameter and start and step will be set with its default value, so start will be 0 and the step will be 1 and the value of end is 11.
Output
Example 2 - for val in range(1,11):
- print(val)
The above example of the range contains two parameters so that it will be the start and end parameter and the step will be set with its default value. So in the above example. start=1,end=11 and step=1(default).
Output
Example 3
- for val in range(1,11,2):
- print(val)
In the above example start=1 end=11 and step=2.
Output
For loop with else condition
Same as while loop you can also use for loop with else condition in python. For loop works the same as normal for loop but whenever for loop condition will be false it's else part will execute.
Note
else part will only execute when for loop condition will be false. If for loop will be terminated by break statement then for loop's else part never executes.
Example
-
-
-
- num = int(input("Enter a Number : "))
- for i in range(2,num):
- if(num%i==0):
- print("%d is not a prime number..."%num)
- break
- else:
- print("%d is a prime number..."%num)
Output
Nested Loop
A nested loop means a loop inside the loop. In python, you can create a loop inside a loop.
Example 1
- str="";
- for Row in range(0,7):
- for Col in range(0,7):
- if (((Col == 1 or Col == 5) and Row != 0) or ((Row == 0 or Row == 3) and (Col > 1 and Col < 5))):
- str=str+"*"
- else:
- str=str+" "
- str=str+"\n"
- print(str);
Output
Example 2
- str="";
- for Row in range(0,7):
- for Col in range(0,7):
- if (Col == 1 or ((Row == 0 or Row == 3 or Row == 6) and (Col < 5 and Col > 1)) or (Col == 5 and (Row != 0 and Row != 3 and Row != 6))) :
- str=str+"*"
- else:
- str=str+" "
- str=str+"\n"
- print(str);
Output
Example 3
- row=15
- col=18
- str=""
- for i in range(1,row+1):
- if((i<=3)or(i>=7 and i<=9)or(i>=13 and i<=15)):
- for j in range(1,col):
- str=str+"*"
- str=str+"\n"
- elif(i>=4 and i<=6):
- for j in range(1,5):
- str=str+"*"
- str=str+"\n"
- else:
- for j in range(1,14):
- str=str+" "
- for j in range(1,5):
- str=str+"*"
- str=str+"\n"
- print(str)
Output
Summary
In the next chapter, you will learn how to use Python functions.