Nearby lessons

36 of 108

Python - Relational Operators

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Example - Greater Than (False Case)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Example - Less Than (False Case)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
True
False

5. Equal To Operator (==)

The equality operator checks whether both values are equal.

Example - Equal To

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Example - Equal To with Strings

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

6. Not Equal To Operator (!=)

The not equal operator checks whether two values are different.

Example - Not Equal To

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Relational Operators with Variables

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False
True
False
True

Relational Operators with Strings

Strings are compared character by character using their Unicode values.

Example - String Comparison

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False
True

Important Note

Uppercase and lowercase letters have different Unicode values.

Example - Uppercase and Lowercase

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False
True

Chaining of Relational Operators

Python supports chaining of relational operators.

Example - Chaining

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Explanation

Python internally evaluates the above expression as:

10 < 20 and 20 < 30
        

Example - Another Chaining Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Invalid Comparison

Some comparisons are not supported.

Example - Invalid Comparison

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Using Relational Operators in if Statement

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Eligible to vote

Using Relational Operators in while Loop

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1
2
3
4
5

Difference Between = and ==

Operator Meaning
= Assignment Operator
== Comparison Operator

Example - Assignment vs Comparison

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Real World Usage

Relational operators are commonly used in:

  • Login validation
  • Decision making
  • Loops
  • Conditions
  • Searching and filtering

Important Notes

  • Relational operators compare two values.
  • The result is always True or False.
  • Python supports chaining of relational operators.
  • Comparing incompatible data types causes a TypeError.

Quick Summary

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

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Logical Operators