Tuesday , December 1 2026
Python Loops - For Loop, While Loop and Nested Loops

Python Loops: for and while Loops with Examples

Updated: 28 November 2026Level: BeginnerReading time: about 10 minutesPython 3.8+

A loop repeats a block of code. Python has two loops: for, which goes through the items of a sequence, and while, which repeats as long as a condition is True.

What You Will Learn

  • for loops with range(), lists and strings
  • while loops
  • break, continue and pass
  • enumerate(), zip() and nested loops

Prerequisites

Python Decision Making.

for Loop

Example
fruits = ['apple', 'banana', 'mango']
for fruit in fruits:
    print(fruit)
Output
apple
banana
mango

range()

Example
for i in range(5):
    print(i, end=' ')
print()
for i in range(2, 11, 2):
    print(i, end=' ')

Explanation: The stop value is never included.

Call Produces
range(5) 0 to 4
range(2, 6) 2 to 5
range(2, 11, 2) 2, 4, 6, 8, 10
range(5, 0, -1) 5 down to 1
Output
0 1 2 3 4 
2 4 6 8 10 

while Loop

Example
count = 1
while count <= 5:
    print(count)
    count += 1
Output
1
2
3
4
5
Note: If the condition never becomes False you get an infinite loop. Press Ctrl+C (or Interrupt in Jupyter) to stop it.

break and continue

Example
for n in range(1, 10):
    if n == 3:
        continue   # skip 3
    if n == 6:
        break      # stop at 6
    print(n)
Output
1
2
4
5

else with Loops

Example
for n in [3, 5, 7]:
    if n % 2 == 0:
        print('Found even')
        break
else:
    print('No even number')

Explanation: The else block runs only if the loop finished without break.

Output
No even number

pass

Example
for i in range(3):
    pass   # placeholder, does nothing

enumerate() and zip()

Example
names = ['Asha', 'Ravi']
marks = [88, 92]
for i, name in enumerate(names, start=1):
    print(i, name)
for name, m in zip(names, marks):
    print(name, m)
Output
1 Asha
2 Ravi
Asha 88
Ravi 92

Nested Loops

Example
for i in range(1, 4):
    for j in range(1, 4):
        print(i * j, end='\t')
    print()
Output
1	2	3	
2	4	6	
3	6	9	

Looping Over a Dictionary

Example
student = {'name': 'Asha', 'age': 21}
for key, value in student.items():
    print(key, '->', value)
Output
name -> Asha
age -> 21

Common Errors

Problem Cause and fix
Infinite while loop Make sure the loop variable changes so the condition becomes False.
RuntimeError: dictionary changed size during iteration Loop over a copy: for k in list(d):

Practice Exercises

  1. Print the multiplication table of any number.
  2. Sum the numbers 1 to 100 with a while loop.
  3. Print all prime numbers below 50.
  4. Count the vowels in a string.

Interview Questions

Q: When does a loop else run?
When the loop ends without break.

Q: for vs while?
for iterates over a known sequence; while repeats until a condition is False.

Summary

Use for with range(), lists and dicts, while for condition-based repetition, and break/continue to control the flow.

Next: Python Numbers · Python Lists

About Machine Learning

Check Also

Python Inheritance: super(), Method Overriding and MRO

Python inheritance with super(), method overriding, isinstance, multiple inheritance and the method resolution order.