Menu Close
Python for loop

Python for Loop Tutorial

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 for 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 little bit about Python for loop.

What is Python for loop ?

Python for loop is used to execute a set of statements for a specific number of times.
or
Python for loop is used to iterate over a sequence ( sequence mab be list, tuple, dictionary, set, or string).

Syntax of for loop in Python

for val in sequence:
	print(val)

In the above syntax of python for loop, a sequence refers to any iterable ( list, tuple, set, or dictionary ) and val is the variable that takes the value of the item inside the sequence of each iteration.

To understand the for loop in Python in 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)

Above example will print the each item of list.

Python for loop with else statement.

You can use 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 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, we want to generate a number from 0 to 9 then we will use the range() function and pass number 10 in range() function.

range() function take three parameter start, stop and step.

  • start refer starting number.
  • end refer to the end of sequence.
  • step refer to how many number skip we want.

Example:

To understand the range() function with python for loop, use 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 above we have print all the numbers between 1 to 20 who divisible by 2.

Example:

If you want to print list of ten number then you can use 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 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 above example, we have printed the 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 What is the Python for loop and how to use for loop in Python in a different for loop example. When you want yo execute a block of code for specific numbers 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 comment and share it with your friends who want to learn Python programming.

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply