python-operadores-comparacion

Comparison Operators in Python

  • 2 min

Comparison operators in Python allow us to compare two values and determine if they are equal, different, greater, or less:

The results of comparisons are boolean values (True or False), and they are fundamental for control flow logic.

OperatorNameDescription
==Equal toCompares if two values are equal
!=Not equal toCompares if two values are different
>Greater thanChecks if one value is greater than another
<Less thanChecks if one value is less than another
>=Greater than or equal toChecks if one value is greater than or equal to another
<=Less than or equal toChecks if one value is less than or equal to another

If you want to learn more, check out the Introduction to Programming Course

List of comparison operators

Equality ==

The equality operator (==) is used to check if two values are equal.

a = 5
b = 5
is_equal = (a == b)  # True, since a and b are equal
Copied!

Inequality !=

The inequality operator (!=) is used to check if two values are not equal.

a = 5
b = 3
is_not_equal = (a != b)  # True, since a is not equal to b
Copied!

Greater than >

The greater than operator (>) is used to check if one value is greater than another.

a = 10
b = 5
is_greater = (a > b)  # True, since a is greater than b
Copied!

Less than <

The less than operator (<) is used to check if one value is less than another.

a = 3
b = 7
is_less = (a < b)  # True, since a is less than b
Copied!

Greater than or equal to >=

The greater than or equal to operator (>=) is used to check if one value is greater than or equal to another.

a = 10
b = 10
is_greater_equal = (a >= b)  # True, since a is equal to b
Copied!

Less than or equal to <=

The less than or equal to operator (<=) is used to check if one value is less than or equal to another.

a = 5
b = 7
is_less_equal = (a <= b)  # True, since a is less than b
Copied!