Custom Search

PYTHON:

Relational or Comparison Operators

A relational or comparison operator is an operator that tests some kind of relation between two operands - comparing the values of the two operands.

==
Do they equal each other?
x == y

Here we are comparing the equality of two operands - not setting 'x' equal to 'y'

x = 5

y = 3

print(x == y)

This returns 'False' because 5 is not equal to 3

Whereas if we just use a single '=' sign we are assigning a value not comparing two.

x = 5
y = 3

x=y

print(x)

print(y)

This returns 3 for each of x and y because we have made x equal to y (the value of 3).

!=
Are they not equal to each other?
x != y

Note that the exclamation mark indicates 'not'

x = 5

y = 3

print(x != y)

This returns 'True' because 5 is not equal to 3

>
Greater than
x > y
 
<
Less than
x < y
 
>=
Greater than or equal to
x >= y
 
<=
Less than or equal to
x <= y