Lesson 04
Relational Operators
Compare two values with >, <, ==, !=, >=, and <=. Every relational expression returns True or False — the building blocks for if and logical operators.
Relational operators compare two values. When two values are connected with a relational operator, the result is a relational expression (also called a test condition).
The result is always a boolean: True or False.
What you'll learn
- The six relational operators
- Why
==is different from= - Every comparison returns
TrueorFalse - How to compare two values in Python
The six relational operators
| Operator | Meaning | Example |
|---|---|---|
> |
Greater than | 10 > 3 |
< |
Less than | 10 < 20 |
== |
Equal to | 10 == 10 |
!= |
Not equal to | 10 != 20 |
>= |
Greater than or equal | 10 >= 10 |
<= |
Less than or equal | 10 <= 10 |
Interactive comparison playground
Interactive comparison playground
Choose two numbers and watch every relational operator update.
- 10 > 3 = TrueTrue
- 10 < 3 = FalseFalse
- 10 == 3 = FalseFalse
- 10 != 3 = TrueTrue
- 10 >= 3 = TrueTrue
- 10 <= 3 = FalseFalse
Greater than (>)
Returns True only when the left value is larger.
print(10 > 3)
print(10 > 20)True
FalseLess than (<)
Returns True only when the left value is smaller.
print(10 < 20)
print(10 < 5)True
FalseEqual to (==)
== compares values, not variable names.
print(10 == 10)
print(10 == 20)True
FalseNot equal (!=)
Returns True when two values are different.
print(10 != 10)
print(10 != 20)False
TrueGreater than or equal (>=)
This includes equality.
print(10 >= 10)
print(10 >= 25)True
FalseLess than or equal (<=)
This also includes equality.
print(10 <= 10)
print(10 <= 5)True
False= vs ==
This is one of the most common beginner mistakes.
| Operator | Purpose |
|---|---|
= |
Assign a value to a variable |
== |
Compare two values |
Assignment
a = 10Comparison
print(a == 10)Demo program
This prints a complete comparison table for two numbers.
a = int(input("Enter value of a: "))
b = int(input("Enter value of b: "))
print("{} > {} = {}".format(a, b, a > b))
print("{} < {} = {}".format(a, b, a < b))
print("{} == {} = {}".format(a, b, a == b))
print("{} != {} = {}".format(a, b, a != b))
print("{} >= {} = {}".format(a, b, a >= b))
print("{} <= {} = {}".format(a, b, a <= b))Sample output:
Enter value of a: 10
Enter value of b: 3
10 > 3 = True
10 < 3 = False
10 == 3 = False
10 != 3 = True
10 >= 3 = True
10 <= 3 = FalseUse the bottom playground with a, b = 10, 3 to run the same logic without input(), or add Program input fields when your lesson supports input().
Practice exercises
Exercise 1 — Predict before running:
10 >= 10
10 > 10Why are the answers different? (>= allows equal; > does not.)
Exercise 2 — What does this return?
10 != 10Explain in one sentence. (False — the values are equal.)
Exercise 3 — Compare your age with 18:
age = 21
print(age >= 18)Quick knowledge check
-
What type does a relational expression return?
bool(TrueorFalse) -
Which operator checks equality?
== -
Is
10 <= 5True?
False
Summary
- Python has six relational operators:
>,<,==,!=,>=,<= - Every comparison produces a boolean (
TrueorFalse) =assigns a value;==compares values- These expressions are the building blocks for logical operators (
and,or,not)
Next lesson: Logical Operators — combine multiple relational expressions into compound conditions.
Loading playground…