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.
The syntax of while loop
The syntax of while loop in Python programming language is given below:
while expression: //body of while statement(s)
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)
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
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
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
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…