Back to course

Introduction to Comparison Operators

Python Programming: The 0 to Hero Bootcamp

Comparison Operators

Comparison operators are used to compare two values. They always return a Boolean result: True or False. They are the foundation of decision making.

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Examples

python a = 10 b = 5 c = 10

print(f'a equals c? {a == c}') # True print(f'a equals b? {a == b}') # False print(f'a is not equal to b? {a != b}') # True print(f'a is greater than b? {a > b}') # True print(f'b is less than 5? {b < 5}') # False (b is equal to 5) print(f'b is less than or equal to 5? {b <= 5}') # True

Comparing Strings

Strings can also be compared. Comparison is done lexicographically (alphabetical order, based on ASCII/Unicode values).

python print('apple' == 'Apple') # False (due to case sensitivity) print('banana' > 'apple') # True print('cat' < 'dog') # True