2.6 KiB
Introduction
In Python, if, elif (a contraction of 'else and if') and else statements are used to control the flow of execution and make decisions in a program.
Unlike many other programming languages, Python versions 3.9 and below do not offer a formal case-switch statement, instead using multiple elif statements to serve a similar purpose.
Python 3.10 introduces a variant case-switch statement called pattern matching, which will be covered separately in another concept.
Conditional statements use expressions that must resolve to True or False -- either by returning a bool directly, or by evaluating "truthy" or "falsy".
x = 10
y = 5
# The comparison '>' returns the bool 'True',
# so the statement is printed.
if x > y:
print("x is greater than y")
...
>>> x is greater than y
When paired with if, an optional else code block will execute when the original if condition evaluates to False:
x = 5
y = 10
# The comparison '>' here returns the bool False,
# so the 'else' block is executed instead of the 'if' block.
if x > y:
print("x is greater than y")
else:
print("y is greater than x")
...
>>> y is greater than x
elif allows for multiple evaluations/branches.
x = 5
y = 10
z = 20
# The elif statement allows for the checking of more conditions.
if x > y > z:
print("x is greater than y and z")
elif y > x > z:
print("y is greater than x and z")
else:
print("z is greater than x and y")
...
>>> z is greater than x and y
Boolean operations and comparisons can be combined with conditionals for more complex testing:
>>> def classic_fizzbuzz(number):
if number % 3 == 0 and number % 5 == 0:
return 'FizzBuzz!'
elif number % 5 == 0:
return 'Buzz!'
elif number % 3 == 0:
return 'Fizz!'
else:
return str(number)
>>> classic_fizzbuzz(15)
'FizzBuzz!'
>>> classic_fizzbuzz(13)
'13'