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 by writing just 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).

range() function

range() function is used to generate a sequence of numbers. It is commonly used in for loops to iterate over a specific range of numbers. The range() function can take up to three arguments: start, stop, and step.

Syntax: range(start, stop, step)

In the range() function start and step are optional parameters. By default, the start is set to 0, and the step argument is used when you want to skip the sequence.

Let’s understand the range() function with the help of an example:

Example-1:

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

Output:

0
1
2
3
4

In this example, the range(5) generates a sequence of numbers from 0 to 4 (because the range function does not consider the last value), and the for loop iterates over each number and prints it.

Example-2:

# print odd number from 1 to 10
for num in range(1, 10, 2):
    print(num)

Output:

1
3
5
7
9

In this example, the range(1, 10, 2) generates a sequence of numbers starting from 1 and incrementing by 2 until reaching 10 (but not include 10).

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 to infinite times.

For loop in reverse order

# print number from 10 to 1 using for-loop

# pass negative step argument (-1) to iterate in reverse order 
for i in range(10,0,-1):
    print(i)

Output:

10
9
8
7
6
5
4
3
2
1

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