Python Basics with WHILE loop

Python Basics with WHILE loop

In Python, the WHILE loop is used to repeatedly execute a block of code as long as a specified condition is true. The syntax for a while loop is as follows:

while condition:
statement(s)

The condition is evaluated before each iteration of the loop. If the condition is true, the statements inside the loop are executed. This continues until the condition becomes false, at which point the loop exits and control is transferred to the next statement after the loop.

Here's an example of a simple WHILE loop that prints the numbers from 1 to 5:

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

In this example, the initial value of num is set to 1. The condition num <= 5 is true, so the statement inside the loop (print(num)) is executed. Then, the value of num is incremented by 1 using the += operator. This process continues until num becomes 6, at which point the condition num <= 5 becomes false and the loop exits.

I hope that helps! Let me know if you have any other questions.

Python Basics with WHILE loop

Facebook Reviews:

If you are a training provider

Interested to offer our courses in your own platform with Life-time Resale License?