Yesterday's 7 > 5 returned True. Useful by itself? Not really — the value is just printed and forgotten. The real use of a boolean is to steer the program: run one piece of code when it's true, a different piece when it's false. That's an if.
Like a fork in the road?
Exactly. Syntax:
if condition:
do_this()
else:
do_that()The condition is any expression that produces a boolean — usually a comparison. The colon at the end is required. The lines under if/else are indented — Python uses indentation to mark blocks, where most languages use braces.
How much indent?
4 spaces is the convention. Whatever you choose, be consistent inside one block — Python will reject mixed indentation with IndentationError.
And else is optional?
It is. if alone runs the block when true, skips it when false, and moves on. else only matters when you have something to do in the false case.
if / else — branchingThe shape:
if condition:
# runs when condition is True
...
else:
# runs when condition is False
...The condition is any expression that produces a boolean — typically a comparison (x > 0, name == "Ada", etc.) but also any other expression Python can interpret as truthy.
Python uses indentation (whitespace at the start of a line) to mark which lines belong to which block. The convention is 4 spaces per level.
x = 7
if x > 5:
print("big") # belongs to the if
print("really big") # also belongs to the if
print("done") # outside — runs every timeMixing tabs and spaces, or having inconsistent depth, raises IndentationError. Most editors handle indentation automatically — if yours doesn't, set tab to insert 4 spaces.
else is optionalif x < 0:
print("negative")
# no else — falling through is fineUse else only when you have something to do when the condition is false. A bare if is common.
Most real conditions use comparison operators, but Python lets you put almost any value in the if slot — it converts to boolean automatically. Things that count as False: 0, 0.0, "" (empty string), [] (empty list), None. Everything else is True.
if name: # True for any non-empty string
print(name)This is idiomatic — "if name has a value, use it" — and clearer than if name != "".
Yesterday's 7 > 5 returned True. Useful by itself? Not really — the value is just printed and forgotten. The real use of a boolean is to steer the program: run one piece of code when it's true, a different piece when it's false. That's an if.
Like a fork in the road?
Exactly. Syntax:
if condition:
do_this()
else:
do_that()The condition is any expression that produces a boolean — usually a comparison. The colon at the end is required. The lines under if/else are indented — Python uses indentation to mark blocks, where most languages use braces.
How much indent?
4 spaces is the convention. Whatever you choose, be consistent inside one block — Python will reject mixed indentation with IndentationError.
And else is optional?
It is. if alone runs the block when true, skips it when false, and moves on. else only matters when you have something to do in the false case.
if / else — branchingThe shape:
if condition:
# runs when condition is True
...
else:
# runs when condition is False
...The condition is any expression that produces a boolean — typically a comparison (x > 0, name == "Ada", etc.) but also any other expression Python can interpret as truthy.
Python uses indentation (whitespace at the start of a line) to mark which lines belong to which block. The convention is 4 spaces per level.
x = 7
if x > 5:
print("big") # belongs to the if
print("really big") # also belongs to the if
print("done") # outside — runs every timeMixing tabs and spaces, or having inconsistent depth, raises IndentationError. Most editors handle indentation automatically — if yours doesn't, set tab to insert 4 spaces.
else is optionalif x < 0:
print("negative")
# no else — falling through is fineUse else only when you have something to do when the condition is false. A bare if is common.
Most real conditions use comparison operators, but Python lets you put almost any value in the if slot — it converts to boolean automatically. Things that count as False: 0, 0.0, "" (empty string), [] (empty list), None. Everything else is True.
if name: # True for any non-empty string
print(name)This is idiomatic — "if name has a value, use it" — and clearer than if name != "".
Create a free account to get started. Paid plans unlock all tracks.