Flow Control
Python - A Quick Start for existing Programers
2 min read
Published Sep 16 2025, updated Sep 30 2025
Guide Sections
Guide Comments
If, elif & else statements
If statements are structured using just indents (usually 4 spaces) so define the code flow. if
, elif
and else
blocks cannot be left empty, you can use the pass
statement to add content to a block that doesn't run any code, this can be useful in development when the contents of a block aren't known yet, to avoid errors.
An if
statement:
An if
, elif
(else if) statement:
An if
, else
statement:
An if
, elif
, else
statement:
Nested if
statements (defined by nested indentation):
Ternary operators
Ternary operators are a way of writing if else code in a short hand way on a single line. They only handle if
and else
, not elif
, however you can stack multiple in a row.
Ternary if
statement:
Ternary if
else
statement:
Stacked ternary if
else
statements:
Match statement
This is similar to switch
/ case
statements in other languages.
Case statements are processed from top to bottom. Multiple values can be specified for a case by using the |
between them. A catch all of _
can be added to the bottom of the list as this always matches (as it always matches, make sure it is the last case check in the list):
Another example of multiple value cases:
Case checks can also have added if conditions combined: