2.4.2 Looping Constructs
Python Programming
Loops help us repeat actions, making our code more efficient and easier to read. There are two main types of loops in Python: while loops and for loops.
2.4.2.1 while Loop
A while loop runs as long as a condition is true. It checks the condition before each iteration and stops running when the condition is no longer true.
python
# Syntax of while loop while condition:
# code to run while the condition is trueExample: Add 1 to a number until it reaches 10.
python
number = 1
while number < 10:
print(number)
number += 1