Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

Comparison Operators in Python with Syntax and Examples

Basics of Python
Basics of Python
icon
5 Hrs. duration
icon
9 Modules
icon
1800+ Learners
logo
Start Learning

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:

output

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:

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:

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:

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:

output

DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

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:

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:

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:

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:

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:

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:

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:

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
Comparison operators evaluate different data types based on their inherent properties, such as numerical value or lexicographical order.
Yes, we can compare strings using comparison operators in Python. The comparison is based on lexicographical order.
Yes, Python considers comparison operators as case-sensitive while comparing strings. For Python, the uppercase and lowercase letters are not alike.
To compare non-numeric types like lists or tuples, Python considers their elements' values and their positions in the sequence during the comparison process.
Yes, but only for equality and inequality. Python does not support greater than or less than comparisons for complex numbers.

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

24 October, 7:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
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.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved