Comparisons - π#
We need to be able to compare different variables. We will be working on:
Are these things the same?
Are these things not the same?
How do these things compare?
We can compare any data type, and our output will be a boolean (True or False). The other things we will cover are:
Comparing different data types
Making multiple comparisons at once
Comparison operators are important on their own (how do these things compare?) and are also useful for sorting and switching (see the next notebook).
Are these things the same?#
Numeric Comparisons#
We have already initiated variables by setting something equal to something else - letβs do that here by setting kitten π equal to 10 and then setting dog π equal to kitten π. Finally, π bee will be equal to 11.
Soβ¦
π = 10
π = π
π = 11
kitten = 10
dog = kitten
bee = 11
print( "kitten =", kitten, "; dog =", dog, "; bee = ", bee )
kitten = 10 ; dog = 10 ; bee = 11
The first comparison operator is β==β, which tests to see if two variables are equal.
print( "kitten =", kitten, "; dog =", dog, "; bee = ", bee )
print( "Is kitten equal to dog?")
print( kitten == dog )
print( "Is kitten equal to bee?")
print( kitten == bee )
kitten = 10 ; dog = 10 ; bee = 11
Is kitten equal to dog?
True
Is kitten equal to bee?
False
This tells us that kitten is equal to dog, because it returns True and kitten is not equal to bee, as that returns False.
Character Comparisons#
We can also do comparisons with other variable types. Hereβs an example with strings instead of integers.
Letβs think about some foods, how about:
food1 = π
food2 = πͺ
food3 = π
food1 = 'apple'
food2 = 'cookie'
food3 = 'apple'
print( "food1=", food1,"; food2 =", food2,"; food3 = ", food3 )
print( "Is food1 equal to food2?")
print( food1 == food2 )
print( "Is food1 equal to food3?")
print( food1 == food3 )
food1= apple ; food2 = cookie ; food3 = apple
Is food1 equal to food2?
False
Is food1 equal to food3?
True
Are these things different?#
This is Logical⦠NOT!#
We can also test to see if two values are not equal using the β!=β operator.
print( "food1 =", food1,"; food2 =", food2,"; food3 =", food3 )
print( "Is food1 not equal to food2?")
print( food1 != food2 )
print( "Is food1 not equal to food3?")
print( food1 != food3 )
food1 = apple ; food2 = cookie ; food3 = apple
Is food1 not equal to food2?
True
Is food1 not equal to food3?
False
This gives us the opposite of what we had before.
So, what did we learn?
π == π = True
π != πͺ = True
How do these things compare?#
Math Comparisons 101#
We can also compare the magnitude of values using β<β, β<=β, β>βand β>=β, which will return βTrueβ if the condition is being met.
print( "kitten =", kitten, "; dog =", dog, "; bee = ", bee )
kitten = 10 ; dog = 10 ; bee = 11
print( "Is kitten less than dog?")
print( kitten < dog )
print( "Is kitten less than or equal to dog?")
print( kitten <= dog )
print( "Is kitten greater than or equal to dog?")
print( kitten >= dog )
print( "Is kitten greater than dog?")
print( kitten > dog )
Is kitten less than dog?
False
Is kitten less than or equal to dog?
True
Is kitten greater than or equal to dog?
True
Is kitten greater than dog?
False
Note
We do have to watch out for our types. Characters and numerics are not the same.
TheCharacters = "10"
TheNumbers = 10
print( "Is TheNumbers equal to TheCharacters?")
print( TheNumbers == TheCharacters )
print( "TheNumbers type is ", type( TheNumbers ), "; and TheCharacters type is ", type( TheCharacters ) )
Is TheNumbers equal to TheCharacters?
False
TheNumbers type is <class 'int'> ; and TheCharacters type is <class 'str'>
We can compare integers and floats (!) but not other disparate data types.
If you let python take care of your data-types, be warned that they could be different from what you think they are!
Toy (Car) Problem#
To start thinking of these concepts from a logical perspective, letβs create a toy (car) problem. Here are a bunch of toy cars with various colors and costs. Here is how they labeled.
auto:
car
truck
color:
red
blue
yellow
white
black
cost:
1 <= cost <= 5
Note
varible = varible is not the same thing as variable == variable
varible = varible will always return true
Multiple Comparisons#
We can make multiple comparisons at once by stringing the statements
and
not
or
together.
The individual testable (true/false) components need to be broken apart. For example,
If the V CATA bus is coming around the corner, then I need to run towards the bus stop.
requires several things for it to be true and to require running. We can break these things out with:
If there is a vehicle coming around the corner AND that vehicle is a CATA bus AND that CATA bus is a V
then I need to run towards the bus stop
We will only run towards the bus stop if all of the statements are true.
AND#
Note
the and operator will return True if all of the conditions are met
Letβs create another scenario for this around clothes. For this, letβs assume:
face = π
shirt = π
pants = π
face = "sunglasses"
shirt = "tshirt"
pants = "jeans"
print ( "Am I wearing sunglasses and jeans?" )
print (face == "sunglasses")
print (pants == "jeans")
print( (face == "sunglasses") and (pants == "jeans") )
print ( "Am I wearing sweater and jeans?" )
print (shirt == "sweater")
print (pants == "jeans")
print( (shirt == "sweater") and (pants == "jeans") )
Am I wearing sunglasses and jeans?
True
True
True
Am I wearing sweater and jeans?
False
True
False
We can also string as many comparisons together as we want.
print( (1 < 2) and (1 < 3) and (1 < 4) and (1 < 5) and (1 < 6) and (1 < 7) and (1 < 8) )
True
OR#
Note
the or operator will return True if at least 1 of the conditions is met
print( "face =", face, "; shirt =", shirt, "; pants = ", pants )
print ( "Am I wearing sunglasses or jeans?" )
print (face == "sunglasses")
print (pants == "jeans")
print( (face == "sunglasses") or (pants == "jeans") )
print ( "Am I wearing sweater or jeans?" )
print (shirt == "sweater")
print (pants == "jeans")
print( (shirt == "sweater") or (pants == "jeans") )
face = sunglasses ; shirt = tshirt ; pants = jeans
Am I wearing sunglasses or jeans?
True
True
True
Am I wearing sweater or jeans?
False
True
True
Not#
Note
the not will reverse or switch the meaning of the and/or operators
print( "face =", face, "; shirt =", shirt, "; pants = ", pants )
print ( "Am I wearing sunglasses and not jeans?" )
print (face == "sunglasses")
print (not (pants == "jeans"))
print( (face == "sunglasses") and not (pants == "jeans") )
print ( "Am I wearing jeans and not a sweater?" )
print (not (shirt == "sweater"))
print (pants == "jeans")
print( not (shirt == "sweater") and (pants == "jeans") )
face = sunglasses ; shirt = tshirt ; pants = jeans
Am I wearing sunglasses and not jeans?
True
False
False
Am I wearing jeans and not a sweater?
True
True
True
Try It Out!#
Try to fill in code to fulfill the request! Here are some variables used in the exercise
dogA_color = 'brown'
dogA_mass = 42
dogA_sex = 'male'
dogA_age = 5
dogA_name = 'chip'
dogB_color = 'white'
dogB_mass = 19
dogB_sex = 'female'
dogB_age = 2
dogB_name = 'lady'
Is dogA the same color as dogB? (False)
# Example:
print( dogA_color == dogB_color )
False
Does dogA have the same name as dogB? (False)
# Try it out here:
Is dogA older than dogB? (True)
# Try it out here:
Is dogA the same sex as dogB? (False)
# Try it out here:
Is dogA heavier than dogB and have a different name than dogB? (True)
# Try it out here:
Does dogA have a different age than dogB and not a different sex than dogB? (False)
# Try it out here: