Hello, Stellar Navigator! Today, we're diving into Python Comparison Operators. These operators allow us to compare two values, yielding a boolean
result — True
or False
. Our goal is to understand these operators and become comfortable with operator chaining. Are you ready? Let's delve in!
Comparison operators in Python are used to compare two values and yield a boolean
outcome. The comparison operators in Python are:
==
)
=
and ==
are two different operators - the former is an assignment operator (e.g., variable = 2
), and the latter is the equality comparison.!=
)>
)<
)>=
)<=
)For instance, if you have $20
and want to buy a $15
toy, see the example below:
Python1price_of_toy = 15 2money_in_hand = 20 3 4# Compare two variables and assign the result to a new variable 5can_buy_toy = money_in_hand >= price_of_toy 6print(can_buy_toy) # Prints: True
Here, we use the >=
comparison operator to check if we have enough money to buy the toy — the answer is True
! Note how we assigned the result of the comparison to a new variable - can_buy_toy.
Let's investigate these operators further with some coding examples:
Python1print(10 == 10) # True - because 10 is indeed equal to 10 2print(10 > 10) # False - because 10 isn't greater than 10 3print(10 < 10) # False - because 10 isn't less than 10 4print(10 != 10) # False - because 10 isn't unequal to 10 5print(10 >= 10) # True - because 10 is equal to 10 6print(10 <= 10) # True - because 10 is equal to 10
The result of these comparison operators is a boolean
value — True
or False
.
Python allows us to chain together comparisons. This technique can be handy when we want to perform multiple comparisons in a single line:
Python1book_pages = 150 2is_desired_book = 100 < book_pages < 200 # Check if the number of pages is between 100 and 200, exclusive 3print(is_desired_book) # Prints: True
In this case, we've used operator chaining to verify whether a book that's 150 pages long falls within the range of 100 to 200 pages. The answer is True
!
Let's apply comparison operators to numbers and strings:
Python1# Numbers 2number = 5 3print(number == 5) # True - 5 is equal to 5 4print(number > 3) # True - 5 is greater than 3 5print(number < 3) # False - 5 is not less than 3 6print(number >= 5) # True - 5 is equal to 5 7 8# Strings 9fruit = "apple" 10print(fruit == "apple") # True - "apple" is the same as "apple" 11print(fruit != "banana") # True - "apple" is not "banana" 12print(fruit < "banana") # True - "apple" comes before "banana" in the dictionary
In Python, strings are compared alphabetically. To provide a real-life example - imagine both words in the word dictionary, which one will go first there? This word is less than the other one!
Congratulations! Today, we navigated through the Python comparison operators and the concept of operator chaining. We saw them in action with some practical examples. Our upcoming exercises provide numerous opportunities for you to practice these concepts. Remember, each correct solution brings you closer to mastering Python. Happy coding!