What is a while loop in Python?

Python Tutorials (31 Part Series)

1 How to use ‘not equal’ operator in Python?
2 How to use exponents in Python?
27 more parts…
3 How to check if file exists in Python?
4 How to represent an infinite number in Python?
5 Various ways to sum numbers in Python
6 Python – pass by reference
7 4 Ways to implement Python Switch Case Statement
8 How to create a list of lists in Python?
9 Python regex: How to search and replace strings
10 How to copy a list in Python?
11 Python rename file: How to rename a file in Python
12 How to calculate the length of an array in Python?
13 How to use Open() in Python?
14 How to sort dictionary by value in Python?
15 How to get Timestamp in Python?
16 How to block a comment in Python?
17 How does return() in Python work?
18 Python constructors – How to use them?
19 Python append to string – How is it done?
20 Python list contains: How to check if an item exists in list?
21 Python Initialize List – How to do it?
22 How to print a list in Python?
23 Python remove file – How to delete a file?
24 What are Identifiers in Python?
25 Python Reserved Words List – Your Complete Guide
26 Different Arithmetic operators in Python
27 Different Comparison operators in Python
28 Different Assignment operators in Python
29 What are the Control Statements in Python?
30 Looping Statements in Python
31 What is a while loop in Python?

Table of Content

While loop in Python

The while loop in python is used to iterate over a block of code till the given condition is True. And as soon as the condition turns False, the next statement immediately after the loop is executed. It comes under indefinite iteration.

Indefinite iteration means that the number of times the loop is supposed to be executed is not specified in advance.

Syntax of while loop in Python:

while expression:
statement(s)
while expression:
       statement(s)
while expression: statement(s)

Enter fullscreen mode Exit fullscreen mode

The expression is executed in the Boolean context and if it is true, then the statement is executed. The expression is checked again when the loop is executed and will continue this process until the expression returns False.

Input:

count = 0
while (count < 5):
count = count + 1
print("Flexiple")
count = 0
while (count < 5):
    count = count + 1
    print("Flexiple")
count = 0 while (count < 5):     count = count + 1     print("Flexiple")

Enter fullscreen mode Exit fullscreen mode

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple Flexiple Flexiple Flexiple Flexiple

Enter fullscreen mode Exit fullscreen mode

The expression count<5 is True till count=4 but as soon as the value of count becomes 5, the expression is not satisfied and the loop ends.

Single statement

Similar to the syntax of the if statement, if the while block consists of a single statement then it may be declared in the same line as the while header. And if there are multiple statements, then each statement can be separated by semicolons.

Input:

count = 0
while (count < 5): count += 1; print("Flexiple")
count = 0
while (count < 5): count += 1; print("Flexiple")
count = 0 while (count < 5): count += 1; print("Flexiple")

Enter fullscreen mode Exit fullscreen mode

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple Flexiple Flexiple Flexiple Flexiple

Enter fullscreen mode Exit fullscreen mode

While loop with else statement in Python

Python also allows programmers to use the else statement within the while loop. Like in the for loop, if the expression becomes false in the while loop, the else block is executed.

Input:

count = 0
while (count < 5):
count = count + 1
print("Flexiple")
else:
print("The else statement is executed.")
count = 0
while (count < 5):
    count = count + 1
    print("Flexiple")

else:  
    print("The else statement is executed.")   
count = 0 while (count < 5):     count = count + 1     print("Flexiple") else:       print("The else statement is executed.")   

Enter fullscreen mode Exit fullscreen mode

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
The else statement is executed.
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
The else statement is executed.
Flexiple Flexiple Flexiple Flexiple Flexiple The else statement is executed.

Enter fullscreen mode Exit fullscreen mode

If the break control statement is used with the while loop, then the else statement will not be executed. As soon as the condition becomes false, the break statement will ignore the else statement and end the loop and the statement after the loop will be executed.

Input:

count = 0
while (count < 5):
count = count + 1
print("Flexiple")
break
else:
print("The else statement is executed.")
count = 0
while (count < 5):
    count = count + 1
    print("Flexiple")
    break

else:  
    print("The else statement is executed.")   
count = 0 while (count < 5):     count = count + 1     print("Flexiple")     break else:       print("The else statement is executed.")   

Enter fullscreen mode Exit fullscreen mode

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Flexiple Flexiple Flexiple Flexiple Flexiple

Enter fullscreen mode Exit fullscreen mode

Infinite while loop in Python

If the expression in the while loop never becomes False, then the loop will never end and it becomes an infinite while loop.

Input:

count = 1
while (count > 0):
count = count + 1
print("Flexiple")
count = 1
while (count > 0):
    count = count + 1
    print("Flexiple")
count = 1 while (count > 0):     count = count + 1     print("Flexiple")

Enter fullscreen mode Exit fullscreen mode

Output:

Flexiple
Flexiple
Flexiple...
Flexiple
Flexiple
Flexiple...
Flexiple Flexiple Flexiple...

Enter fullscreen mode Exit fullscreen mode

Since the value of count will always be greater than 0, this loop will never end and will keep printing ‘Flexiple’ infinitely.

Closing Thoughts

In this tutorial, we read about the while loop in Python and various situations where the while loop can be used. We can use the control statements with the while loop to change how the loop behaves. One can read about other Python concepts here.

Python Tutorials (31 Part Series)

1 How to use ‘not equal’ operator in Python?
2 How to use exponents in Python?
27 more parts…
3 How to check if file exists in Python?
4 How to represent an infinite number in Python?
5 Various ways to sum numbers in Python
6 Python – pass by reference
7 4 Ways to implement Python Switch Case Statement
8 How to create a list of lists in Python?
9 Python regex: How to search and replace strings
10 How to copy a list in Python?
11 Python rename file: How to rename a file in Python
12 How to calculate the length of an array in Python?
13 How to use Open() in Python?
14 How to sort dictionary by value in Python?
15 How to get Timestamp in Python?
16 How to block a comment in Python?
17 How does return() in Python work?
18 Python constructors – How to use them?
19 Python append to string – How is it done?
20 Python list contains: How to check if an item exists in list?
21 Python Initialize List – How to do it?
22 How to print a list in Python?
23 Python remove file – How to delete a file?
24 What are Identifiers in Python?
25 Python Reserved Words List – Your Complete Guide
26 Different Arithmetic operators in Python
27 Different Comparison operators in Python
28 Different Assignment operators in Python
29 What are the Control Statements in Python?
30 Looping Statements in Python
31 What is a while loop in Python?

原文链接:What is a while loop in Python?

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
I'm not lazy, I'm on energy saving mode.
我不懒,我只是开启了节能模式
评论 抢沙发

请登录后发表评论

    暂无评论内容