Comparison operators - interactively introduced
Comparison operators - interactively introduced#
The comparison operators in python are:
comparison operators (return either
TrueorFalse)==(equal to)!=(not equal to)<(less than)<=(less than or equal to)>(greater than)>=(greater than or equal to)
To illustrate using these, 3 is bigger than 2 so doing:
3 > 2
True
returns True. Using some of the variables defined previously we can try this out a few times:
# Here are the variables from previously
num1 = 10
num2 = -3
num3 = 7.41
num4 = -.6
num5 = 7
num6 = 3
num7 = 11.11
# Are these two expressions not equal to each other?
num3 != num4
True
# Are these two expressions equal to each other?
# Note I use brackets to make it clear what is being compared and the order in which to evaluate.
(num1 + num2) == num5
True
# Is the first expression less than the second expression?
num5 < num6
False
# Is this expression True?
5 > 3
True
# You can do messy things like this. But what does it actually mean? Don't write things like this!
# Simple rule with coding: If it isn't clear what's going on, can you write it in a clearer way?
5 > 3 < 4 == 3 + 1
True