While Loop in Python

The while loop in Python is a control flow statement that repeats a block of code as long as a specified condition is true. It’s useful when the number of iterations is not known beforehand.

Key Points on While Loop:

Syntax of While Loop:

Syntax Example

while condition:
            # Code to execute in each iteration

Example of While Loop in Python:

This example demonstrates a basic while loop that prints numbers from 1 to 5.

Code Example

count = 1
        while count <= 5:
            print(count)
            count += 1

Output

1
2
3
4
5

Detailed Explanation:

Mastering the while loop helps in creating flexible loops that adapt to runtime conditions in Python programs.