Python Learning Series · @onlysamarrr · Day 8 / 30

Day 8: if-else

Day 8 teaches if-else decisions. If the condition is True the if block runs, otherwise the else block runs. Blocks are recognised by 4-space indentation.

Phase: Beginner · Reels: 36/450 – 40/450

Quick Answer

Day 8 teaches if-else decisions. If the condition is True the if block runs, otherwise the else block runs. Blocks are recognised by 4-space indentation.

What You Will Learn

Code

marks = 72
if marks >= 90:
    grade = "A"
elif marks >= 60:
    grade = "B"
else:
    grade = "C"
print(grade)
status = "Pass" if marks >= 33 else "Fail"
print(status)

Output

B
Pass

Code Explained, Line by Line

  1. A condition is an expression that is True or False, like marks >= 33.
  2. elif means 'otherwise, if': the first True condition in the chain wins and the rest are skipped.
  3. else runs only when no condition above it was True.
  4. Blocks must be indented, usually by 4 spaces. Wrong indentation raises an IndentationError.
  5. "Pass" if marks >= 33 else "Fail" is a one-line if-else, called a ternary expression.
Common Mistake: Wrong indentation raises IndentationError. Also note the difference: multiple if statements all run their checks, while an elif chain stops at the first True condition.

Real-Life Use

Pass/fail results, login checks and discount rules are all if-else logic.

Practice Task: Write a program that prints whether a number is big or small, and a login check using username and password.

Test Yourself — MCQs

Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.

Q1. marks = 20. What does "Pass" if marks >= 33 else "Fail" give?

Q2. What causes an IndentationError?

Q3. In an elif chain, how many blocks run?

Today's 5 Reels (from the 90-Day Plan Excel)

36/450Conceptif-else ka logic
37/450Live demoPass ya fail program
38/450Common mistakeIndentation error
39/450QuizQuiz: bada number batao
40/450Interview / real lifeReal life: login check
← Day 7Day 9 →