While Loop in Python

A while loop is used to repeat a block of code as long as a condition remains true.

🔁 Used when the number of iterations is not known in advance.

Basic Syntax

i = 1

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

while Loop with Condition

number = 1

while number <= 10:
    print(number)
    number += 2

Infinite Loop

❌ Be careful! Infinite loops never stop.
while True:
    print("Hello")

Practice

  1. Print numbers from 1 to 20
  2. Print even numbers using while loop