Conditionals
from Exercism
- if
- elif
- else
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 is executed when the original if condition returns False.
x = 5
y = 10
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.
x = 5
y = 10
z = 20
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