Control flow is how your program makes decisions. Python's if statement evaluates conditions and runs different code blocks depending on the result.
if
Key syntax rules:
:
elif
else
Python uses indentation to define blocks. This is not cosmetic โ it is the syntax:
Python's if doesn't require an explicit boolean โ it evaluates the truthiness of any value:
Python's ternary operator lets you write a simple if/else on a single line:
Deep nesting makes code hard to read. Prefer early returns to flatten logic:
Python lets you chain comparisons naturally:
Which of the following values is TRUTHY in Python?
What is the correct syntax for a ternary expression in Python?
How many elif clauses can an if statement have?