Menu

Conditional Logic

Introduction

Conditional statements let your program make decisions. Python uses if, elif, and else — and indentation instead of curly braces.

If statements

age = 20

if age >= 18:
    print("You are an adult.")
elif age >= 13:
    print("You are a teenager.")
else:
    print("You are a child.")

# Ternary (conditional expression)
status = "adult" if age >= 18 else "minor"

# Match statement (Python 3.10+)
command = "start"
match command:
    case "start":
        print("Starting...")
    case "stop":
        print("Stopping...")
    case _:
        print("Unknown command")

Important: Python uses indentation (4 spaces) to define code blocks. This is not optional — it's part of the language syntax.

Assignment

  1. Read about control flow in Python.
  2. Write a program that asks the user for a number and prints whether it's positive, negative, or zero.
  3. Experiment with the match statement if you have Python 3.10+.

Knowledge check

Support me!

I am a software engineer giving back to the community - my name is Musila Peter. Join me in empowering learners around the globe by supporting SaneGenius!