10 Examples of For loop in Python

This blog contain 10 examples of for loop

Example 1 : Printing all elements from a list

programming_lang = ['C++','Python','Java','PHP','Perl']
for c in programming_lang:
    print(c)

Output

C++
Python
Java
PHP
Perl

Example 2 : Asking user to enter any string and printing all characters of a string using for loop

str = input('Enter any String : ')
for s in str:
    print(s)

Output

Enter any String : India
I
n
d
i
a

Example 3 : Using range to display numbers

for r in range(5):
    print(r)

Output

0
1
2
3
4

Example 4 : Displaying numbers from 1 to 8 using range. Numbers 1 to 7 are displayed

for r in range(1,8):
    print(r)
1
2
3
4
5
6
7

Example 5 : Displaying numbers from 1 to 20 with incremental value of 3.

for r in range(1,20,3):
    print(r)

Output

1
4
7
10
13
16
19

Example 6 : Creating a number list from 1 to 8. Breaking the loop when the current value of list is 4

num = [1,2,3,4,5,6,7,8]
for n in num:
    print(n)
    if n == 4:
        break

Output

1
2
3
4

Example 7 : Creating a country list and breaking the loop when the current value of list is France

country = ['India','Singapore','USA','UK','France','Germany','Brazil']
for c in country:
    print(c)
    if c == 'France':
        break

Output

India
Singapore
USA
UK
France

Example 8 : Creating a number list from 1 to 5 and calculating square of each number.

num = [1,2,3,4,5]
for n in num:
    square = n*n
    print('Square of %d is %d'%(n,square))

Output

Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25

Example 9 : Asking user to enter 5 radius values to calculate area of a circle

print('Enter 5 Radius values to calculate area of circle\n')
counter = 1
pi = 3.14
for v in range(5):
    try:
        rad= float(input('Enter #%d radius value : '%counter))
        counter += 1
        area_of_circle = pi * rad*rad
        print('Area of circle with %.2f radius is %.2f'%(rad,area_of_circle))
    except ValueError:
        print('Enter correct Radius Value')

Output

Enter 5 Radius values to calculate area of circle

Enter #1 radius value : 2
Area of circle with 2.00 radius is 12.56
Enter #2 radius value : 5
Area of circle with 5.00 radius is 78.50
Enter #3 radius value : 9
Area of circle with 9.00 radius is 254.34
Enter #4 radius value : 7
Area of circle with 7.00 radius is 153.86
Enter #5 radius value : 4
Area of circle with 4.00 radius is 50.24

Example 10 : Displaying tables of 2,3 and 4 using for loop

counter = 1
for num in range(2,5):
    print('Table of %d'%num)
    counter = 1
    for c in range(1,11):
        res = num * counter
        print('%d*%d=%d'%(num,counter,res))
        counter += 1

Output

Table of 2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
Table of 3
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30
Table of 4
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40


Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s