Comparisons

2.2. Comparisons#

In Python, when we compare values, the results return a Boolean.

x = 10
print(x < 0)
False

We can compare values using the following operators:

  • < less than

  • > greater than

  • <= less than or equal to

  • >= greater than or equal to

  • == check values are equal

  • != check values are not equal

Question 1

Does the following program return True or False?

x = 2
print(x > 3)
Solution

False. x is not greater than 3.

Question 2

Does the following program return True or False?

x = 7.2
print(x <= 7.2)
Solution

Solution is locked

Question 3

Does the following program return True or False?

x = 2 + 4
print(x == 6)
Solution

Solution is locked

Question 4

Does the following program return True or False?

x = 4*3
print(x != 5)
Solution

Solution is locked

Question 5

Does the following program return True or False?

x = 2**2
print(x != 4)
Solution

Solution is locked

Question 6

Does the following program return True or False?

x = 10
print((x - 1) % 3 == 0 )
Solution

Solution is locked

Question 7

Does the following program return True or False?

x = 'red'
print(x == 'red')
Solution

Solution is locked