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

  1. 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.
  2. Repeat with for: for city in ["Pune", "Delhi"]: print(city). Use range(5) to repeat 5 times.
  3. Repeat until something changes with while: while balance < target: balance += saving. Make sure the loop can end.
  4. Package logic in a function: def emi(principal, rate, months): ... return value. Call it with different inputs instead of copying code.
  5. 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

python
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 β†’ 300

A 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?

  1. Copy-paste the code 1,000 times
  2. A single if statement
  3. 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