5. Conditionals and comparisons
5.1. Bools and comparison
In Python, bool type is object that is assumes only two values, either their are True or False:
[1]:
x = True
y = False
type(x)
type(y)
[1]:
bool
We get a bool as a result when we check if the value of two variables is the same using == as in the examples below:
[2]:
a = 312
x = (42 == 3) #does 42 equals 3?
y = (a == 311) #does the variable "a" equals 311?
z = (a == 312) #does the variable "a" equals 312?
x
y
z
a == 312.14
type(x)
type(y)
type(z)
type(a == 421.25)
[2]:
bool
Other possible comparisons are, for example, >, <, >= and <=:
[3]:
41 > 33 #is 41 greater than 33?
41 > 41 #is 41 greater than 41
41 >= 41 #is 41 greater or equal to 41?
41 < 33 #is 41 less than 33?
41 < 41 #is 41 less than 41?
41 <= 41 #is 41 less or equal to 41?
[3]:
True
You can also combine the results of many comparisons using or and and:
[4]:
51 > 132 or 31 < 54 #is either (51 greater than 132) or (31 less than 54)?
51 > 123.7 or 31 < 4 #is either (51 greater than 123.7) or (31 less than 4)?
151 > 133 and 141 < 54 #is both (151 greater than 133) and (141 less than 54)?
410 > 133 and 45.2 < 45.7 #is both (410 greater than 133) or (45.2 less than 45.7)?
[4]:
True
5.2. Ifs
The most simple conditional expression in Python is an if, written as below:
[5]:
x = True
if x:
print("x is True")
x is True
Here, the code indented bellow the if clause will execute if and only if the condition evaluates to True. Try changing the value of x to False. Here’s another example:
[6]:
x = 3.1
if x == 3.5:
print("x == 3.5 is True")
print("therefore, x equals 3.5")
There’s also the else’s clauses:
[7]:
x = True
if x:
print("x is True")
else:
print("x is False")
y = False
if y:
print("y is True")
else:
print("y is False")
if 44 > 41:
print("44 is greater than 41 =]")
else:
print("44 is not greater than 41 =[")
x is True
y is False
44 is greater than 41 =]
And if elif’s clauses:
[8]:
x = 1
if x == 1:
print("x is 1")
elif x == 0:
print("x is 0")
else:
print("x is neither 1 nor 0")
x is 1
5.3. Int and bools
You can directly place an int object as a condition in a loop and it will evaluate to True if and only if it’s different from 0:
[9]:
x = 1
y = 0
z = 2
w = -13
if x:
print('x "evaluates" to True')
if y:
print('y "evaluates" to True')
if z:
print('z "evaluates" to True')
if w:
print('w "evaluates" to True')
x "evaluates" to True
z "evaluates" to True
w "evaluates" to True
5.4. Is keyword
Consider the following code:
[10]:
x = [3, 2, 4]
y = [3, 2, 4]
v = [3, 4, 2]
w = [3, 1, 4]
z = x
if x == y:
print("x == y evaluates to True")
if x == v:
print("x == v evaluates to True")
if x == w:
print("x == w evaluates to True")
if x == z:
print("x == z evaluates to True")
if y == w:
print("y == w evaluates to True")
if y == z:
print("y == z evaluates to True")
x == y evaluates to True
x == z evaluates to True
y == z evaluates to True
Everything is as expected here. Two lists are equal if and only if they have the same elements and they are stored in the same order.
Consider however this:
[11]:
x = [3, 2, 4]
y = [3, 2, 4]
v = [3, 4, 2]
w = [3, 1, 4]
z = x
if x is y:
print("x and y are the same object")
if x is v:
print("x and v are the same object")
if x is w:
print("x and w are the same object")
if x is z:
print("x and z are the same object")
if y is w:
print("y and w are the same object")
if y is z:
print("y and z are the same object")
x and z are the same object
As it implies, is will give you True only if the two variables reference the same object. So, it’s equivalent to comparing their id’s:
[12]:
x = [3, 2, 4]
y = [3, 2, 4]
v = [3, 4, 2]
w = [3, 1, 4]
z = x
if id(x) == id(y):
print("x and y are the same object")
if id(x) == id(v):
print("x and v are the same object")
if id(x) == id(w):
print("x and w are the same object")
if id(x) == id(z):
print("x and z are the same object")
if id(y) == id(w):
print("y and w are the same object")
if id(y) == id(z):
print("y and z are the same object")
x and z are the same object