Comparison Operators Python

Comparison Operators Python

04 May 2024
Beginner
2.97K Views
12 min read
Learn with an interactive course and practical hands-on labs

Free Data Structures & Algorithms Online Course

Comparison Operators in Python: An Overview

Python, a flexible and powerful programming language, includes a number of operators for performing various operations on variables and values. Previously we have seen types of operators in Python. The comparison operators, which allow developers to compare two values and make decisions based on the result.

In this article, we will explore the comparison operators in Python and provide examples to illustrate their usage. If want more concepts regarding the Python and operators, you must explore a Python tutorial, which provides you each and every concept of Python in detail with hands on examples.

What are the Comparison Operators in Python?

Comparison operators, also known as relational operators in Python, are used to compare two operands. Depending on whether the comparison condition is true or false, they return a Boolean True or False. These operators help determine the relationships between variables, enabling the creation of conditional statements in Python code.

Python provides several comparison operators to facilitate these comparisons. The table below describes the various comparison operators in Python, including their names, descriptions, and syntax -

Operator Name Syntax
== Equal tooperand1 == operand2
!= Not equal to operand1 != operand2
>Greater than operand1 > operand2
<Less than operand1 < operand2
>=Greater than or Equal to operand1 >= operand2
<=Less than or Equal to operand1 <= operand2

Read More - 50 Python Interview Questions

Types of Comparison Operators

1. Equal to (==)

The equality operator (==) is used to check if two values are equal. It returns True if the values are equal, and False otherwise.

Syntax

operand1 == operand2

Example

x = 5
y = 7
result = x == y
print(result)

Explanation

In this example in the Python Compiler, two variables, x and y, are assigned the values 5 and 7, respectively. The equality operator (==) to compare the values of x and y. The result of this comparison is then stored in the variable result. 

Finally, the print(result) statement displays the outcome of the comparison, which will be False in this case since 5 is not equal to 7.

Output

False

2. Not equal to (!=)

The not equal operator (!=) checks if two values are not equal. It returns True if they are not equal and False if they are.

Syntax

operand1 != operand2

Example

a = 10
b = 10
result = a != b
print(result) 

Explanation

Example uses the not equal to (!=) comparison operator to check if the values of a and b are not equal. The result of this comparison is stored in the variable result, and the final output is displayed using the print statement. 
In this specific case, since a is equal to b, the result will be False, and the output will be False.

Output

False

Read More - Python Developer Salary

3. Greater than (>)

The greater-than operator (>) compares if the left operand is greater than the right operand. It returns True if the condition is met and False otherwise.

Syntax

operand1 > operand2

Example

m = 15
n = 10
result = m > n
print(result)

Explanation

The given example compares two variables, m and n, using the greater-than (>) operator. It assigns the result of the comparison to the variable result. 
In this specific case, since the value of m (15) is indeed greater than the value of n (10), the variable result will be assigned the value True.

Output

True

4. Less than (<)

The less-than operator (<) checks if the left operand is less than the right operand. It returns True if the condition is satisfied and False otherwise.

Syntax

operand1 < operand2

Example

p = 25
q = 30
result = p < q
print(result)

Explanation

The example uses the less-than (<) comparison operator to check if the value of p is less than the value of q. The result of this comparison is stored in the variable result, which is then printed.

Output

True

5. Greater than or equal to (>=)

The greater-than-or-equal-to operator (>=) checks if the left operand is greater than or equal to the right operand. It returns True if the condition holds true and False otherwise.

Syntax

operand1 >= operand2

Example

alpha = 12
beta = 12
result = alpha >= beta
print(result)

Explanation

The example in the above Python Editor uses the greater-than-or-equal-to comparison operator (>=) to check if the value stored in the variable alpha is greater than or equal to the value in the variable beta. 
The result of this comparison is stored in the variable result, and the outcome is printed to the console.

Output

True

6. Less than or equal to (<=)

The less-than-or-equal-to operator (<=) determines if the left operand is less than or equal to the right operand. It returns True if the condition is met and False otherwise.

Syntax

operand1 <= operand2

Example

gamma = 8
delta = 10
result = gamma <= delta
print(result)

Explanation

The example utilizes the less-than-or-equal-to (<=) comparison operator to check if the value of gamma is less than or equal to the value of delta. The result of this comparison is stored in the variable result.

Output

True

Examples of Comparison Operators in Python Online Compiler

# Example 1: Equal to
x = 5
y = 5
result = x == y
print(result) 

# Example 2: Not equal to
a = 10
b = 15
result = a != b
print(result) 

# Example 3: Greater than
m = 2
n = 15
result = m > n
print(result)

# Example 4: Less than
p = 8
q = 12
result = p < q
print(result) 

# Example 5: Greater than or equal to
alpha = 10
beta = 10
result = alpha >= beta
print(result)

# Example 6: Less than or equal to
gamma = 5
delta = 7
result = gamma <= delta
print(result) 

Explanation

The example demonstrates the usage of various comparison operators in Python. Each example compares two variables using a different comparison operator and prints the result.

Output

True
True
False
True
True
True
Summary
Comparison operators in Python are crucial for creating dynamic and responsive programs. They allow developers to compare values and make decisions based on these comparisons. Understanding the types of comparison operators and their usage is fundamental for writing effective Python code.
This was all about the basics of logical operators in Python, if you want to go deeper into data types then consider enrolling in a Python Certification Course.

FAQs

Yes, Python's comparison operators can be used with various data types, including numbers, strings, and other objects. However, it's essential to consider the compatibility of the data types being compared.

Yes, Python supports shorthand notations like +=, -=, *=, and /= for certain operations, but not specifically for comparison operators. The standard comparison operators are used as described in the examples.

The six comparison operators are 1) == or equal to, 2) != or not equal to, 3) > or greater than, 4) >= or greater than or equal to, 5) < or less than, and 6) <= or less than or equal to. They can be used to compare different values in Python, such as integers or strings.
Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this