AppDividend
Latest Code Tutorials

Python While Loop Example | Loops in Python Tutorial

0

Python While Loop Example | Loops in Python Tutorial is today’s topic. With the use of while loop, we can execute the set of statements as long as the condition is true. A while loop tells a computer to do something as long as the condition is met or holds true. Its construct consists of the block of code and the condition. The condition is evaluated, and if the condition is true, the code within the block is executed. It repeats until the condition becomes false.

Python While Loop Example

Loops are used in the Python programming to repeat the specific block of code. In this post, you will learn to create the while loop in Python. 

A while loop in Python is used to iterate over the block of code as long as the test condition) is true. See the following syntax of while loop. We generally use a while loop when we don’t know, the number of times to iterate.

The syntax of while Loop in Python

See the following syntax of while loop.

while test_expression:
    { body of while }

In while loop, the test expression is checked first. The body of a loop is entered only if a test_expression evaluates to True.

After one iteration, the test expression is rechecked. This process continues until the text_expression evaluates to False. Remember, we need to change the value somehow otherwise it will go to an infinite loop.

In the Python, a body of a while loop is determined by indentation. Body starts with an indentation, and the first unindented line marks the end. Python interprets any non-zero value to TrueNone and 0 are interpreted as False.

See the following example of while loop in Python.

# app.py

i = 21
while i < 29:
  print(i)
  i += 1

 

Python While Loop Example

In the above program, the test expression will hold as long as our variable i is less than or equal to 29 in our program starting from 21. We need to increase the value of i variable in the body of the loop. It is essential and mostly forgotten. If we failing to do so will result in an infinite loop or never-ending loop. Finally, the result is displayed.

while loop with else

Let’s take an example of while loop with else in Python. 

If you have used Python For Loop then in that we have else block same here as we can have an optional else block with the while loop as well.

The else part is executed if the condition in a while loop evaluates to False.

A while loop can be stopped with the break statement. In such a case, the else part is ignored. Hence, the while loop’s else part runs if no break occurs and the condition is false.

Here is an example.

# app.py

counter = 0

while counter < 3:
    print(counter)
    counter = counter + 1
else:
    print('Executed Else Statement')

In the above code, unless the condition is false, the counter is printed. After the condition is false, the else block is executed. See the output below.

 

while loop with else

The break Statement in Python

With the break statement, we can stop the loop even if the while condition is true.

# app.py

i = 1
while i < 5:
  print(i)
  if i == 3:
    break
  i += 1

See the output below.

 

The break Statement in Python

Finally, Python While Loop Example | Loops in Python Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.