Indentation
Python uses indentation to define which lines of code belong to a specific block (like an if statement or a loop).
Where other languages use curly braces {}, Python uses whitespace.
By default, most editors automatically insert 4 spaces when you press the Tab key. However, if you manually type spaces on one line and use an actual Tab character on the next, your code will crash with a TabError.
Just don’t mix them.
If
An if statement tells Python to run a block of code only if a condition is True. If the condition is False, Python skips it.
balance = 100
if balance > 50:
print("You have enough money to buy this.")
Else
You can provide a fallback using else. This code runs only if the if condition is False.
age = 15
if age >= 18:
print("You can vote.")
else:
print("You are too young to vote.")
Elif
If you have more than two options, use elif (else if). Python checks these in order and stops at the first one that is true.
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
Because Python stops at the first True condition it finds - always put your most specific conditions at the top.
score = 95
# WRONG: This will print "Passed" and stop, even though the student got an "A"
if score >= 50:
print("Passed")
elif score >= 90:
print("A")
Match Case (Python 3.10+)
If you are checking one variable against many specific values, elif chains get ugly. The match statement is the modern, cleaner alternative.
command = "settings"
match command:
case "home":
print("Loading Dashboard...")
case "settings":
print("Loading User Settings...")
case "help":
print("Opening Help Menu...")
case "quit":
print("Exiting...")
case _:
# The underscore is a 'wildcard' - it catches anything else.
print("Unknown command. Type 'help' for options.")
But if you’d try match score: followed by case score >= 90:, that actually won’t work (it will treat score as a variable name and catch everything). To use comparisons in a match statement, you need a “guard” like this:
score = 85
match score:
case s if s >= 90:
print("Grade: A")
case s if s >= 80:
print("Grade: B")
case s if s >= 70:
print("Grade: C")
case _:
# The underscore is a 'wildcard' - it catches anything else.
print("Grade: F")
Logical Combinations
You can check multiple things at once using and and or.
and: Everything must beTrue.or: At least one thing must beTrue.
# Using 'and'
age = 20
has_id = True
if age >= 18 and has_id:
print("Entry allowed.")
# Using 'or'
is_admin = False
has_guest_pass = True
if is_admin or has_guest_pass:
print("Access granted to the lounge.")
Chained Comparisons
Python allows you to chain comparisons together like you do in math. This is much more readable (and “Pythonic”) than using and.
age = 25
# The standard way
if age >= 18 and age <= 30:
print("Target group")
# The Pythonic way (Chained)
if 18 <= age <= 30:
print("Target group")
One-Liner (Ternary Operator)
For very simple logic, you can write an if-else statement on a single line. This is called a Ternary Operator.
Syntax: value_if_true if condition else value_if_false
age = 20
# Instead of 4 lines of if/else:
status = "Adult" if age >= 18 else "Minor"
print(status) # Adult
Nested Logic
You can put if statements inside other if statements. Just be careful: if you go 3 or 4 levels deep, your code becomes very hard to read (often called “Spaghetti Code”).
is_logged_in = True
is_admin = False
if is_logged_in:
if is_admin:
print("Welcome, Boss.")
else:
print("Welcome, User.")
else:
print("Please log in.")