Wednesday, June 12, 2019

Python while Loop With Examples

In Python programming language there are two loops for..in loop and while loop. In this tutorial you’ll learn about while loop in Python which is used to repeatedly execute the loop body while the given condition evaluates to true.


Syntax of Python while loop

Full syntax of while loop in Python is as given below.

while condition:
 while_suite
else:
 else_suite

Here condition is a boolean expression that evaluates to either true or false. Initially condition is checked and if it evaluates to true the statements making up the while_suite are executed. Control then goes back to the start of the loop and condition is checked again if it evaluates to true the statements making up the while_suite are executed again. Same process is followed repeatedly while the condition evaluates to true. When the condition evaluates to false control comes out of the while loop.

The else statement in the while loop is optional. The else_suite is executed if the while loop terminates normally and if the optional else statement is present.

while loop flow

Python while Loop

Python while loop examples

1- To display numbers 1..10 using while loop in Python.

i = 1
while i <= 10:
    print(i)
    i+=1

print('Out of while loop')

Output

1
2
3
4
5
6
7
8
9
10
Out of while loop

In the example i <= 10 is the condition for the while loop. While this condition is true loop body which are the following two indented statements are executed.

    print(i)
    i+=1

When value of i becomes greater than 10 control comes out of while loop.

2- Displaying table of 5 using while loop in Python.

i = 1
while i <= 10:
    print(5, 'X', i, '=', (i*5))
    i+=1

Output

5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50

Python while loop with else statement

In Python, while loop has an optional else statement too. If the while loop runs till completion and terminates the else-suite is executed.

If while loop doesn’t run completely because of any of the following reason, else statement is not executed.

  • while loop is terminated abruptly due to a break statement
  • while loop is terminated abruptly due to a return statement
  • if an exception is raised

In the following example else_suite is executed as the while loop completes normally.

i = 1
while i <= 10:
    print(i)
    i+=1
else:
    print('in else block')

Output

1
2
3
4
5
6
7
8
9
10
in else block

Now, if we break the while loop abruptly, else-suite won’t be executed.

i = 1
while i <= 10:
    print(i)
    i+=1
    if i > 5:
        break
else:
    print('in else block')

Output

1
2
3
4
5

Nested while loop in Python

while loop can be nested which means you can have one while loop inside another (or even for loop inside while loop). In the nested loops for each iteration of the outer while loop, inner while loop is iterated while the condition of inner while loop evaluates to true.

Python Nested while loop example

Suppose you want to print inverted right triangle using asterisk (*) symbol, following example shows one way to do it using nested while loops in Python.

rows = 6
while rows > 0:
    j = 1
    # inner while
    while j <= rows:
        # end='' to ensure print remains on the same line
        print('* ', end='')
        j += 1
    # for outer while
    rows = rows - 1
    print()

Output

* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

That's all for this topic Python while Loop With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Python Conditional Statement - if, elif, else Statements
  2. Python continue Statement With Examples
  3. Python First Program - Hello World
  4. Ternary Operator in Python
  5. Polymorphism in Python

You may also like-

  1. Local, Nonlocal And Global Variables in Python
  2. List Comprehension in Python With Examples
  3. Python Program to Check Whether String is Palindrome or Not
  4. Constructor Overloading in Java
  5. Private Methods in Java Interface
  6. Matrix Multiplication Java Program
  7. How to Sort an ArrayList in Descending Order in Java
  8. Sending Email Using Spring Framework Example

No comments:

Post a Comment