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

Loading editor…

Example - Greater Than (False Case)

Loading editor…

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

Loading editor…

3. Less Than Operator (<)

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

Example - Less Than

Loading editor…

Example - Less Than (False Case)

Loading editor…

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

Loading editor…

5. Equal To Operator (==)

The equality operator checks whether both values are equal.

Example - Equal To

Loading editor…

Example - Equal To with Strings

Loading editor…

6. Not Equal To Operator (!=)

The not equal operator checks whether two values are different.

Example - Not Equal To

Loading editor…

Relational Operators with Variables

Loading editor…

Relational Operators with Strings

Strings are compared character by character using their Unicode values.

Example - String Comparison

Loading editor…

Important Note

Uppercase and lowercase letters have different Unicode values.

Example - Uppercase and Lowercase

Loading editor…

Chaining of Relational Operators

Python supports chaining of relational operators.

Example - Chaining

Loading editor…

Explanation

Python internally evaluates the above expression as:

10 < 20 and 20 < 30
        

Example - Another Chaining Example

Loading editor…

Invalid Comparison

Some comparisons are not supported.

Example - Invalid Comparison

Loading editor…

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

Loading editor…

Using Relational Operators in if Statement

Loading editor…

Using Relational Operators in while Loop

Loading editor…

Difference Between = and ==

Operator Meaning
= Assignment Operator
== Comparison Operator

Example - Assignment vs Comparison

Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.