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 += 1

output

hello world
hello world
hello world
hello world
hello world

Dry Run Table

countcondition (count 5)output
1Truehello world
2Truehello world
3Truehello world
4Truehello world
5Truehello world
6Falsestop
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 += 1

output

1
2
3
4
5

Question

Write a program in Python to print numbers from 5 to 1

count = 5
 
while count >= 1:
    print(count)
    count -= 1

output

5
4
3
2
1

Important

  • 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 += 1

output

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 >>