Comparison operators in Python help you evaluate relationships between values. They are essential for making decisions in your code, like checking equality or determining which value is larger. Understanding these operators is key to effective programming.
Equal to (==)
True
if the values are equal, otherwise returns False
.5 == 5
evaluates to True
.Not equal to (!=)
True
if the values are different, otherwise returns False
.5 != 3
evaluates to True
.Greater than (>)
True
if the first value is greater, otherwise returns False
.10 > 5
evaluates to True
.Less than (<)
True
if the first value is less, otherwise returns False
.3 < 7
evaluates to True
.Greater than or equal to (>=)
True
if the first value is greater than or equal to the second.5 >= 5
evaluates to True
.Less than or equal to (<=)
True
if the first value is less than or equal to the second.4 <= 6
evaluates to True
.Identity operator (is)
True
if both variables refer to the same object, otherwise False
.a is b
checks if a
and b
are the same object.Negated identity operator (is not)
True
if the variables refer to different objects, otherwise False
.a is not b
checks if a
and b
are different objects.