Nearby lessons
36 of 108Python - Relational Operators
Introduction
Relational operators are used to compare two values.
The result of a relational operation is always a Boolean value:
TrueFalse
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
True
Example - Greater Than (False Case)
main.py
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
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
True
Example - Less Than (False Case)
main.py
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
True True False
5. Equal To Operator (==)
The equality operator checks whether both values are equal.
Example - Equal To
main.py
True False
Example - Equal To with Strings
main.py
True False
6. Not Equal To Operator (!=)
The not equal operator checks whether two values are different.
Example - Not Equal To
main.py
True False
Relational Operators with Variables
main.py
False True False True
Relational Operators with Strings
Strings are compared character by character using their Unicode values.
Example - String Comparison
main.py
False True
Important Note
Uppercase and lowercase letters have different Unicode values.
Example - Uppercase and Lowercase
main.py
False True
Chaining of Relational Operators
Python supports chaining of relational operators.
Example - Chaining
main.py
True
Explanation
Python internally evaluates the above expression as:
10 < 20 and 20 < 30
Example - Another Chaining Example
main.py
True
Invalid Comparison
Some comparisons are not supported.
Example - Invalid Comparison
main.py
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
True
Using Relational Operators in if Statement
main.py
Eligible to vote
Using Relational Operators in while Loop
main.py
1 2 3 4 5
Difference Between = and ==
| Operator | Meaning |
|---|---|
= |
Assignment Operator |
== |
Comparison Operator |
Example - Assignment vs Comparison
main.py
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
TrueorFalse. - 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 |