Nearby lessons
31 of 159Python - Relational Operators
- 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:
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
Example - Greater Than (False Case)
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
3. Less Than Operator (<)
The less than operator checks whether the left value is smaller than the right value.
Example - Less Than
Example - Less Than (False Case)
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
5. Equal To Operator (==)
The equality operator checks whether both values are equal.
Example - Equal To
Example - Equal To with Strings
6. Not Equal To Operator (!=)
The not equal operator checks whether two values are different.
Example - Not Equal To
Relational Operators with Variables
Relational Operators with Strings
Strings are compared character by character using their Unicode values.
Example - String Comparison
Important Note
Uppercase and lowercase letters have different Unicode values.
Example - Uppercase and Lowercase
Chaining of Relational Operators
Python supports chaining of relational operators.
Example - Chaining
Explanation
Python internally evaluates the above expression as:
10 < 20 and 20 < 30
Example - Another Chaining Example
Invalid Comparison
Some comparisons are not supported.
Example - Invalid Comparison
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
Using Relational Operators in if Statement
Using Relational Operators in while Loop
Difference Between = and ==
| Operator | Meaning |
|---|---|
= |
Assignment Operator |
== |
Comparison Operator |
Example - Assignment vs Comparison
Real World Usage
Relational operators are commonly used in:
- Login validation
- Decision making
- Loops
- Conditions
- Searching and filtering
- 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