Some text some message..
Back 6 Loops in Python 🌀 20 Jun, 2025

🔁 What is a Loop?

A loop is used to repeat a block of code multiple times.


🧠 TYPES OF LOOPS IN PYTHON


🔹 1. for loop

Used to iterate over a sequence like a list, tuple, string, or range.

for i in range(5):
    print(i)

✅ Output: 0 1 2 3 4

✨ Tip: range(start, stop, step) is often used.


🔹 2. while loop

Repeats as long as a condition is True.

count = 0
while count < 5:
    print(count)
    count += 1

✅ Output: 0 1 2 3 4


🔹 3. nested loop

A loop inside another loop.

for i in range(2):
    for j in range(3):
        print(i, j)

✅ Output:

0 0
0 1
0 2
1 0
1 1
1 2

🚨 Loop Control Statements

Keyword Purpose
break Exits the loop completely 🚪
continue Skips the current iteration 🔁
pass Does nothing, just a placeholder 🧱

🧪 Example with break and continue

for i in range(5):
    if i == 3:
        break
    print(i)

✅ Output: 0 1 2

for i in range(5):
    if i == 3:
        continue
    print(i)

✅ Output: 0 1 2 4


🎯 When to Use Which Loop?

Use Case Loop Type
Know how many times to loop for loop
Loop until condition is met while loop
Iterate over a sequence for loop

🌈 VISUAL FLOWCHART OF LOOPING

Start 🔹
   ↓
[ Condition? ]
   ↓ Yes         ↓ No
[ Execute Block ] → End 🔚
   ↓
Repeat 🔁