Conditional Statements in Python | Python Course | Dataplexa

Conditional Statements in Python

Programs become truly useful when they can make decisions. Think about how apps work in real life — a banking app shows a different message when your balance is low, a login system blocks access if the password is wrong, and a shopping site applies a discount only when you spend enough. All of these decisions are made using conditional statements.

In Python, a conditional statement checks whether something is True or False, then runs different code depending on the result. This lesson covers everything you need to know about making decisions in Python.

How Conditions Work

A condition is an expression that Python evaluates and turns into either True or False. You build conditions using comparison operators like ==, >, <, and !=.

print(10 > 5)    # True  — 10 is greater than 5
print(10 == 5)   # False — 10 is not equal to 5
print(7 != 7)    # False — 7 does equal 7
True False False
  • Every comparison returns True or False — nothing else.
  • These True/False results are what conditional statements use to decide what to do next.

The if Statement

The if statement is the most basic form of decision-making. It runs a block of code only when the condition is True. If the condition is False, Python skips the block entirely and moves on.

age = 20

if age >= 18:
    print("Access granted")
Access granted
  • The condition is age >= 18. Since 20 is greater than 18, this is True.
  • Because it is True, the indented line runs and prints the message.
  • If age were 15, the condition would be False and nothing would print.

Indentation Defines the Block

Python uses indentation — spaces at the start of a line — to define which lines belong to a conditional block. Every line indented under an if is part of that block. Lines that go back to the original indentation level run regardless of the condition.

marks = 75

if marks >= 50:
    print("Pass")        # runs because condition is True
    print("Good job")    # also inside the if block

print("End of program")  # always runs — not inside the if block
Pass Good job End of program
  • Both indented lines only run when the condition is True.
  • The last line always runs because it is outside the if block.
  • Python is strict about indentation — inconsistent spacing causes errors.

The if...else Statement

An else block gives your program a second path to take when the condition is False. This guarantees that one of the two blocks will always run — either the if block or the else block, never both.

temperature = 25

if temperature > 30:
    print("Weather is Hot")
else:
    print("Weather is Pleasant")
Weather is Pleasant
  • Since 25 is not greater than 30, the condition is False.
  • Python skips the if block and runs the else block instead.
  • Exactly one block runs every time.

The if...elif...else Statement

When you have more than two possible outcomes, use elif (short for "else if") to check additional conditions. Python checks each condition from top to bottom and stops at the first one that is True.

score = 82

if score >= 90:
    print("Grade: A")
elif score >= 75:
    print("Grade: B")
elif score >= 50:
    print("Grade: C")
else:
    print("Grade: Fail")
Grade: B
  • Python checks score >= 90 first — False, so it moves on.
  • It checks score >= 75 — True, so it prints "Grade: B" and stops checking.
  • Only one block ever runs in an if-elif-else chain.
  • Always put the strongest condition first — checking 50 before 90 would give wrong results.

Using Logical Operators in Conditions

Real-world decisions often depend on more than one condition at the same time. Use and, or, and not to combine conditions into a single expression.

age = 22
has_id = True

# and — both must be True
if age >= 18 and has_id:
    print("Entry allowed")

# or — at least one must be True
day = "Sunday"
if day == "Saturday" or day == "Sunday":
    print("Weekend")

# not — reverses the condition
is_blocked = False
if not is_blocked:
    print("User is active")
Entry allowed Weekend User is active
  • and is strict — every condition must be True for the block to run.
  • or is flexible — just one True condition is enough.
  • not flips True to False and False to True, which is useful for checking the opposite of something.

Nested Conditions

A nested condition is an if statement placed inside another if statement. This is useful when one decision depends on another decision first being made.

age = 20
has_license = False

if age >= 18:
    if has_license:
        print("You can drive")
    else:
        print("You are eligible, but you need a license first")
else:
    print("You are too young to drive")
You are eligible, but you need a license first
  • The outer condition checks age first.
  • Only if age is 18 or above does Python bother checking the license.
  • Nested conditions are useful but can become hard to read if you go too deep — keep it to two or three levels at most.

Chained Comparisons

Python has a clean shortcut for checking whether a value falls within a range. Instead of writing two separate conditions joined by and, you can chain them in a single readable expression.

x = 7

if 1 <= x <= 10:
    print("x is within the range 1 to 10")
else:
    print("x is outside the range")
x is within the range 1 to 10
  • This is the same as writing x >= 1 and x <= 10, but much easier to read.
  • Chained comparisons are a Python feature that many other languages do not support.

Truthy and Falsy Values

In Python, some values behave like False even when they are not literally the word False. These are called falsy values. Knowing them helps you write shorter, cleaner conditions.

The most common falsy values are: 0, "" (empty string), [] (empty list), {} (empty dictionary), and None. Everything else is considered truthy.

name = ""      # empty string is falsy
items = []     # empty list is falsy
count = 0      # zero is falsy

if not name:
    print("Name is missing")

if not items:
    print("No items in the cart")

if not count:
    print("Count is zero")
Name is missing No items in the cart Count is zero
  • This pattern is used all the time in real programs for validation and error checking.
  • It is cleaner than writing if name == "" or if len(items) == 0.

Real World Example — E-commerce Discount System

This example puts everything together. It takes a customer's purchase total and decides whether they qualify for a discount, then calculates and displays the final price.

subtotal = 1499.0
discount_rate = 0.10
is_member = True

# Members get 10% off if they spend 1000 or more
if subtotal >= 1000 and is_member:
    discount = subtotal * discount_rate
    final_total = subtotal - discount
    print("Member discount applied:", discount)
    print("Final Total:", final_total)
elif subtotal >= 1000:
    discount = subtotal * 0.05
    final_total = subtotal - discount
    print("Standard discount applied:", discount)
    print("Final Total:", final_total)
else:
    print("No discount — spend 1000 or more to qualify")
    print("Final Total:", subtotal)
Member discount applied: 149.9 Final Total: 1349.1
  • The first condition checks both the spending threshold and membership using and.
  • The second condition handles non-members who still qualify for a smaller discount.
  • The else block covers everyone else with no discount.
  • This is how most real discount engines work at their core.

Common Mistakes with Conditionals

  • Missing the colon — Every if, elif, and else line must end with a colon :. Forgetting it causes a syntax error.
  • Wrong indentation — Python will raise an error if lines inside a block are not indented consistently. Use 4 spaces.
  • Using = instead of ==if x = 5 tries to assign a value, which is not allowed here. Use if x == 5 to compare.
  • Wrong elif order — In a grade checker, putting score >= 50 before score >= 90 means a score of 95 would match the 50 condition first and print the wrong grade.

Practice

Which keyword is used to check additional conditions after the first if statement?



In Python, what defines which lines belong to an if block?



Which block runs when the if condition is False?



Which logical operator requires all conditions to be True?



Quick Quiz

In an if-elif-else chain, how many blocks will execute?





Values like 0, "", and [] are called what in Python conditions?





Which symbol must appear at the end of every if, elif, and else line?





NEXT UP
Loops in Python
Learn how to repeat actions automatically using for loops and while loops — so you can process lists, count numbers, and handle repetitive tasks without rewriting the same code over and over.