In this post, we will learn about python for loop with very basic explanations and examples. So let’s start learning all the concepts of for loop in detail.

For loop in python

For loop is used to perform repetition (or iteration) over the block of code as long as the condition is true.

For loop is very easy and simple to use as compared to a while loop. In a while loop, you have to do initialization, condition, increment, or decrement but this is not the case with a for-loop you can do all three things in a single line of code.

Suppose you want to print 1 to 10 numbers in your program, using for loop you can do it just by writing two lines of code.

Example:

for i in range(1,11):
    print(i)

Output:

1
2
3
4
5
6
7
8
9
10

NOTE: for-loop and while-loop both perform the same task (repetition over the block of code) but in a different ways.

Syntax of for loop

syntax of for-loop

range() function starts from the initial value and stops before the specified value.

In our case loop started from 1 and stop at 10 (11-1).

Infinity for loop

For infinity for-loop, you have to import INFINITY form asyncio.windows_events module.

Example:

from asyncio.windows_events import INFINITE

for i in range(INFINITE):
    print(i)

Above program prints numbers(starting from 0) infinite times.

let’s do some examples based on for-loop for practice.

Examples

Write a python program to print 1 to N natural numbers using for loop.

number = int(input('ENter a number: '))

for i in range(1, number+1):
    print(i)

Output:

ENter a number: 5
1
2
3
4
5

Write python program to print sum of 1 to N natural number using for loop

number = int(input('ENter a number: '))
total = 0
for i in range(1, number+1):
    total+=i

print(f'sum of 1 to {number} is : {total}')

Output:

ENter a number: 10
sum of 1 to 10 is : 55

Write a python program to print factorial of N natural number using for loop.

number = int(input('ENter a number: '))
fact = 1
for i in range(1, number+1):
    fact*=i

print(f'factorial of {number} is : {fact}')

Output:

ENter a number: 4
factorial of 4 is : 24

iterate in the list using for loop

There are two different ways to iterate in a list using for loop.

  1. using range() function
  2. using in keyword

1. Iterate in list using range() function

fruit = ['apple','mango','orange','banana','banana']

for i in range(len(fruit)):
    print(fruit[i])

Output:

apple
mango
orange
banana
banana

2. Iterate in list using using in keyword

names = ['rose','deep','roy','yash']
for i in names:
    print(i)

Output:

rose
deep
roy
yash

iterate in the Dictionary using for loop

info = {'name':'saurabh','age':20,'country':'India'}
for i in info:
    print(f'{i} : {info[i]}')

Output:

name : saurabh
age : 20
country : India

this is one of the ways to iterate in the Dictionary, there are a few more ways –> click here to learn python Dictionary in detail.

iterate in the String using for loop

my_string = "allinpython"

# iterate in the String Using range function
# for i in range(len(my_string)):
#     print(my_string[i])


# iterate in the String Using 'in' keyword
for i in my_string:
    print(i)

Output:

a
l
l
i
n
p
y
t
h
o
n

this is all about python for-loop hope this post adds some value to your life – thank you.

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment