Nearby lessons

31 of 159

Python - Relational Operators

📌 What You Will Learn
  • Define relational operators and what they compare
  • Use the six comparison operators >, >=, <, <=, ==, and !=
  • Compare strings using their Unicode values
  • Chain relational operators in a single expression
  • Use relational operators in if statements and while loops
  • Distinguish the = assignment operator from the == comparison operator

Introduction

Relational operators are used to compare two values.

The result of a relational operation is always a Boolean value:

  • True
  • False

Relational Operators List

Operator Meaning
>Greater Than
>=Greater Than or Equal To
<Less Than
<=Less Than or Equal To
==Equal To
!=Not Equal To

1. Greater Than Operator (>)

The greater than operator checks whether the left value is greater than the right value.

It returns True if the condition is true; otherwise, it returns False.

Example - Greater Than

🐍Code Cell
1print(10 > 5)
Output
True

Example - Greater Than (False Case)

🐍Code Cell
1print(5 > 10)
Output
False

2. Greater Than or Equal To Operator (>=)

The >= operator returns True if the left value is greater than or equal to the right value.

Example - Greater Than or Equal To

🐍Code Cell
1print(10 >= 10)
2print(20 >= 10)
3print(5 >= 10)
Output
True
True
False

3. Less Than Operator (<)

The less than operator checks whether the left value is smaller than the right value.

Example - Less Than

🐍Code Cell
1print(5 < 10)
Output
True

Example - Less Than (False Case)

🐍Code Cell
1print(20 < 10)
Output
False

4. Less Than or Equal To Operator (<=)

The <= operator returns True if the left value is less than or equal to the right value.

Example - Less Than or Equal To

🐍Code Cell
1print(10 <= 10)
2print(5 <= 10)
3print(20 <= 10)
Output
True
True
False

5. Equal To Operator (==)

The equality operator checks whether both values are equal.

Example - Equal To

🐍Code Cell
1print(10 == 10)
2print(10 == 20)
Output
True
False

Example - Equal To with Strings

🐍Code Cell
1print("python" == "python")
2print("python" == "java")
Output
True
False

6. Not Equal To Operator (!=)

The not equal operator checks whether two values are different.

Example - Not Equal To

🐍Code Cell
1print(10 != 20)
2print(10 != 10)
Output
True
False

Relational Operators with Variables

🐍Code Cell
1a = 10
2b = 20
3 
4print(a > b)
5print(a < b)
6print(a == b)
7print(a != b)
Output
False
True
False
True

Relational Operators with Strings

Strings are compared character by character using their Unicode values.

Example - String Comparison

🐍Code Cell
1print("a" > "b")
2print("abc" < "xyz")
Output
False
True

Important Note

Uppercase and lowercase letters have different Unicode values.

Example - Uppercase and Lowercase

🐍Code Cell
1print("A" == "a")
2print("A" < "a")
Output
False
True

Chaining of Relational Operators

Python supports chaining of relational operators.

Example - Chaining

🐍Code Cell
1print(10 < 20 < 30)
Output
True

Explanation

Python internally evaluates the above expression as:

10 < 20 and 20 < 30
        

Example - Another Chaining Example

🐍Code Cell
1print(10 < 20 > 5)
Output
True

Invalid Comparison

Some comparisons are not supported.

Example - Invalid Comparison

🐍Code Cell
1print(10 > "python")
Output
TypeError

Why Does This Error Occur?

Python cannot directly compare different data types such as int and str.

Relational Operators Return bool Type

The result of every relational operation is a Boolean value.

Example - Boolean Result

🐍Code Cell
1result = 10 > 5
2 
3print(result)
4print(type(result))
Output
True

Using Relational Operators in if Statement

🐍Code Cell
1age = 18
2 
3if age >= 18:
4 print("Eligible to vote")
Output
Eligible to vote

Using Relational Operators in while Loop

🐍Code Cell
1i = 1
2 
3while i <= 5:
4 print(i)
5 i += 1
Output
1
2
3
4
5

Difference Between = and ==

Operator Meaning
= Assignment Operator
== Comparison Operator

Example - Assignment vs Comparison

🐍Code Cell
1a = 10
2 
3print(a == 10)
Output
True

Real World Usage

Relational operators are commonly used in:

  • Login validation
  • Decision making
  • Loops
  • Conditions
  • Searching and filtering
📝 Key Takeaways
  • Relational operators compare two values and always return True or False
  • The six relational operators are >, >=, <, <=, ==, and !=
  • Strings are compared character by character using Unicode values
  • Chained comparisons like 10 < 20 < 30 work in Python
  • Comparing incompatible types like int and str raises a TypeError
  • = is the assignment operator, while == is the comparison operator

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8