Python Mastery – From Basics to Brilliance / Operators in Python

What Are Comparison Operators?

Comparison operators are used to compare two values.

They always return either True or False, which are called Boolean values.

Think of them like questions you ask your program:

“Is this equal?”, “Is this bigger?”, “Is this not the same?”


Operator

Symbol

What It Means

Example

Equal to

==

Checks if two values are equal

a == b

Not equal to

!=

Checks if two values are not equal

a != b

Greater than

>

Checks if left side is bigger

a > b

Less than

<

Checks if left side is smaller

a < b

Greater or equal to

>=

Left side is bigger or equal

a >= b

Less or equal to

<=

Left side is smaller or equal

a <= b


== Equal To

x = 5
x == 5 # Output: True

!= Not Equal To

x = 5
x != 3 # Output: True

> Greater Than

x = 8
x > 5 # Output: True

< Less Than

x = 3
x < 5 # Output: True

>= Greater Than or Equal To

x = 5
x >= 5 # Output: True

<= Less Than or Equal To

x = 4
x <= 5 # Output: True