Skip to content
Zeni Study
  • Pages
    • Learn Python In 7 Days
      • Day 1 : Variable + if/else
      • icon picker
        Day 2: Loops
      • Day 3 : Array + String
      • Day 4 — Functions
      • Day 5 — Dictionaries - Hashmap
      • Day 6 — Files & Errors
      • Day 7 — Mini Project & Programmer Thinking
    • Machine Coding:
      • Day 1 — Bank account + Wallet :
      • Day 2 : RateLimiter + seat booking
      • Day 3 : Library Management + Classroom Attendance System
      • Day 4 : Bank account and notification system.
      • Day 5 : Key-value and payment processor.
      • Day 6 : Discount engine + Seat booking( in depth)
      • Day 7: Parking lot System :
    • Machine coding extended:
      • Day 7.5 — Parking Lot (Constraint-Heavy Variant)
      • Day 8 : Order + Payment System
      • Day 8 — Extension Round (Mid-Interview Change)
      • Day 9 — Ticket Booking with Expiry (Simulated Time)
      • Day 9 — Extension Round (Seat Lock Ownership + Forced Unlock)
    • SQL + DB Thinking course
      • Day 1 — Tables, Rows & Thinking in Data
      • Day 2 — Relationships & Foreign Keys (Thinking in Connections)
      • Day 3 — Filtering, Aggregation & Answering Data Questions
      • Day 4 — JOINs (Combining Tables the Right Way)
      • Day 5 — Subqueries, EXISTS & NOT EXISTS (Thinking in Layers)
      • Day 6 — Indexes, Constraints & How Databases Think
      • Day 7 — Full SQL Interview Simulation (SDE-1 Ready)
      • Extended Day 1 — Core Business Systems (Deep Practice)
      • Extended Day 2 — Booking & Platform Systems (High-Depth Practice)
      • Extended Day 3 — Content & Learning Platforms (Capstone Practice)
    • LLD: Java
      • DAY 1 — Core Foundations (SDE-2 Level)
      • DAY 2 — State, Time & Domain Correctness
      • DAY 3 — Extensibility & Failure Handling (SDE-2 Core Signal)
      • DAY 4 — Concurrency, Scheduling & Consistency (SDE-2 Critical)
      • DAY 5 — Workflow Design & Config-Driven Systems (SDE-2+)
      • DAY 6 — Eventing & Usage-Based Systems (Advanced SDE-2)
      • DAY 7 — Interview Simulation (Hard, Production-Grade)
      • Hidden Edge Cases Interviewers Expect (Day-wise)
      • Detailed Testing file:
    • Assignment:
      • If else Practice Set
Share
Explore

Day 2: Loops

🟢 Day 2 — Repeating Work (Loops & Logical Flow)

1️⃣ Why Do We Need Loops?

Imagine this task:
Print numbers from 1 to 100.

Option 1 (Not Possible)

print(1)
print(2)
print(3)
...
print(100)
❌ This is not programming. This is typing.

Real-World Example

Washing 10 plates
Attendance for 50 students
Sending reminders to 100 users
💡 Whenever same work repeats, we need a loop.

2️⃣ How Repetition Works in Real Life

Think like this:
“Start from 1 Keep increasing by 1 Stop when you reach 10”
That is exactly how a loop works.

3️⃣ for Loop — When You Know How Many Times

Basic Syntax

for i in range(1, 6):
print(i)

How Computer Thinks (VERY IMPORTANT)

Line-by-line:
range(1, 6) generates numbers → 1, 2, 3, 4, 5
First time: i = 1
Print 1
Go back to loop
i = 2
Print 2 …and so on
Stops automatically when numbers finish.
🧠 The loop controls i, not you.

4️⃣ Understanding range() (Critical Concept)

range(start, end)
start → included
end → NOT included
range(1, 5)1, 2, 3, 4

Common Beginner Confusion

“Why doesn’t it include 5?”
Answer:
Because computer counts how many steps, not numbers.

5️⃣ Using Loop for Calculation

Example: Sum of Numbers

total = 0

for i in range(1, 6):
total = total + i

print(total)

How to Think

total remembers result so far
Each loop adds something
This is called accumulation
🧠 Very important thinking pattern: “Start with zero → keep adding”

6️⃣ while Loop — When You Don’t Know How Many Times

Real-World Example

Keep asking password until correct
Keep reading input until user types ‘exit’

Example

i = 1

while i <= 5:
print(i)
i = i + 1

How Computer Thinks

Check condition
If True → run body
After body → check again
Stop when condition becomes False

7️⃣ Infinite Loop (Danger Zone 🚨)

i = 1
while i <= 5:
print(i)
❌ This never stops.
Why?
i never changes
Condition always True
🧠 Every while loop must move toward stopping

8️⃣ break — Force Stop

for i in range(1, 10):
if i == 5:
break
print(i)
Output:
1
2
3
4
break exits loop immediately.

9️⃣ continue — Skip One Step

for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
continue skips current iteration.

🔁 Key Thinking Pattern (Very Important)

Whenever you see a loop, ask:
Where does it start?
When does it stop?
What changes each time?
What is remembered across iterations?

⚠️ Common Beginner Mistakes

Off-by-one errors (range)
Forgetting to update loop variable
Writing logic outside loop
Confusing for and while
Expecting loop to “understand intent”

🧪 Practice Questions (DO NOT RUSH)

Think → write steps → then code.

Q1

Print numbers from 1 to 10.

Q2

Print all even numbers between 1 and 20.

Q3

Take a number n and print sum of numbers from 1 to n.

Q4

Print multiplication table of a given number.
Example:
5 x 1 = 5
5 x 2 = 10

Q5

Count how many digits are there in a given number.
Example:
Input: 12345
Output: 5

Q6

Reverse a number.
Example:
Input: 123
Output: 321

Q7 (Thinking Question)

What happens if n = 0 or n is negative? Does your logic still work?

🔑 HINTS (Read Together, Only If Stuck)

Use % 10 to extract last digit
Use // 10 to remove last digit
Initialize counters properly
For sum problems → start with 0
For reverse → start with 0
Check loop condition carefully

✅ What You Should Be Able to Do Now

After Day 2, you should:
Repeat tasks using loops
Predict how many times loop runs
Avoid infinite loops
Use loop to compute results
Think step-by-step like computer

🧠 Reflection Questions (Answer in Words)

What controls when a loop stops?
Difference between for and while?
Why do we need to update variables inside loops?
What causes infinite loops?

⭐ Golden Rule from Day 2

If you don’t control repetition, repetition will control your program.

 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.