Ever wondered how we compare values in Python? Let’s imagine we have to find the greater of two integers or see if two numbers are equal. We use comparison operators in Python to make these comparisons. Understanding these operators is crucial for controlling the flow of our code and making decisions.

Comparison operators help us evaluate conditions. They return either True or False, making them essential for if statements, loops, and more. Let’s dive into the types of comparison operators and see how they work with examples.

Equality Operator (==) with Syntax and Examples

The equality operator checks if two values are equal.

Syntax: a == b

Example

# Python program to check the equality of two numbers
# Taking input from the user
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
# Checking equality
result = (x == y)
# Displaying the result
print(f"Is {x} equal to {y}? {result}")

Output:

Get curriculum highlights, career paths, industry insights and accelerate your technology journey.

Download brochure

Inequality Operator (!=) with Syntax and Examples

The inequality operator checks if two values are not equal.

Syntax: a != b

Example

# Python program to check the inequality of two numbers
# Taking input from the user
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
# Checking inequality
result = (x != y)
# Displaying the result
print(f"Is {x} not equal to {y}? {result}")

Output:

Greater Than Operator (>) with Syntax and Examples

The greater-than operator checks if the left operand is greater than the right one.

Syntax: a > b

Example

# Python program to check if one number is greater than another
# Taking input from the user
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
# Checking if x is greater than y
result = (x > y)
# Displaying the result
print(f"Is {x} greater than {y}? {result}")

Output:

Less Than Operator (<) with Syntax and Examples

Ever needed to check if one value is smaller than another in Python? The less-than-operator is your friend.

Syntax: a < b

Example

# Python program to check if one number is less than another
# Taking input from the user
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
# Checking if x is less than y
result = (x < y)
# Displaying the result
print(f"Is {x} less than {y}? {result}")

Output:

Greater Than or Equal To Operator (>=) with Syntax and Examples

Want to check if one value is greater than, or exactly equal to, another? What we use is the ‘>=’ operator.

Syntax: a >= b

Example

# Python program to check if one number is greater than or equal to another
# Taking input from the user
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
# Checking if x is greater than or equal to y
result = (x >= y)
# Displaying the result
print(f"Is {x} greater than or equal to {y}? {result}")

Output:

Less Than or Equal To Operator (<=) with Syntax and Examples

How about checking whether one value is smaller than, or even equal to, another? That would require the less than or equal operator.

Syntax: a <= b

Example

# Python program to check if one number is less than or equal to another
# Taking input from the user
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
# Checking if x is less than or equal to y
result = (x <= y)
# Displaying the result
print(f"Is {x} less than or equal to {y}? {result}")

Output:

Handling Multiple Comparison Operators in Python

Sometimes, we need to check multiple conditions in one go. This is where chaining comparison operators in Python becomes super useful.

Syntax: a < b < c

Example

# Python program to chain comparison operators
# Taking input from the user
x = int(input("Enter a number: "))
# Checking multiple conditions
result1 = (1 < x < 10)
result2 = (10 <= x <= 20) result3 = (x != 5 > 2)
# Displaying the results
print(f"Is {x} between 1 and 10? {result1}")
print(f"Is {x} between 10 and 20? {result2}")
print(f"Is {x} not equal to 5 and greater than 2? {result3}")

Output:

Comparison of Different Data Types in Python

We often need to compare different data types. Whether it’s numbers, strings, or even lists, comparison operators in Python help us make these checks seamlessly.

Comparing Integers and Floats

Let’s start with the basics – comparing numbers.

Example:

# Python program to compare integers and floats
# Taking input from the user
x = float(input("Enter the first number (integer or float): "))
y = float(input("Enter the second number (integer or float): "))
# Performing comparisons
result1 = (x == y)
result2 = (x != y)
result3 = (x > y)
result4 = (x < y)
# Displaying the results
print(f"Is {x} equal to {y}? {result1}")
print(f"Is {x} not equal to {y}? {result2}")
print(f"Is {x} greater than {y}? {result3}")
print(f"Is {x} less than {y}? {result4}")

Output:

Comparing Strings

Comparing strings can be tricky, as it depends on the lexicographical order (alphabetical order).

Example:

# Python program to compare strings
# Taking input from the user
a = input("Enter the first string: ")
b = input("Enter the second string: ")
# Performing comparisons
result1 = (a == b)
result2 = (a != b)
result3 = (a > b)
result4 = (a < b)
# Displaying the results
print(f"Is '{a}' equal to '{b}'? {result1}")
print(f"Is '{a}' not equal to '{b}'? {result2}")
print(f"Is '{a}' greater than '{b}'? {result3}")
print(f"Is '{a}' less than '{b}'? {result4}")

Output:

Comparing Complex Numbers

Python supports equality and inequality comparisons for complex numbers, but not greater or less comparisons.

<strong>Example</strong>:
# Python program to compare complex numbers
# Defining complex numbers
a = complex(input("Enter the first complex number (format: x+yj): "))
b = complex(input("Enter the second complex number (format: x+yj): "))
# Performing comparisons
result1 = (a == b)
result2 = (a != b)
# Displaying the results
print(f"Is {a} equal to {b}? {result1}")
print(f"Is {a} not equal to {b}? {result2}")

Output:

Comparing Booleans

In Python, True is greater than False because True is treated as 1 and False as 0.

Example:

# Python program to compare boolean values
# Taking input from the user
a = bool(int(input("Enter the first boolean (0 or 1): ")))
b = bool(int(input("Enter the second boolean (0 or 1): ")))
# Performing comparisons
result1 = (a == b)
result2 = (a != b)
result3 = (a > b)
result4 = (a < b)
# Displaying the results
print(f"Is {a} equal to {b}? {result1}")
print(f"Is {a} not equal to {b}? {result2}")
print(f"Is {a} greater than {b}? {result3}")
print(f"Is {a} less than {b}? {result4}")

Output:

Comparing Sequences: Lists, Tuples, and Dictionaries

Python compares sequences like lists and tuples element by element.

Example:

# Python program to compare lists
# Defining lists
a = [1, 2, 3]
b = [1, 2, 4]
# Performing comparisons
result1 = (a == b)
result2 = (a != b)
result3 = (a > b)
result4 = (a < b)
# Displaying the results
print(f"Is {a} equal to {b}? {result1}")
print(f"Is {a} not equal to {b}? {result2}")
print(f"Is {a} greater than {b}? {result3}")
print(f"Is {a} less than {b}? {result4}")

Output:

Conclusion

Comparison operators in Python are elementary concepts that help us evaluate expressions and enforce choices in our code. Be it a comparison between numbers, strings, or complex data types, good knowledge of these operators goes a long way in writing efficient and readable code. Keep practising; try different data types to shore up understanding and prowess. Proper insight into how comparison operators work gives us the ability to make our Python programs dynamic and responsive.

FAQs

How do comparison operators work with different data types in Python?

Comparison operators evaluate different data types based on their inherent properties, such as numerical value or lexicographical order.

Can we compare strings using comparison operators in Python?

Yes, we can compare strings using comparison operators in Python. The comparison is based on lexicographical order.

Are the comparison operators case-sensitive when comparing strings?

Yes, Python considers comparison operators as case-sensitive while comparing strings. For Python, the uppercase and lowercase letters are not alike.

In Python, how may non-numeric types such as lists or tuples be compared?

To compare non-numeric types like lists or tuples, Python considers their elements' values and their positions in the sequence during the comparison process.

Can we use comparison operators to compare complex numbers in Python?

Yes, but only for equality and inequality. Python does not support greater than or less than comparisons for complex numbers.

Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.