Lesson Β· β± 8 min read
π Python logic: if/else, loops and functions
EnglishHinglish
π Real-life example
A traffic signal makes decisions: if red, stop; else if green, go. That's if/else. A washing machine repeats the same rinse cycle several times. That's a loop. And your mom's chai recipe is written once and reused every morning. That's a function: steps you package once and call whenever you need them.
π‘ In simple words
Programs need to make decisions, repeat work and stay organised. 'if / elif / else' runs different code depending on a condition. A 'for' loop repeats code for each item in a collection, and a 'while' loop repeats until a condition becomes false. A function (made with 'def') bundles steps under a name, takes inputs (parameters) and gives back an output with 'return'. Good functions do one clear job, which makes code easier to test and reuse.
πͺ Step by step
- Decisions with if / elif / else: if marks >= 90: grade = "A" elif marks >= 75: grade = "B" else: grade = "C". Indentation (4 spaces) shows what's inside each block.
- Repeat with for: for city in ["Pune", "Delhi"]: print(city). Use range(5) to repeat 5 times.
- Repeat until something changes with while: while balance < target: balance += saving. Make sure the loop can end.
- Package logic in a function: def emi(principal, rate, months): ... return value. Call it with different inputs instead of copying code.
- Test functions with small known cases: if emi(100000, 0, 10) should be 10000, check it with assert.
π Key terms
- Condition
- A question with a True/False answer, e.g. price > 100.
- Loop
- Code that repeats for each item or until a condition changes.
- Function
- A named, reusable block of code with inputs and an output.
π See it in action
def bill(amount):
if amount >= 500:
return amount * 0.9 # 10% off on big orders
return amount
orders = [120, 650, 300]
for o in orders:
print(o, "β", bill(o))
# 120 β 120, 650 β 585.0, 300 β 300A kirana-store discount rule written once and used for every order.
β οΈ Common mistakes to avoid
- Wrong indentation: mixing tabs and spaces or forgetting to indent after a colon causes errors or wrong logic.
- Infinite while loops: the condition never becomes false. Always update the variable you check.
- Printing inside a function instead of returning. print shows a value; return gives it back so other code can use it.
β Quick check
You need to apply the same discount rule to 1,000 orders. What do you use?
- Copy-paste the code 1,000 times
- A single if statement
- A function inside a loop
Show answer
C. A function inside a loop: Write the rule once as a function, then loop over the orders and call it for each one.
π Questions learners ask
When should I use for vs while?
Use for when you know what to go through (a list, a range). Use while when you repeat until a condition changes and don't know how many times.
How big should a function be?
Small enough to describe in one sentence. If you need 'and' twice to describe it, split it into two functions.
π Remember
if = decide, loop = repeat, function = reuse.
Learn this as part of a full course
Track your progress, take the quick checks and follow the lessons in the right order. This lesson is part of:
AI Foundations Path Β· AI Product Engineer Β· MLOps / AI Platform Engineer Β· LLM / Agent Engineer Β· AI Forward Deployed Engineer
Continue in the course