While loop in Python

In this tutorial, we will learn while loop in Python with simple example programs.

If you have some lines of code and you want to run those lines again and again then we make use of loops. The While loop in Python is very similar to other languages with some syntactical changes but logically it’s the same thing. The while loop in any programming language iterate over a block of code as long as the condition specified in the loop is True.

Syntax: while loop in Python

while condition:
    Body of while loop

 

while is a keyword in Python.

The body of the while loop is entered if the condition is true. After one iteration the test expression(condition) is checked again. If the condition is false then it will not go on anymore, it will simply exit from the loop. If we do not increment/decrement the condition specified in the while loop then while loop will run infinite times. In the below example if I do not increase the value of index then this loop will run infinite times.

 

Examples of while loop

lst=[10,20,30,40,50]
product=1
index=0
while index<len(lst):
    product*=lst[index]
    index+=1
print("Product is {}".format(product))

Output:-

Product is:12000000

While Loop With Else in Python

While loop can also have else part with them. Now the question is when this else part will get executed. This else part will execute when the condition in the while loop becomes false. But one thing to remember here is that if we use break statement then this else part will not execute it will be ignored. So as a conclusion we can say that else part will get execute when the condition in the while becomes False and there is no breaking of while loop using break statement.

numbers=[1,2,3]
index=0
while index<len(numbers):
    print(numbers[index])
    index+=1
else:
    print('no item left in the list')

Output:-
1
2
3
no item left in the list

In the above example we can see first our index value is zero so condition in the while loop evaluates to true and we go inside the body of while loop and we print the first element of our numbers list then index value gets incremented by 1 in every iteration. When index value becomes 3 and len(numbers) is also three 3<3 condition false and we do not enter in the body of while loop and we can also see there is no breaking of while loop using break statement so our else part will get execute and we enter in the body of else part and print no item left in the list.

Python Program to check a given number is prime or not using while loop

num=int(input('Enter a number'))
isdivisible=True
i=2;
while(i<num):
    if num%i==0:
        isdivisible=True
        print("{} is divisible by {}".format(num,i))
    i+=1
if isdivisible:
    print("{} is not a prime number".format(num))
else:
    print("{} is a prime number".format(num))
Output:-
Enter a number-19
19 is a prime number

You can check: Check given number is prime or not in python

We know that prime numbers are those which are divisible by 1 or by themselves. So we can run a loop from 2 to n-1 where n is our number and if anyone number between 2 to n-1 divides that number we can directly break the loop here and we can say that the number is not prime if our loop no number from 2 to n-1 divides that number then we can say that number is a prime number. Taking example of 19 19 is not divisible by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18. So we can say that 19 is a prime number.

Leave a Reply

Your email address will not be published. Required fields are marked *