Nested Loops in Python - Class 11 Guide
What is a Loop?
A loop repeats a block of code. In Python, we use 'for' or 'while' to run loops.
Example:
for i in range(1, 6):
print(i) # Prints numbers 1 to 5
What is a Nested Loop?
A nested loop is a loop inside another loop.
Example:
for i in range(1, 4):
for j in range(1, 4):
print(i, "*", j, "=", i*j)
print("-----")
Output of Above Example
1*1=1
1*2=2
1*3=3
-----
2*1=2
2*2=4
2*3=6
-----
3*1=3
3*2=6
3*3=9
-----
Nested Loops in Python - Class 11 Guide
Tips and Tricks
1. Total Iterations = Outer loop count × Inner loop count.
2. Fix outer variable, vary inner loop to understand the flow.
3. Use print(i, j) to trace the loops.
Example Programs
1. Star Triangle:
for i in range(1, 5):
for j in range(1, i+1):
print("*", end=" ")
print()
2. Square Pattern:
for i in range(4):
for j in range(4):
print("#", end=" ")
print()
3. Multiples Table:
for i in range(1, 6):
for j in range(1, 6):
print(i * j, end="\t")
print()
Practice Challenges
1. Print:
12
123
Nested Loops in Python - Class 11 Guide
1234
2. Letters Triangle:
AB
ABC
ABCD
3. Hollow Square:
****
* *
* *
****
4. Two Line Numbers:
12345
12345
Summary Table
| Concept | Meaning |
|----------------|----------------------------------|
| Nested Loop | Loop inside another loop |
| Outer Loop | Runs first per full inner loop |
| Inner Loop | Runs completely per outer cycle |
| Total Iterations| Outer × Inner |