21
NovComparison Operators Python
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 to | operand1 == 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
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
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
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
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
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
Output
True
True
False
True
True
True