SAMANTUS Python for Data Analytics — Complete Training Manual MODULE 05
← Back to Course Index

Conditional Statements


On This Page

Module 5: Conditional Statements


5.1  The if Statement

Conditional statements let your program make decisions — running certain code only when a condition is True. The if statement is the most basic form of decision-making.

► Basic if Statement
marks = 85
if marks >= 40:
    print("Result: Pass")
Output
Result: Pass
⚠ Important
Python uses indentation (spaces) instead of curly braces {} to define code blocks. All lines inside an if block must be indented consistently — usually 4 spaces.

5.2  if-else Statement

The else block runs when the if condition is False, giving your program two possible paths.

► if-else Example
marks = 32
if marks >= 40:
    print("Result: Pass")
else:
    print("Result: Fail")
Output
Result: Fail

5.3  Nested if

A nested if is an if statement placed inside another if or else block — useful when you need to check a second condition only after the first one is satisfied.

► Nested if Example
marks = 92
if marks >= 40:
    print("Result: Pass")
    if marks >= 90:
        print("Grade: A+ (Distinction)")
    else:
        print("Grade: B")
else:
    print("Result: Fail")
Output
Result: Pass
Grade: A+ (Distinction)
✗ Common Mistake
Incorrect indentation is the #1 cause of errors with nested if statements. Always double-check that each nested block is indented one level deeper than its parent.

5.4  elif Ladder

When you have multiple conditions to check in sequence, elif (else-if) is cleaner than multiple nested if statements.

► elif Ladder Example
marks = 76
if marks >= 90:
    grade = "A+"
elif marks >= 75:
    grade = "A"
elif marks >= 60:
    grade = "B"
elif marks >= 40:
    grade = "C"
else:
    grade = "Fail"
print("Grade:", grade)
Output
Grade: A
✓ Instructor Tip
Grading systems are a perfect real-world example to teach elif — students immediately understand why checking conditions in the right order (highest first) matters.

5.5  match-case (Python 3.10+)

Introduced in Python 3.10, match-case is Python's version of a switch statement — useful when comparing one variable against several fixed values.

► match-case Example
day = "MON"
match day:
    case "MON":
        print("Digital Marketing Class")
    case "WED":
        print("SEO Class")
    case "FRI":
        print("Google Ads Class")
    case _:
        print("No class scheduled")
Output
Digital Marketing Class
ℹ Note
The underscore _ acts as the default case, similar to 'else' — it runs when none of the other cases match.

5.6  Logical Thinking & Flowcharts

Before writing code, it helps to visualize the decision-making process as a flowchart. This builds logical thinking — a core skill for every programmer and data analyst.

... ... Start Condition True or False? Run 'if' block Run 'else' block Continue Program True False
Figure — Basic if-else decision flow.

Figure 5.1 — Basic if-else decision flow: the program checks a condition, then follows exactly one of two paths before continuing.

✓ Instructor Tip
Ask students to draw a flowchart on paper BEFORE writing code for any logic problem. This habit dramatically reduces bugs and builds strong problem-solving skills early on.

5.7  Practical Exercises

Basic (5 Questions)

1. Write a program to check if a number is positive or negative.

2. Write a program to check if a person is eligible to vote (age >= 18).

3. Check if a given number is even or odd using if-else.

4. Write a program that checks if a student passed or failed given their marks.

5. Check if a year is a leap year using basic if conditions (divisible by 4).

Intermediate (5 Questions)

1. Write a grading program using elif that assigns A+/A/B/C/Fail based on marks.

2. Check if a number is divisible by both 3 and 5 using nested if.

3. Write a program using match-case to print a message based on a day of the week.

4. Build a simple login checker that verifies both username and password using nested if.

5. Write a program to find the largest of 3 numbers using elif.

Advanced (5 Questions)

1. Build a BMI Calculator that categorizes a person as Underweight/Normal/Overweight/Obese using elif.

2. Draw a flowchart AND write the code for a simple 'course fee discount' system (student gets 10% off if fee > 10000).

3. Write a program that validates a password (must be 8+ characters AND contain at least 1 number) using nested conditions.

4. Create a simple traffic-light simulator using match-case (Red = Stop, Yellow = Get Ready, Green = Go).

5. Build a 'Loan Eligibility Checker' using nested if that checks income, age, and credit score together.

5.8  Module Quiz (MCQs)

Q1. What does Python use to define a block of code?

Q2. Which keyword checks multiple conditions in sequence?

Q3. What is a nested if?

Q4. Which Python version introduced match-case?

Q5. What does the underscore _ represent in match-case?

ℹ Answer Key
1-b, 2-b, 3-b, 4-c, 5-b

5.9  Module Assignment

Assignment: Student Result & Grading System

Expected Output: A complete grading script that correctly classifies any input marks into the right grade band and adds a distinction remark where applicable.

5.10  Interview Questions — Module 5


5.11  Student Notes Page

Use this space to write down key points, doubts, and your own examples from today's session.