1. The comparison (otherwise known as relational) operators are used to compare values. The table below illustrates how the comparison operators work, assuming that x = 0
, y = 1
, and z = 0
:
Operator | Description | Example |
---|---|---|
== |
returns True if operands’ values are equal, and False otherwise |
|
!= |
returns True if operands’ values are not equal, and False otherwise |
x != y # True x != z # False |
> |
True if the left operand’s value is greater than the right operand’s value, and False otherwise |
x > y # False y > z # True |
< |
True if the left operand’s value is less than the right operand’s value, and False otherwise |
x < y # True y < z # False |
≥ |
True if the left operand’s value is greater than or equal to the right operand’s value, and False otherwise |
x >= y # False x >= z # True y >= z # True |
≤ |
True if the left operand’s value is less than or equal to the right operand’s value, and False otherwise |
x <= y # True x <= z # True y <= z # False |
2. When you want to execute some code only if a certain condition is met, you can use a conditional statement:
-
- a single
if
statement, e.g.:
- a single
1 2 3 4 |
x = 10 if x == 10: # condition print("x is equal to 10") # Executed if the condition is True. |
-
- a series of
if
statements, e.g.:
- a series of
1 2 3 4 5 6 7 8 9 10 |
x = 10 if x > 5: # condition one print("x is greater than 5") # Executed if condition one is True. if x < 10: # condition two print("x is less than 10") # Executed if condition two is True. if x == 10: # condition three print("x is equal to 10") # Executed if condition three is True. |
Each if
statement is tested separately.
-
- an
if-else
statement, e.g.:
- an
1 2 3 4 5 6 7 |
x = 10 if x < 10: # Condition print("x is less than 10") # Executed if the condition is True. else: print("x is greater than or equal to 10") # Executed if the condition is False. |
-
- a series of
if
statements followed by anelse
, e.g.:
- a series of
1 2 3 4 5 6 7 8 9 10 11 12 13 |
x = 10 if x > 5: # True print("x > 5") if x > 8: # True print("x > 8") if x > 10: # False print("x > 10") else: print("else will be executed") |
Each
if
is tested separately. The body of else
is executed if the last if
is False
.
-
- The
if-elif-else
statement, e.g.:
- The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
x = 10 if x == 10: # True print("x == 10") if x > 15: # False print("x > 15") elif x > 10: # False print("x > 10") elif x > 5: # True print("x > 5") else: print("else will not be executed") |
If the condition for
if
is False
, the program checks the conditions of the subsequent elif
blocks – the first elif
block that is True
is executed. If all the conditions are False
, the else
block will be executed.
-
- Nested conditional statements, e.g.:
1 2 3 4 5 6 7 8 9 10 11 |
x = 10 if x > 5: # True if x == 6: # False print("nested: x == 6") elif x == 10: # True print("nested: x == 10") else: print("nested: else") else: print("else") |
Exercise 1
What is the output of the following snippet?
1 2 3 4 5 6 |
x = 5 y = 10 z = 8 print(x > y) print(y > z) |
False
True
Exercise 2
What is the output of the following snippet?
1 2 3 4 |
x, y, z = 5, 10, 8 print(x > z) print((y - 5) == x) |
False
True
Exercise 3
What is the output of the following snippet?
1 2 3 4 5 |
x, y, z = 5, 10, 8 x, y, z = z, y, x print(x > z) print((y - 5) == x) |
True
False
Exercise 4
What is the output of the following snippet?
1 2 3 4 5 6 7 8 9 10 11 |
x = 10 if x == 10: print(x == 10) if x > 5: print(x > 5) if x < 10: print(x < 10) else: print("else") |
True
True
else
Exercise 5
What is the output of the following snippet?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
x = "1" if x == 1: print("one") elif x == "1": if int(x) > 1: print("two") elif int(x) < 1: print("three") else: print("four") if int(x) == 1: print("five") else: print("six") |
four
five
Exercise 6
What is the output of the following snippet?
1 2 3 4 5 6 7 8 9 10 11 12 |
x = 1 y = 1.0 z = "1" if x == y: print("one") if y == int(z): print("two") elif x == y: print("three") else: print("four") |
one
two
Ex. 7
What is the output of the following code snippet?
1 2 3 4 5 6 7 8 |
incipit = "It was a dark and stormy night" if 'it' in incipit: print('one') elif 'ht"' in incipit: print('two') else: print('three') |
three
- The program executes the
else
statement and outputsthree
to the console. - The
if
condition isFalse
because the lowercase substring'it'
is not present inincipit
. - The
elif
condition is alsoFalse
, because the substring'ht"'
includes a quotation mark that is not part of the string contained in theincipit
variable.
Ex. 8
What is the output of the following code snippet?
1 2 3 4 5 6 7 8 9 10 11 12 |
a = 3 b = '3' c = 3.0 if int(b) == a: print('A') if a == b: print('B') if c == a: print('C') |