While loop in Python programming language
- Home
- Loop
- While loop
- While loop in Python programming language
- On
- By
- 0 Comment
- Categories: 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.
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
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
Similar post
If statement in Python language
Suggested for you
Nested for loop in C++ language
Nested while loop in C++ language
Nested for loop in Java language
Nested while loop in Java language
Three dim Array in C++ language
Single dim Array in Java language
Two dim Array in Java language
Three dim Array in Java language
Single dim Array in C language