
10 Types of Programming Languages Every Coder should Know
Learn about different types of programming languages and how they are implemented in the real world.

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.
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:


POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
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:

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:

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:


82.9%
of professionals don't believe their degree can help them get ahead at work.
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:

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:

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:

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.
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 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:

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:

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:

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:

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.
Updated on July 31, 2024

Learn about different types of programming languages and how they are implemented in the real world.

Explore 10 front-end development, including key languages, its advantages and disadvantages, and how it shapes user experience in web design and functionality.