Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1 — Nested Loops

Lesson 22 of 50 in the free Python Programming notes on Siksha Sarovar, written by Rohit Jangra.

Nested Loops

A nested loop is a loop inside another loop. The inner loop completes all its iterations for every single iteration of the outer loop.

for i in range(1, 4):
    for j in range(1, 4):
        print(f"i={i}, j={j}")

Output:

i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
i=2, j=2
i=2, j=3
i=3, j=1
i=3, j=2
i=3, j=3

Example: Multiplication table (1–5)

for i in range(1, 6):
    for j in range(1, 11):
        print(i * j, end="\t")
    print()   # newline after each row

Example: Star pattern (right triangle)

rows = 5
for i in range(1, rows + 1):
    for j in range(i):
        print("*", end="")
    print()

Output:

*
**
***
****
*****

Example: Number pyramid

rows = 4
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()

Output:

1
1 2
1 2 3
1 2 3 4

while inside for (mixed nesting)

for i in range(3):
    j = 0
    while j < 2:
        print(f"outer={i} inner={j}")
        j += 1

Time complexity note

Nested loops running n times each cost O(n²) — important to remember when analyzing performance of pattern-printing or matrix programs.