Python While Loop

    In this tutorial, we will discuss one more loop which is while loop Python, as like for loop while loop also used to repeat a block of code. In this article, you will code and learn all the concepts of while loop. so let's discuss Python While Loop.

    Python While Loop

    Like for loop while loop is used to repeat a block of code or statements. Unlike for loop while does not need any sequence to iterate, rather than it works on Boolean values like True and False . A while loop has two parts one is an expression that should be a Boolean value (True or False) or return a Boolean value and the second is its body. The while loop continues to execute its body or block of code until its expression turns False . In case if the expression does not turn False the body of the while loop executes for infinite times.

    The syntax of a While Loop.

    while expression:
        statement() or While body

    First, the python interpreter checks the while loop expression if the expression state True the while body execute one time, after executing the complete body of the while loop the interpreter again come to the top of the while loop and again check its expression and if it finds the expression True its goanna executes the whole body again and again until the expression become False . Python treats everything True except for 0, False, empty object (string, list, tuple, dictionary, set) and None.

    Let’s understand while loop with an example:

    number = 10
    while number:
        print("number value inside while loop",":" ,number)
        number = number -1
    print("the value of number outside the while loop: ", number)   

    Output

    number value inside while loop: 10
    number value inside while loop: 9
    number value inside while loop: 8
    number value inside while loop: 7
    number value inside while loop: 6
    number value inside while loop: 5
    number value inside while loop: 4
    number value inside while loop : 3
    number value inside while loop: 2
    number value inside while loop: 1
    the value of number outside the while loop:  0

    in the above example, you can see that the moment the value of the number turns 0, the python interpreter treats it as False and we get out of the while loop.

    else statement with while loop

    Like if and for loop statements you can also use else statement with the while loop. The else statement blocks of code or body will automatically execute when the expression of while loop becomes False .

    The syntax of while loop with else:

    while expression:
        while loop body or statements()
    else:
        else body or statement()

    Apparently, the execution of else statements depends on the Turning False of while expression.

    Let’s understand it with an example:

    true=True
    number = 10
    while true:
        if number < 4:
            true = False
        print(number)
        number = number-1
    
    else:   #this else is with the while loop
        print("the value of true becomes False and we are out of the while loop")

    Output

    10
    9
    8
    7
    6
    5
    4
    3
    the value of true becomes False and we are out of the while loop

    In the above example, we have used the if statement inside the while loop. The moment, the value of the number becomes less than 4 the if statement of the while loop is executed and the value of the expression true become False which terminates while and throws us out of the while loop. After the expression becomes False the else statement which is corresponded to the while loop, executes.

    Points to remember

    • There should be a logic inside the while loop body that can make the expression False otherwise the while loop executes infinite time and form an infinite loop.
    • The while loop in Python treats its expression as a Boolean value
    • Like if and for statements you can use else statement with while loop