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 — for Loop

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

for Loop (Iteration)

The for loop iterates over items of any iterable (string, list, tuple, dict, range, set) — unlike C's counter-based for loop.

Syntax

for item in iterable:
    statement(s)
for ch in "Python":
    print(ch)

Output:

P
y
t
h
o
n

Using range()

range(start, stop, step) generates a sequence of numbers (stop is excluded).

for i in range(5):          # 0,1,2,3,4
    print(i)

for i in range(1, 6):       # 1,2,3,4,5
    print(i)

for i in range(10, 0, -2):  # 10,8,6,4,2
    print(i)

Iterating over a list

fruits = ["apple", "banana", "mango"]
for fruit in fruits:
    print(fruit)

Iterating with index — enumerate()

for index, fruit in enumerate(fruits):
    print(index, fruit)
# 0 apple
# 1 banana
# 2 mango

Iterating over a dictionary

student = {"name": "Riya", "age": 21}
for key in student:
    print(key, ":", student[key])

for key, value in student.items():
    print(key, "->", value)

for-else

The else block runs only if the loop completes without a break.

for i in range(5):
    print(i)
else:
    print("Loop finished normally")

Flowchart