Chapter 17 of 20

Comparison Operators in Python

When writing programs, we often need to compare two values.

For example:

  • Is one number greater than another?

  • Are two values equal?

  • Is a user old enough?

  • Did a student score more than 40 marks?

Python provides comparison operators for exactly this purpose.

What are Comparison Operators?

Comparison operators are used to compare two values and return either True or False.

For example:

age = 20

print(age >= 18)

Output:

True

Because 20 is greater than or equal to 18.

Python Comparison Operators

Operator

Meaning

Example

Result

==

Equal to

5 == 5

True

!=

Not equal to

5 != 3

True

>

Greater than

5 > 3

True

<

Less than

3 < 5

True

>=

Greater than or equal to

5 >= 5

True

<=

Less than or equal to

3 <= 5

True

Let's understand them one by one.

Equal to (==)

The == operator checks whether two values are equal.

a = 10
b = 10

print(a == b)

Output:

True

Don't confuse == with =.

x = 10     # Assigns 10 to x
x == 10    # Checks whether x is 10

Not Equal (!=)

The != operator checks whether two values are different.

a = 10
b = 5

print(a != b)

Output:

True

Greater Than (>)

Checks whether the left value is greater than the right value.

age = 20

print(age > 18)

Output:

True

Less Than (<)

Checks whether the left value is smaller than the right value.

age = 16

print(age < 18)

Output:

True

Greater Than or Equal To (>=)

Checks whether a value is greater than or equal to another value.

marks = 40

print(marks >= 40)

Output:

True

This is useful when the boundary value should also be accepted.

Less Than or Equal To (<=)

Checks whether a value is less than or equal to another value.

age = 18

print(age <= 18)

Output:

True

Using Comparison Operators with if

Comparison operators are especially useful with conditional statements.

marks = 75

if marks >= 40:
    print("You passed!")

Here, Python checks whether marks is greater than or equal to 40.

Since it is, the condition becomes True.

Comparing Strings

Comparison operators can also be used with strings.

password = "python123"

if password == "python123":
    print("Correct password")

Output:

Correct password

String comparisons are case-sensitive:

print("Python" == "python")

Output:

False

Because uppercase P and lowercase p are different characters.

Comparing Multiple Conditions

You can combine comparisons using logical operators such as and and or.

age = 25

if age >= 18 and age <= 60:
    print("Eligible")

Both comparisons must be true because we used and.

You can also use comparison chaining in Python:

age = 25

if 18 <= age <= 60:
    print("Eligible")

This is a convenient Python feature and means the same thing as checking both boundaries separately.

A Practical Example

Let's check whether a student passed and received a distinction:

marks = int(input("Enter your marks: "))

if marks >= 90:
    print("Excellent!")
elif marks >= 40:
    print("You passed.")
else:
    print("You failed.")

Here, comparison operators allow Python to make decisions based on the student's marks.

Remember

Comparison operators don't change the values. They simply compare them and give a Boolean result:

True
False

You'll use them constantly with if, elif, loops, and other decision-making logic in Python.