Nearby lessons

42 of 159

Python - While Loop

📌 What You Will Learn
  • Define the while loop and when it is used
  • Explain the flow of a while loop that checks the condition before each iteration
  • Write while loops to print numbers, even numbers, odd numbers and reverse counting
  • Identify why an infinite loop occurs when the loop variable is not updated
  • Apply the while loop when the number of iterations is not known

What is a while Loop?

The while loop executes a group of statements as long as the given condition is True.

When the condition becomes False, the loop stops.

Syntax of while Loop

🐍Code Cell
1while condition:
2 statement1
3 statement2
Output
No output captured.

Flow of while Loop

  1. Check the condition.
  2. If the condition is True, execute the loop body.
  3. Again check the condition.
  4. Repeat until the condition becomes False.
  5. When the condition becomes False, control comes outside the loop.

Example 1 - Print "Hello" 10 Times

🐍Code Cell
1i = 1
2 
3while i <= 10:
4 print("Hello")
5 i = i + 1
Output
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

Explanation

The variable i starts from 1.

After every iteration, its value increases by 1.

The loop stops when i becomes greater than 10.

Example 2 - Print Numbers from 1 to 10

🐍Code Cell
1x = 1
2 
3while x <= 10:
4 print(x)
5 x = x + 1
Output
1
2
3
4
5
6
7
8
9
10

Example 3 - Print Numbers from 1 to 20

🐍Code Cell
1x = 1
2 
3while x <= 20:
4 print(x)
5 x = x + 1
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Example 4 - Print Even Numbers from 1 to 20

🐍Code Cell
1x = 2
2 
3while x <= 20:
4 print(x)
5 x = x + 2
Output
2
4
6
8
10
12
14
16
18
20

Explanation

The loop starts from 2.

Each time, the value increases by 2, so only even numbers are printed.

Example 5 - Print Odd Numbers from 1 to 20

🐍Code Cell
1x = 1
2 
3while x <= 20:
4 print(x)
5 x = x + 2
Output
1
3
5
7
9
11
13
15
17
19

Explanation

The loop starts from 1.

Each time, the value increases by 2, so only odd numbers are printed.

Example 6 - Print Numbers from 10 to 1

🐍Code Cell
1x = 10
2 
3while x >= 1:
4 print(x)
5 x = x - 1
Output
10
9
8
7
6
5
4
3
2
1

Explanation

The loop starts from 10.

After every iteration, the value decreases by 1.

The loop stops when the value becomes less than 1.

Infinite while Loop

If the condition is always True, the loop runs forever.

Such a loop is called an Infinite Loop.

Example - Infinite while Loop

🐍Code Cell
1while True:
2 print("Hello")
Output
Hello
Hello
Hello
Hello
...

(The loop continues until it is stopped manually.)

Why Does an Infinite Loop Occur?

An infinite loop happens when the loop condition never becomes False.

This usually happens when the loop variable is not updated correctly.

📝 Key Takeaways
  • The while loop executes statements as long as the condition stays True
  • When the condition becomes False, the loop stops
  • The condition is checked before each iteration
  • An infinite loop occurs when the loop condition never becomes False
  • The while loop is useful when the number of iterations is not known

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3