While loop

While loop in Python programming language

While loop in Python programming language

In this tutorial, we will discuss While loop in Python programming language

A while loop in Python programming language executes a target statement repeatedly until satisfaction or test expression is true.

While loop in Python

Declaration

The syntax of while loop

The syntax of while loop in Python programming language is given below:

while expression:
//body of while
    statement(s)

Flow diagram of while loop

Flow diagram – While loop in Python

In the above diagram, initially, the flow of control enters the while loop and checks test expression.

When test expression is true, the flow of control enters the body part of while loop and codes inside the body of while loop is executed.

The process continue until the test expression evaluates to false

When test expression is false, the flow of control jumps out of the body part of the while loop and executes statements below of while loop.

 

Examples

Program 1

To display positive  numbers less than ten using while loop in Python

i=1
while i<10:
    print i
    i=i+1;

 

When the above code  executes, it produces the following results

1
2
3
4
5
6
7
8
9

 

Program 2

count_1=10
count_2=100
while count_1<=20 and count_2>=0:
    print(count_1,count_2)
    count_1=count_1+2
    count_2=count_2+10
print("End the loop")

 

When the above code executes, it produces the following results

(10, 100)
(12, 110)
(14, 120)
(16, 130)
(18, 140)
(20, 150)
End the loop

 

Program 3

Print first ten triangular number Using while loop in Python

#Diclare two variable as int and initialize zero
sum=0
i=0
while i<=10:    #while loop test expression
    sum=sum+i   #Updating sum by i
    print sum,
    i=i+1 #updating i by 1

 

When the above code  executes, it produces the following results

0 1 3 6 10 15 21 28 36 45 55

 

Program 4

To calculate and display the sum of given numbers

#add natural number 1 to n
#1+2+3+4.....+n
n=input("Enter the number for n:" )
        #To take input from user

#initialized two variable sum and count
sum=0
count=0
while count<=n:    #while loop test expression
    sum=sum+count   #Updating sum by count
    count=count+1#update count
    print ("The total is",sum) #display total

 

When the above code is executed, it produces the following results

Enter the number for n:7
('The total is', 0)
('The total is', 1)
('The total is', 3)
('The total is', 6)
('The total is', 10)
('The total is', 15)
('The total is', 21)
('The total is', 28)

 

 

How to use else statement in while loop

In the Python programming language, we can use optional else block with while loop same as for loop in python

The else part is excuted  when the test expression in the wile loop evaluated to false

Program 1

i=0
while i<=5:
    print i, "Is less than equal to 5"
    i=i+1
else:
    print i, "Is not less than equal to 5"

 

When the above code  executes, it produces the following results

0 Is less than equal to 5
1 Is less than equal to 5
2 Is less than equal to 5
3 Is less than equal to 5
4 Is less than equal to 5
5 Is less than equal to 5
6 Is not less than equal to 5

 

 

Python list with the while loop

Program 1

Name=['Ram','Jhon','Muru','Nilo','Mackal','Yaso'];
i=0;
while(i<=5):
    print Name[i];
    i=i+1;

 

When the above code is executed, it produces the following results

Ram
Jhon
Muru
Nilo
Mackal
Yaso

The name is a list in Python

Python list index starts from Zero

So while loop display 0 to 4 element in the list (i<=4)

Program 2

Name=['Ram','Jhon','Muru','Nilo','Mackal','Yaso'];
i=0;
while(i<=5):
    print Name;
    i=i+1;

 

When the above code is executed, it produces the following results

['Ram', 'Jhon', 'Muru', 'Nilo', 'Mackal', 'Yaso']
['Ram', 'Jhon', 'Muru', 'Nilo', 'Mackal', 'Yaso']
['Ram', 'Jhon', 'Muru', 'Nilo', 'Mackal', 'Yaso']
['Ram', 'Jhon', 'Muru', 'Nilo', 'Mackal', 'Yaso']
['Ram', 'Jhon', 'Muru', 'Nilo', 'Mackal', 'Yaso']
['Ram', 'Jhon', 'Muru', 'Nilo', 'Mackal', 'Yaso']

 

In this program, Python uses the name as a single (list)variable

 

 

 

Suggested for you

Java while loop

C while loop

C++ while loop

Java While keyword

C++ wile keyword

C while keyword

Python while keyword

 

 

 

While loop in C programming language
While loop in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago