The if/else statement is the foundation of decision-making in Python. It lets your program choose between two paths based on whether a condition is True or False. The if block runs when the condition is True; the else block runs when it is False. Python uses indentation (4 spaces) to define which lines belong to each block. The condition can be any expression that evaluates to a boolean — comparisons, logical operators, or even truthy/falsy values.
Key Points
if checks a condition: if condition: runs the indented block when True.
else provides an alternative: else: runs when the if condition is False.
Indentation matters: Python uses 4 spaces to group block contents.
The condition can use comparison operators: ==, !=, >, <, >=, <=.
You can combine conditions with logical operators: and, or, not.
Only one branch executes: either the if block or the else block, never both.
The colon (:) after if and else is required syntax.