Selecting a branch to execute
Indenting code for conditional statements
Indentation allows you to organize your program into branches: blocks of code that can be selected based on conditions.
if condition:
# first branch
# this code will only execute if condition is true
# more code
else:
# second branch
# this code will only execute if the first branch condition is not true
# more code
Selecting between two branches
If the first condition is not true, you can run some other branch with the else
syntax.
sunny_outside = True
if sunny_outside:
print('Let\'s go to the beach!')
else:
print('Let\'s stay inside...')
Selecting between multiple branches
If you have multiple branches to choose from, you can set a condition for each one using the elif
syntax.
your_age = 12 # in years
if your_age >= 20:
# first branch to run only if its condition is true
print('You are too old for the teen area.')
elif your_age < 13:
# additional branch to run only if is condition is true
print('You are too young for the teen area.')
else:
# final branch to run only if none of the above conditions are true
print('Welcome to the teen area!')
Executing conditional code in your head
To execute conditional code in your head, follow these steps:
- Read each conditional statement in order from top to bottom
- Determine if the condition is true
- If the condition is true, execute the indented code in its branch and skip all other branches
- If the condition is not true, continue to the next branch
- If you reach the else branch, execute the indented code