Skip to content

Latest commit

 

History

History
474 lines (301 loc) · 5.42 KB

04-Comparison-Operators-in-Python.md

File metadata and controls

474 lines (301 loc) · 5.42 KB

Comparison (Relational) Operators in Python

Learning Objectives

After completing this chapter, you will be able to:

  • Understand Comparison Operators.
  • Explain why comparison operators are used.
  • Learn all comparison operators.
  • Compare different values.
  • Write programs using comparison operators.
  • Understand Boolean results.

Introduction

Comparison Operators are used to compare two values.

The result of every comparison is either:

  • True
  • False

These operators are mainly used in:

  • if statements
  • while loops
  • Decision making
  • Login validation
  • Eligibility checking

Without comparison operators, programs cannot make decisions.


Definition

A Comparison (Relational) Operator compares two operands and returns a Boolean value (True or False).

Example

a = 20
b = 15

print(a > b)

Output

True

Why do we use Comparison Operators?

Comparison operators help to:

  • Compare two values.
  • Make decisions.
  • Control program flow.
  • Validate user input.
  • Check conditions.

Types of Comparison Operators

Operator Description Example
== Equal to a == b
!= Not Equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or Equal to a >= b
<= Less than or Equal to a <= b

1. Equal To (==)

Definition

Returns True if both operands are equal.

Program

a = 10
b = 10

print(a == b)

Output

True

2. Not Equal To (!=)

Definition

Returns True if both operands are different.

Program

a = 10
b = 20

print(a != b)

Output

True

3. Greater Than (>)

Definition

Returns True if the left operand is greater than the right operand.

Program

a = 25
b = 15

print(a > b)

Output

True

4. Less Than (<)

Definition

Returns True if the left operand is smaller than the right operand.

Program

a = 10
b = 25

print(a < b)

Output

True

5. Greater Than or Equal To (>=)

Definition

Returns True if the left operand is greater than or equal to the right operand.

Program

marks = 75

print(marks >= 35)

Output

True

6. Less Than or Equal To (<=)

Definition

Returns True if the left operand is less than or equal to the right operand.

Program

age = 18

print(age <= 25)

Output

True

Program 1: Demonstrate All Comparison Operators

Problem Statement

Write a Python program to demonstrate all comparison operators.


Program

a = 20
b = 10

print("a == b :", a == b)
print("a != b :", a != b)
print("a > b  :", a > b)
print("a < b  :", a < b)
print("a >= b :", a >= b)
print("a <= b :", a <= b)

Output

a == b : False
a != b : True
a > b  : True
a < b  : False
a >= b : True
a <= b : False

Pseudocode

START

Create two variables

Compare using all comparison operators

Display the results

STOP

Line-by-Line Explanation

Line 1

a = 20
b = 10

Creates two integer variables.


Remaining Lines

Each print() statement compares the two values and displays either True or False.


Boolean Result

Comparison operators always return a Boolean value.

Example

print(15 > 5)
print(15 == 5)

Output

True
False

Real-Time Applications

Comparison operators are used in:

  • Login Authentication
  • ATM Applications
  • Banking Software
  • Online Examinations
  • Student Result Systems
  • Age Verification
  • E-Commerce Websites

Advantages

  • Supports decision making.
  • Controls program execution.
  • Produces Boolean values.
  • Easy to understand and use.

Difference between = and ==

= ==
Assignment Operator Comparison Operator
Assigns value Compares values
Does not return True/False Returns True or False

Common Mistakes

Using = Instead of ==

❌ Incorrect

if age = 18:
    print("Eligible")

Output

SyntaxError

✅ Correct

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

Comparing Different Data Types

print(10 == "10")

Output

False

Reason:

One value is an integer and the other is a string.


Interview Questions

1. What are Comparison Operators?

Answer

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


2. How many Comparison Operators are available in Python?

Answer

Six:

  • ==
  • !=
  • <
  • =

  • <=

3. What is the return type of Comparison Operators?

Answer

Boolean (True or False).


4. What is the difference between = and ==?

Answer

  • = assigns a value.
  • == compares two values.

5. Can Comparison Operators compare strings?

Answer

Yes.

Example

print("Apple" == "Apple")

Output

True

Quick Revision

  • == → Equal To
  • != → Not Equal To
  • > → Greater Than
  • < → Less Than
  • >= → Greater Than or Equal To
  • <= → Less Than or Equal To
  • Always returns True or False.

Summary

In this chapter, you learned:

  • What Comparison Operators are.
  • Types of Comparison Operators.
  • Practical examples.
  • Boolean results.
  • Real-world applications.
  • Common mistakes.
  • Interview questions.