A while loop runs as long as the condition is True.
# infinite loop
while True:
print("hello world")Note
Do not create infinite loops unless needed — they will run forever (unstoppable).
Info
In real life, infinite loops are used in systems like servers, games, etc.
Question
Write a program in Python to print “hello world” five times
count = 1 # iterator
while count <= 5:
print("hello world")
count += 1output
hello world
hello world
hello world
hello world
hello worldDry Run Table
| count | condition (count ⇐ 5) | output |
|---|---|---|
| 1 | True | hello world |
| 2 | True | hello world |
| 3 | True | hello world |
| 4 | True | hello world |
| 5 | True | hello world |
| 6 | False | stop |
The loop stops when the condition becomes False. |
Question
Write a program in Python to print numbers from 1 to 5
count = 1
while count <= 5:
print(count)
count += 1output
1
2
3
4
5Question
Write a program in Python to print numbers from 5 to 1
count = 5
while count >= 1:
print(count)
count -= 1output
5
4
3
2
1Important
- Most loops start from
0(especially in programming)- Example:
range(0, n)- But you can start from any number depending on your need
Question
Write a program in Python to print the multiplication table of any number
n
n = int(input("enter a number: "))
i = 1
while i <= 10:
print(n, "x", i, "=", n * i)
i += 1output
enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50<< Python Loops | Python Break >>