Lesson 22 of 70 – Python While Loop
31%

Python While Loop

A while loop is used to repeatedly execute a block of code as long as a specified condition is True.

Note: A while loop is useful when we do not know in advance exactly how many times the loop should execute.
What is a While Loop?

A while loop checks a condition before every iteration. If the condition is True, the loop body executes.

while condition:
    statement

When the condition becomes False, the loop stops.

Simple While Loop
i = 1

while i <= 5:
    print(i)
    i = i + 1
Output:
1
2
3
4
5
How While Loop Works
  1. Python checks the condition.
  2. If the condition is True, the loop body executes.
  3. The variable is updated.
  4. The condition is checked again.
  5. The process continues until the condition becomes False.
While Loop with Counter
count = 1

while count <= 3:
    print("Hello")
    count += 1
Output:
Hello
Hello
Hello
Print Numbers Using While Loop
number = 1

while number <= 10:
    print(number)
    number += 1
Output:
1
2
3
4
5
6
7
8
9
10
Print Even Numbers
number = 2

while number <= 10:
    print(number)
    number += 2
Output:
2
4
6
8
10
Print Odd Numbers
number = 1

while number <= 10:
    print(number)
    number += 2
Output:
1
3
5
7
9
Countdown Using While Loop
number = 5

while number >= 1:
    print(number)
    number -= 1
Output:
5
4
3
2
1
While Loop with Else

Python allows an else block with a while loop. The else block executes when the loop ends normally.

i = 1

while i <= 3:
    print(i)
    i += 1
else:
    print("Loop completed")
Output:
1
2
3
Loop completed
While Loop with Condition
number = 1

while number <= 10:

    if number % 2 == 0:
        print(number)

    number += 1
Output:
2
4
6
8
10
While Loop with User Input
number = int(input("Enter a number: "))

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

If the user enters 2:

Output:
2
3
4
5
Sum of Numbers Using While Loop
number = 1
total = 0

while number <= 5:
    total += number
    number += 1

print(total)
Output:
15

The loop calculates: 1 + 2 + 3 + 4 + 5 = 15.

Multiplication Table Using While Loop
number = 5
i = 1

while i <= 10:
    print(number * i)
    i += 1
Output:
5
10
15
20
25
30
35
40
45
50
While Loop with String
name = "Python"
i = 0

while i < len(name):
    print(name[i])
    i += 1
Output:
P
y
t
h
o
n
While Loop with List
names = ["Amit", "Rahul", "Priya"]

i = 0

while i < len(names):
    print(names[i])
    i += 1
Output:
Amit
Rahul
Priya
Infinite While Loop

An infinite loop occurs when the condition never becomes False.

i = 1

while i <= 5:
    print(i)

The value of i never changes, so the condition remains True.

Warning: Always make sure that a while loop has a way to eventually make its condition False, unless an infinite loop is intentionally required.
Updating a Variable

Updating the loop variable is one of the most important parts of a while loop.

i = 1

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

Here, i = i + 1 increases the value of i after every iteration.

Using break in While Loop

The break statement immediately stops the loop.

i = 1

while i <= 10:

    if i == 5:
        break

    print(i)
    i += 1
Output:
1
2
3
4
Using continue in While Loop

The continue statement skips the remaining code of the current iteration and moves to the next iteration.

i = 0

while i < 5:

    i += 1

    if i == 3:
        continue

    print(i)
Output:
1
2
4
5
Nested While Loop

A while loop can be placed inside another while loop. This is called a nested while loop.

i = 1

while i <= 2:

    j = 1

    while j <= 3:
        print(i, j)
        j += 1

    i += 1
Output:
1 1
1 2
1 3
2 1
2 2
2 3
Simple Password Example
password = ""

while password != "1234":

    password = input("Enter password: ")

print("Login successful")

The loop continues until the correct password is entered.

Menu Example
choice = 1

while choice != 0:

    print("1. Continue")
    print("0. Exit")

    choice = int(input("Enter choice: "))

print("Program ended")

This type of loop can be used to keep a program running until the user chooses an exit option.

While Loop with Comparison
number = 100

while number > 10:
    print(number)
    number -= 10
Output:
100
90
80
70
60
50
40
30
20
Difference Between For and While Loop
For Loop While Loop
Used to iterate over a sequence. Runs while a condition is True.
Often used when the number of iterations is known. Useful when the number of iterations depends on a condition.
Works naturally with lists, strings, ranges, etc. Usually requires a condition and variable update.
Important: Indentation

Like other Python control structures, while loops use indentation to define the loop body.

i = 1

while i <= 3:
    print(i)
    i += 1
Incorrect:
i = 1

while i <= 3:
print(i)
The loop body must be properly indented.
Key Points
  • A while loop executes code while a condition is True.
  • The condition is checked before each iteration.
  • The loop stops when the condition becomes False.
  • The loop variable should usually be updated.
  • Forgetting to update the variable can create an infinite loop.
  • break can stop a while loop immediately.
  • continue skips the current iteration.
  • A while loop can have an else block.
  • While loops can be nested.
  • Proper indentation is required.

🧠 Quick Quiz

Question: When does a while loop stop executing?