Python - Relational Operators

Python

Share
python logo

In this tutorial we will learn about Relational operators in Python.

We use relational operators to compare values. The result is in boolean form.

Following are the list of Relational operators in Python

Equals

We use the == sign to find if two values are equal.

In the following Python program we are checking equality of two values.

# variables
x = 10
y = 10

# operation
result = x == y

# output
print("Result:", result)

The above code will give the following result.

Result: True

Not equals

We use the != sign to find if two values are not equal.

In the following Python program we are checking whether two values are not equal.

# variables
x = 10
y = 20

# operation
result = x != y

# output
print("Result:", result)

The above code will give the following result.

Result: True

Greater than

We use the > sign to find if left side value is greater than the right side value.

In the following Python program we are checking whether x is greater than y.

# variables
x = 20
y = 10

# operation
result = x > y

# output
print("Result:", result)

The above code will give the following result.

Result: True

Less than

We use the < sign to find if left side value is less than the right side value.

In the following Python program we are checking whether x is less than y.

# variables
x = 10
y = 20

# operation
result = x < y

# output
print("Result:", result)

The above code will give the following result.

Result: True

Greater than or equal to

We use the >= sign to find if left side value is greater than or equal to the right side value.

In the following Python program we are checking whether x is greater than or equal to y.

# variables
x = 20
y = 10

# operation
result = x >= y

# output
print("Result:", result)

The above code will give the following result.

Result: True

Less than or equal to

We use the <= sign to find if left side value is less than or equal to the right side value.

In the following Python program we are checking whether x is less than or equal to y.

# variables
x = 10
y = 20

# operation
result = x <= y

# output
print("Result:", result)

The above code will give the following result.

Result: True
Share