We now have a youtube channel. Subscribe!

Python While Loop Tutorial with examples

Hello dear readers! Welcome back to another edition of our tutorial on Python. In this tutorial guide, we are going to be discussing about the Python While Loop statement.

A while loop statement in Python will repeatedly execute a target statement provided the condition given is true.


The following below is the syntax of a Python while loop statement -

while expression:

Here, statement(s) may be single statement or block of statements. The condition that is given may be any expression, and true is any non-zero values. The loop iterates while the condition is true.

When the condition that is given becomes false, then the program control passes to the line after the loop.

In Python, all the statements being indented by the same number of character spaces after the Python program construct are regarded to be part of a single block of code. Python uses indentation as its method of grouping statements.

Flow Chart

Here, the key point of the while loop is that the loop might not ever run. When the condition is tested and the result returns false, the body of the loop will be skipped and the first statement that is after the while loop will be executed.


Below is a very short example -


count = 0
while (count < 9):
   print 'The count is:', count
   count = count + 1

print "Welcome to Web design tutorialz!"


Below is the output of the above example -

The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Welcome to Web design tutorialz!

Block here, consisting of a print and an incremental statements, is executed repeatedly until count is no longer less than 9. With each iteration, the current value of the index count is displayed and then increased by 1.

The Python Infinite Loop

A loop becomes an infinite loop if a condition never becomes false. You ought to apply caution when making use of Python while loops because of the possibility that the condition might never resolve to a FALSE value. This results to a loop that will never end.

An infinite loop might be helpful in client/server programming where server will need to continuously run so that client programs can easily communicate with it when need be.


Below is a short example -


var = 1
while var == 1 :  # This constructs an infinite loop
   num = raw_input("Enter a number  :")
   print "You entered: ", num

print "Welcome to Web design tutorialz!"


The following below is the output of the above example -

Enter a number  :20
You entered:  20
Enter a number  :29
You entered:  29
Enter a number  :3
You entered:  3
Enter a number between :Traceback (most recent call last):
   File "test.py", line 5, in <module>
      num = raw_input("Enter a number :")

Note: When an infinite loop is been encountered, you will need to use CTRL + C to exit the program.

Using else Statements with Loops

Python has support for the feature of having a Python else statement to be associated with a loop statement.

  • If the else statement is used with a for loop, the else statement is executed when the loop finish iterating the list.
  • If the else statement is used with a while loop, the else statement will be executed when the condition becomes false.


The example that we have below illustrates the combination of an else statement with a while statement that prints a number as long as it is less than 5, other wise the else statement get executed-


count = 0
while count < 5:
   print count, " is  less than 5"
   count = count + 1
   print count, " is not less than 5"


The following below is the output of the above example -

0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5

Single Statements Suites
Similar to If statement syntax, if your while loop consist only of a single statement, it may be put on the same line as the while header.

The following below is the syntax and example of a one-line while clause -


flag = 1
while (flag): print 'Given flag is really true!'
print "Welcome to Web design tutorialz!"

Note: It is better not to try the above example because it will go into an infinite loop and you need to press the CTRL + C key to exit.

Alright guys! This is where we are rounding up for this tutorial. In our next tutorial, we will be discussing about the Python For Loops.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain