logo

Python While Loop


Show

A while loop statement in the Python programming language frequently performs an objective statement as long as an agreed state is true.

Syntax

The syntax of a though loop in Python programming language is:

while expression:
   statement(s)

Here, statement(s) might be a particular statement or a chunk of statements. The state may be any face, and true is several non-zero values. The loop iterates when the situation is true.

When the circumstance grows to be false, plan control overtakes the procession immediately subsequent to the loop.

In Python, entire statements aligned by a similar number of makeup spaces following a programming build are considered to be a fraction of a lone block of code. Python employs notch as its technique of grouping statements.

Flow Diagram

Here, the key end of the thought loop is that the circle strength does not ever lope. When the situation is tested and the effect is false, the loop remains will be bounced and the original statement subsequent to the while loop will be performed.

Example

#!/usr/bin/python

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

print "Good bye!"

When the above code is executed, it produces the following result:

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
Good bye!

The chunk here, comprising of the print and increase reports, is executed frequently until the count up is no longer less than nine. With every iteration, the present value of the directory count is showed and then augmented by 1.

The Infinite Loop

A loop turned into an unlimited loop if a form never turns out to be FALSE. You ought to use concern when utilizing while loops since of the option that this state never determines to a FALSE value. This consequences a loop that by no means ends. Such a loop is recognized as an infinite loop.

Never-ending loop strength is helpful in client/server programming where the server requires to run incessantly so that client programs can converse with it as and when necessitated.

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

print "Good bye!"

When the above code is executed, it produces the following result:

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 
      num = raw_input("Enter a number :")
KeyboardInterrupt

The above example goes in an infinite loop and you need to use CTRL+C to exit the program.

Using else Statement with Loops

Python reinforces to possess an else statement connected with a loop statement.

  • If besides statement is adopted with a loop, the moreover statement is performed when the loop has drained iterating the list.
  • If the else statement is utilized with a while loop, the besides statement is implemented when the form develops into false.

The next example exemplifies the grouping of an else declaration with the statement that prints a figure as long as it is fewer than 5, or else the statement gets executed.

#!/usr/bin/python

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

When the above code is executed, it produces the following result:

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 Statement Suites

Alike to the if report syntax, if your whilst article consists only of a particular statement, it may be positioned on a similar line as the while header.

Here is the syntax and example of a one-line while clause:

#!/usr/bin/python

flag = 1
while (flag): print 'Given flag is really true!'
print "Good bye!"

It is better not to try the above example because it goes into an infinite loop and you need to press CTRL+C keys to exit.

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.