Back to Python
2026-04-275 min read

Example 3: Comparison Operators (Python Programming)

Learn Example 3: Comparison Operators (Python Programming) step by step with clear examples and exercises.

Title: Comparison Operators in Python Programming - A full guide

Why This Matters

Comparison operators are essential in Python programming as they help you compare values and make decisions based on those comparisons. They play a crucial role in conditional statements, loops, and many other aspects of programming. Understanding comparison operators can help you solve real-world problems, ace programming interviews, and even debug common errors in your code.

Prerequisites

Before diving into the world of comparison operators, it's important to have a good understanding of Python syntax, variables, data types, and basic control structures like if-else statements and loops. If you're new to programming or need a refresher, check out our comprehensive guides on Python Basics and Control Structures in Python.

Core Concept

Comparison operators in Python allow you to compare values of variables or expressions. The comparison operators are:

  1. Equal to (==)
  2. Not equal to (!= or !=)
  3. Greater than (>)
  4. Less than (<)
  5. Greater than or equal to (>=)
  6. Less than or equal to (<=)

Equality Operators

The equality operators (== and !=) compare the values of two operands. The == operator checks if the values are equal, while the != operator checks if they are not equal.

x = 5
y = 10
print(x == y) # Output: False
print(x != y) # Output: True

Relational Operators

Relational operators (<, >, <=, and >=) compare the values of two operands to determine a relationship between them.

x = 5
y = 10
print(x < y) # Output: True
print(x > y) # Output: False
print(x <= y) # Output: True
print(x >= y) # Output: False

Order of Precedence

In Python, comparison operators have a higher precedence than arithmetic operators. This means that when multiple operations are involved in an expression, the comparisons will be performed first. For example:

x = 5 + 3 * 2 == 12
print(x) # Output: True

In this case, the multiplication operation (3 * 2) is performed first, and then the addition and comparison operations are carried out.

Type Comparison

Python also provides a way to check if two operands belong to the same data type using the is keyword. This operator checks for object identity rather than value or type conversion.

x = 5
y = 5
print(x is y) # Output: True
z = "5"
print(x is z) # Output: False (Even though x and z have the same value, they are not the same object.)

Note on Assignment Operator

It's worth mentioning that the equals sign (=) in Python is an assignment operator, not a comparison operator. Using == for comparison and = for assignment will help avoid confusion and prevent common mistakes.

Worked Example

Let's create a simple program that takes two numbers as input, compares them, and performs some actions based on the comparison results.

Get user input

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

Compare numbers

if num1 > num2:

print(f"{num1} is greater than {num2}")

elif num1 < num2:

print(f"{num1} is less than {num2}")

else:

print("Both numbers are equal.")


In this example, we use the `>`, `<`, and `==` operators to compare the user-entered numbers. The if-else statements help us determine the relationship between the numbers and output an appropriate message.

Common Mistakes

  1. Using the equals sign (=) instead of the equality operator (==): This can lead to unexpected behavior, as the equals sign is an assignment operator. For example:
x = 5
print(x = 10) # Assigns 10 to x, prints None
print(x == 10) # Output: False (But this should print True.)
  1. Comparing incompatible data types: When comparing operands of different data types, Python attempts to convert them implicitly. However, this can sometimes lead to unexpected results or errors. For example:
x = 5
y = "10"
print(x > y) # Output: False (Even though 5 is greater than 10 as strings, it's not when compared numerically.)
  1. Mixing up the equality and assignment operators: This can cause confusion and lead to bugs in your code. For example:
x = 5
x == 10 # Output: False (This should assign 10 to x, but it doesn't because we're checking for equality.)
print(x) # Output: 5 (The original value of x is still 5.)

Common Mistakes - Subheadings

  • Using the wrong comparison operator
  • Comparing incompatible data types
  • Mixing up equality and assignment operators

Practice Questions

  1. Write a program that checks if a number is even or odd using comparison operators.
  2. Write a program that sorts three numbers in ascending order using comparison operators and the sort() function.
  3. Write a program that finds the maximum of three numbers using comparison operators and the max() function.
  4. Write a program that checks if a given year is a leap year using comparison operators and logical operators (AND, OR).

Practice Questions - Subheadings

  • Checking number parity
  • Sorting numbers in ascending order
  • Finding the maximum of three numbers
  • Determining if a year is a leap year

FAQ

What's the difference between == and = in Python?

  • == is the equality operator, used to compare values.
  • = is the assignment operator, used to assign a value to a variable.

Can I compare strings using arithmetic operators like + or - in Python?

  • No, you should use comparison operators like ==, !=, <, and >. Using arithmetic operators on strings will result in concatenation or an error.

What does the is keyword do in Python?

  • The is keyword checks for object identity rather than value or type conversion. It compares whether two variables point to the same object in memory, not their values.

Why can't I use an arithmetic operator like + to compare strings in Python?

  • In Python, using arithmetic operators on strings will result in concatenation instead of comparison. To compare strings, you should use comparison operators like ==, !=, <, and >.

Is there a way to check if two variables have the same data type in Python?

  • Yes, you can use the type() function or the isinstance() function to check if two variables have the same data type. For example:
x = 5
y = "5"
print(type(x) is type(y)) # Output: False (Even though x and y have the same value, they are not of the same data type.)
Example 3: Comparison Operators (Python Programming) | Python | XQA Learn