Python Programming Conditional Statements
Decision Making 4 Types

Python Conditional Statements Complete Guide

Learn Python conditional statements - simple if, if-else, elif ladder, and nested if with practical examples, flowcharts, and real-world applications for effective decision making.

Simple if

Single condition check

if-else

Two-way decision

elif Ladder

Multiple conditions

Nested if

Condition within condition

What are Conditional Statements?

Conditional statements allow your program to make decisions and execute different code blocks based on certain conditions. They are fundamental to controlling program flow in Python.

Python control flow diagram illustrating conditional statements: if branches, elif ladders, else defaults, and decision paths for Boolean conditions in programs
Control flow: Conditionals choose which block runs—if, elif, and else structure decisions in Python.

Key Concept

Conditional statements evaluate Boolean expressions (True or False) to decide which code blocks to execute. Python uses indentation (whitespace) to define code blocks.

Basic Conditional Statement Structure
# General syntax of conditional statements
if condition:
    # Code to execute if condition is True
    statement1
    statement2
elif another_condition:  # Optional (multiple can exist)
    # Code to execute if elif condition is True
    statement3
else:                    # Optional
    # Code to execute if all conditions are False
    statement4

# Note the colon (:) and indentation (4 spaces or 1 tab)

Python Conditional Statements Classification

Python conditional statements are classified into 4 main types. Each type serves a specific purpose in decision-making logic.

Complete Conditional Statements Reference Table

Type Syntax Description Best For Example
Simple if if condition: Executes a block only when condition is True Single condition check, execute or skip if age >= 18: print("Can vote")
if-else if-else Provides two alternative execution paths Two-way decisions, either-or scenarios if score >= 50: pass else: fail
elif Ladder if-elif-else Checks multiple conditions sequentially Multiple exclusive conditions, categorization if grade=='A': elif grade=='B': else:
Nested if if inside if Conditional statements inside other conditionals Hierarchical decisions, multi-stage validation if condition1: if condition2: ...
Quick Tip:
  • Use simple if for single condition checks
  • Use if-else for binary (true/false) decisions
  • Use elif ladder when you have multiple exclusive conditions
  • Use nested if for complex hierarchical decisions

Conditional Statement Examples with Output

Let's explore practical examples of each conditional statement type with actual Python code and output.

Simple if Statement Examples
# Simple if Statement Examples
print("=== Simple if Statement ===")

# Example 1: Check if number is positive
number = 10
if number > 0:
    print(f"{number} is a positive number")

# Example 2: Check voting eligibility
age = 18
if age >= 18:
    print("You are eligible to vote!")

# Example 3: String condition
password = "secure123"
if len(password) >= 8:
    print("Password meets minimum length requirement")

# Example 4: Multiple conditions with AND
temperature = 25
humidity = 60
if temperature > 20 and humidity < 70:
    print("Weather conditions are comfortable")
if-else Statement Examples
# if-else Statement Examples
print("\n=== if-else Statement ===")

# Example 1: Even or odd number check
number = 7
if number % 2 == 0:
    print(f"{number} is an even number")
else:
    print(f"{number} is an odd number")

# Example 2: Age verification for movie ticket
age = 16
if age >= 18:
    print("You can watch the movie (18+ rating)")
    print("Ticket price: $12")
else:
    print("Sorry, you are too young for this movie")
    print("Please choose a different movie")

# Example 3: Password validation
entered_password = "admin123"
correct_password = "admin123"
if entered_password == correct_password:
    print("Access granted!")
else:
    print("Access denied!")
elif Ladder Examples
# Short elif ladder example
marks = 82

if marks >= 90:
    print("Grade A")
elif marks >= 75:
    print("Grade B")
elif marks >= 60:
    print("Grade C")
else:
    print("Need improvement")
Nested if Statement Examples
# Short nested if example
age = 20
has_id = True

if age >= 18:
    if has_id:
        print("Entry allowed")
    else:
        print("ID required")
else:
    print("Not eligible")

Comparison and Best Practices

Different types of conditional statements are suitable for different scenarios. Here's a comparison to help you choose the right one.

Type Best For When to Use Complexity Readability
Simple if Single condition check When you need to execute or skip a block Low High
if-else Two alternative paths Either-or decisions, pass/fail scenarios Low High
elif Ladder Multiple exclusive conditions When you have 3+ mutually exclusive options Medium Medium
Nested if Hierarchical conditions Multi-stage validation, complex logic High Low (if deeply nested)
Best Practices:
  • Use meaningful condition names for better readability
  • Avoid deeply nested if statements (more than 2-3 levels)
  • Use logical operators (and, or) to combine simple conditions
  • Always include a final else clause for handling unexpected cases
  • Use comments to explain complex conditional logic

Real-World Applications

Conditional statements are used everywhere in programming. Here are some practical applications:

E-commerce Discount System

Apply discounts based on order amount and user type:

if user_type == "premium":
    discount = 20
elif order_amount > 1000:
    discount = 15
elif order_amount > 500:
    discount = 10
else:
    discount = 5
Health Monitoring System

Check health metrics and provide recommendations:

if blood_pressure > 140:
    status = "High BP - Consult doctor"
elif blood_pressure < 90:
    status = "Low BP - Rest and hydrate"
else:
    status = "Normal - Keep monitoring"
Game AI Decision Making

AI character decisions based on game state:

if player_nearby:
    if player_health < 30:
        action = "attack"
    else:
        if ai_health > 70:
            action = "attack"
        else:
            action = "retreat"
else:
    action = "patrol"
Smart Home Automation

Control home devices based on conditions:

if time.hour > 18:
    if motion_detected:
        lights.on()
    else:
        lights.off()
elif temperature > 25:
    ac.on()
else:
    ac.off()

Practice Programs

Use these short practice programs to master each conditional type quickly.

if statement practice program
# Program 1: Check if number is positive
num = -7
if num > 0:
    print("Positive number")
if-else practice program
# Program 2: Even or odd
num = 24
if num % 2 == 0:
    print("Even")
else:
    print("Odd")
elif ladder practice program
# Program 3: Grade calculator
marks = 68
if marks >= 90:
    print("A Grade")
elif marks >= 75:
    print("B Grade")
elif marks >= 60:
    print("C Grade")
else:
    print("D Grade")
nested if practice program
# Program 4: Login access check
username = "admin"
password = "admin123"

if username == "admin":
    if password == "admin123":
        print("Login successful")
    else:
        print("Wrong password")
else:
    print("Invalid username")

Key Takeaways

  • Simple if: Executes code only when condition is True
  • if-else: Provides two alternative execution paths
  • elif ladder: Handles multiple exclusive conditions sequentially
  • Nested if: Places if statements inside other if statements for complex logic
  • Python uses indentation (4 spaces) to define code blocks - this is mandatory
  • Conditions are Boolean expressions that evaluate to True or False
  • Use and, or, not operators to combine conditions
  • In elif ladders, order matters - conditions are checked top to bottom
  • Avoid deep nesting (more than 2-3 levels) for better readability
  • Always include an else clause for handling unexpected cases