While loop in Python With Examples

Python Tutorial Series: Using while loops.

Rohit Kumar Thakur
4 min readOct 27, 2021
While loop in Python

Hello Python Enthusiasts, In my last article, we talked about Python for loop. In this article, we are going to deal with Python while loop.

Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad

When we are working with Python for loop, in that case, we already know the number of times we have to iterate the statements. For example, if I have to print out the sum of the first 100 natural numbers of the number system. Then, in this case, we already know that we have to iterate the statements 100 times to get the desired output. Python for loop takes care of this kind of problem.

At times, there may be a situation where the number of times, a sequence of statements has to be processed is not known in advance, but depends on the fulfilment of some condition. In such situations, we use a while loop.

For example, let’s suppose we are doing a summation problem in Python. We take input from the user. The input must be an integer. We wish to find the sum of all the numbers entered by the user until the number 7 is entered as input. In this problem, we don’t know in advance the count of numbers that the user will enter before entering the integer 7. Therefore we are going to use the while loop here. Now, let’s write the code:

total = 0
num = int(input("Enter the number: "))
while num != 7:
total = total+num
num = int(input("Enter the number: "))
print("Sum: ", total)

The output of the program is:

>>> Enter the number: 45
Enter the number: 45
Enter the number: 1
Enter the number: 7
Sum: 91
>>> Enter the number: 0
Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 7
Sum: 6

We initiate our program with a variable “total” and assign the value 0 to it. Then take the integer input from the user. Now in the while loop, we first stated a condition that (the number is not equal to 7). Every time the user enters an integer, its value is added to the old value of total. This process continues until the user enters integer 7 as input. On encountering integer 7, the condition becomes false, the while loop terminates, and the control moves to the print statement, where we display the value of total.

You can practice more questions here:

How to Write Cosine Function in Python

Compute the Expansion of e^x using Python

Least Common Multiple (LCM) in Python

Compute Sine Function using Python Programming

Find Armstrong Number In The Range of 1 To 1000 Using Python

General Format of while statement

while < condition >:
< Sequence of Statements >

Flow Diagram of while Structures

Flow diagram of while structure

while Statement vs for Statement

In for loop, we know the number of times the statements have to iterate in advance. But in the while loop, there is nothing like that. The while loop will continue until the condition becomes false. When the condition becomes false, the while loop terminates. We already see that in the example above.

If we talk about the summation of the first few natural numbers. The algorithms of for loop works in the following manners:

for i in range(1, n+1):
total +=i

And the while loop algorithm works something like this:

count = 1
while count < n+1:
total += count
count +=1

For this problem, the for loop looks more elegant. But in some problems, the while loop works more elegantly. It’s upon the developers to choose the most efficient one.

Well, That’s it for this article. In the next article, we will talk about Python break, continue, and pass statements.

If this article sounds informative to you then make sure to follow and share it with your geek community.

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).

More content at plainenglish.io

--

--