In this tutorial, we are going to learn Python for loop with appropriate syntax and python for loop example. for loop is used to execute a block of code a specific number of times.
In the previous tutorial, we have seen details about Python while loop. To learn Python while loop in detail, Click Here.
Before going through this article let’s see a little bit about for loop.
Headings of Contents
What is Python for loop?
Python for loop is used to execute a set of statements a specific number of times.
or
For loop in Python is used to iterate over a sequence ( sequence maybe list, tuple, dictionary, set, or string).
Syntax of for loop in Python
for val in sequence:
print(val)
In the above syntax, a sequence refers to any iterable ( list, tuple, set, or dictionary ) and val is the variable that takes the value of each item of the sequence for each iteration.
Python for loop flowchart
To understand the for loop in Python in an easy way, we will take a simple example.
Example
list = [1,2,3,4,5,6,7,8,9,10]
for x in list:
print(x)
The above example will print each item one by one from the list.
Python for loop with else statement.
You can use the else statement with python for a loop. Sometimes we need to print some extra messages after the execution of for loop then we can use the else statement.
Example:
string = 'python'
for val in string:
print(val)
else:
print("End program")
Output:
p
y
t
h
o
n
End program
Python for loop with range function:
Python range() function is used to generate a sequence of numbers. for example, if we want to generate a number from 0 to 9 then we will use the range() function and pass the number 10 in the range() function.
range() function take three-parameter start, stop and step.
- start to refer starting number.
- end refers to the end of a sequence.
- step refers to how many numbers skip we want.
Example:
To understand the range() function with for loop, use a simple program.
for x in range(1, 31):
if x % 2 == 0:
print(x)
Output:
2
4
6
8
10
12
14
16
18
In the above, we have printed all the numbers between 1 to 20 who divisible by 2.
Example:
If you want to print a list of 10 numbers then you can use the range() function.
x = []
for i in range(1, 11):
x.append(i)
print(x)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Python for loop with a break statement:
The break statement is used to terminate the execution of the program even if the condition is True. the break statement is defined with the break keyword.
Example:
i = 1
while i <= 10:
print(i)
if i == 3:
break
i += 1
Output
1
2
3
Python for loop with continue statement:
continue statement is used to stop the current execution of the program and continue with next.
Example:
for x in range(1, 20):
if x % 2 == 0:
continue
print(x)
Output:
1
3
5
7
9
11
13
15
17
19
In the above example, we have printed all the numbers between 1 to 21 who not divisible by 2.
Python Nested loop:
loop inside another loop is called a nested loop. you can use for loop inside another for loop, the inner loop will be executed one time for each iteration of the outer loop.
Example:
a = ['Python', 'Ruby', 'Go', 'R']
b = ['HTML', 'Bootstrap', 'JavaScript', 'CSS']
for x in a:
for y in b:
print(x, y)
print()
Output:
Python HTML
Python Bootstrap
Python JavaScript
Python CSS
Ruby HTML
Ruby Bootstrap
Ruby JavaScript
Ruby CSS
Go HTML
Go Bootstrap
Go JavaScript
Go CSS
R HTML
R Bootstrap
R JavaScript
R CSS
Conclusion:
So, In this article, you have learned all about for loop in Python with the help of an example. When you want to execute a block of code a specific number of times, Then for loop is the best way to do this.
I hope this tutorial will have helped you. If you like this tutorial, please share it with your friends who want to learn Python programming.
Reference:- Click Here