Tutorials Logic
Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Python if elif else Statement - Conditional Logic Tutorial

What are Conditional Statements?

Conditional statements let your program make decisions - executing different code based on whether a condition is True or False. Python uses if, elif, and else.

if Statement

if Statement
age = 20

if age >= 18:
    print("You are an adult")
    print("You can vote")

# Nothing happens if condition is False
temperature = 15
if temperature > 30:
    print("It's hot!")  # not printed

if-else Statement

if-else
score = 75

if score >= 60:
    print("Pass")
else:
    print("Fail")

# Check even or odd
number = 17
if number % 2 == 0:
    print(f"{number} is even")
else:
    print(f"{number} is odd")   # 17 is odd

if-elif-else Statement

Use elif (short for "else if") to check multiple conditions in sequence.

if-elif-else
score = 82

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"

print(f"Grade: {grade}")  # Grade: B

# Multiple conditions with and/or
age = 25
income = 50000

if age >= 18 and income >= 30000:
    print("Loan approved")
elif age >= 18 or income >= 50000:
    print("Partial approval")
else:
    print("Not eligible")

Nested if Statements

Nested if
num = 15

if num > 0:
    print("Positive")
    if num % 2 == 0:
        print("and even")
    else:
        print("and odd")   # Positive and odd
elif num < 0:
    print("Negative")
else:
    print("Zero")

Ternary (Conditional) Expression

A one-line shorthand for simple if-else. Syntax: value_if_true if condition else value_if_false

Ternary Expression
age = 20
status = "adult" if age >= 18 else "minor"
print(status)  # adult

# Equivalent to:
# if age >= 18:
#     status = "adult"
# else:
#     status = "minor"

# In a print statement
x = 42
print("positive" if x > 0 else "non-positive")  # positive

# Nested ternary (use sparingly - can hurt readability)
score = 75
grade = "A" if score >= 90 else "B" if score >= 80 else "C" if score >= 70 else "F"
print(grade)  # C

match Statement (Python 3.10+)

Python's structural pattern matching - similar to switch/case in other languages but much more powerful.

match Statement
command = "quit"

match command:
    case "quit":
        print("Quitting...")
    case "help":
        print("Showing help")
    case "start":
        print("Starting...")
    case _:           # default (wildcard)
        print(f"Unknown command: {command}")

# Match with conditions (guards)
point = (0, 5)
match point:
    case (0, 0):
        print("Origin")
    case (x, 0):
        print(f"On x-axis at {x}")
    case (0, y):
        print(f"On y-axis at {y}")   # On y-axis at 5
    case (x, y):
        print(f"Point at ({x}, {y})")

Truthy and Falsy Values

In Python, conditions don't have to be explicit True/False. Many values are considered truthy or falsy.

Falsy ValuesTruthy Values
FalseTrue
NoneAny non-zero number
0, 0.0, 0jNon-empty string
"" (empty string)Non-empty list/tuple/set/dict
[], (), {}, set()Any object by default
Truthy/Falsy in Practice
name = ""
if name:
    print(f"Hello, {name}")
else:
    print("Name is empty")   # Name is empty

items = []
if items:
    print("Has items")
else:
    print("Empty list")      # Empty list

# Pythonic way to check for None
result = None
if result is None:
    print("No result yet")

# Pythonic way to provide defaults
username = ""
display = username or "Guest"
print(display)  # Guest

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.