Skip to main content

Conditional Statements

Use if, elif and else to run code depending on a condition. Python uses indentation to define blocks.

marks = 72

if marks >= 90:
grade = "A"
elif marks >= 60:
grade = "B"
else:
grade = "C"

print(grade) # B

Conditional expression​

status = "adult" if age >= 18 else "minor"

Truthy and falsy values​

0, "", [], {}, None and False are falsy. Everything else is truthy.

items = []
if not items:
print("empty")

match statement (Python 3.10+)​

command = "stop"
match command:
case "go":
print("Going")
case "stop":
print("Stopping")
case _:
print("Unknown")