Language: EN

python-operadores-comparacion

Comparison Operators in Python

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

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
<=Less than or equal toChecks if one value is less than or equal

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

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

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

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

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

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