Introduction and Topic Overview

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!

Understanding Python Comparison Operators
Deep Dive into Python Comparison Operators

Let's investigate these operators further with some coding examples:

Python
print(10 == 10)  # True - because 10 is indeed equal to 10
print(10 > 10)   # False - because 10 isn't greater than 10
print(10 < 10)   # False - because 10 isn't less than 10
print(10 != 10)  # False - because 10 isn't unequal to 10
print(10 >= 10)  # True - because 10 is equal to 10
print(10 <= 10)  # True - because 10 is equal to 10

The result of these comparison operators is a boolean value — True or False.

Expanding the Horizons - Operator Chaining

Python allows us to chain together comparisons. This technique can be handy when we want to perform multiple comparisons in a single line:

Python
book_pages = 150
is_desired_book = 100 < book_pages < 200 # Check if the number of pages is between 100 and 200, exclusive
print(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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal