In this post, we will learn Python while loop with a very basic explanation and example. So let’s start learning all the concepts in detail.

while loop in python

while-loop is used to perform repetition over a block of code as long as the condition is True. that means if you want to perform the same task again and again in that case, you can use while-loop.

Let’s understand while-loop with the help of an example, suppose you want to print ‘Hello world’ ten times in your program you can do this by writing ‘Hello world’ ten times using the print function (but this is not the correct way) instead, you can use while-loop to perform the same task.

Example:

# This example is only for your understanding
i = 0
while i<=10:
    print('Hello world')
    i = i+1

Output:

Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world

Syntax of while loop

Three things are very important for the while-loop

  1. Initialization – where to start a loop
  2. Condition – where to stop a loop and
  3. Increment or decrement

In case of not assigning any of the three things, you will get an error or infinity while-loop.

NOTE: while-loop is executed as long as the condition is True.

Flowchart for while-loop

Flowchart for while-loop

Infinity while loop

while True: –> gives you infinite while-loop.

Example:

while True:
    print('allinpython')

The above program runs infinite times.

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

Examples

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

To print numbers from 1 to N using a while-loop, you simply initialize one variable with the value 1 (e.g., i = 1) and run the while-loop as long as i <= N. After each iteration, increment i by 1.

num = int(input('Enter a number: '))

i = 1

while i<=num:
    print(i)
    i+=1

Output:

Enter a number: 7
1
2
3
4
5
6
7

Write a python program to find the sum of the N natural numbers using a while-loop

In the previous example, we printed N natural numbers. In this example, instead of printing ‘i,’ we should add i and store the result in a variable called total. Initially, total is set to 0.

Number = int(input('Enter a number: '))
total = 0
i = 0
while i<= Number:
    total += i
    i+=1

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

Output:

Enter a number: 10
sum of 1 to 10 Natural number is: 55

Write a python program to find the multiplication of the N natural numbers using a while-loop.

Number = int(input('Enter a number: '))
m_total = 1
i = 1
while i<= Number:
    m_total *= i
    i+=1

print(f'multiplication of 1 to {Number} Natural number is: {m_total}')

Output:

Enter a number: 5
multiplication of 1 to 5 Natural number is: 120

Iterate in list Using while loop

my_list = ['one','two','three','four','five']

i = 0

while i< len(my_list):
    print(my_list[i])
    i+=1

Output:

one
two
three
four
five

Iterate in tuple Using while loop

my_tuple = ('saurabh',19,'allinpython.com')

i = 0

while i < len(my_tuple):
    print(my_tuple[i])
    i+=1

Output:

saurabh
19
allinpython.com

Iterate in String Using while loop

my_string = 'allinpython'

j = 0
while j<len(my_string):
    print(my_string[j])
    j+=1

Output:

a
l
l
i
n
p
y
t
h
o
n

This is all about python while loop hopes 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