Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 04

Relational Operators

Compare two values with >, <, ==, !=, >=, and <=. Every relational expression returns True or False — the building blocks for if and logical operators.

Beginner12 min

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.

10 greater than 3 evaluates to True

What you'll learn

  • The six relational operators
  • Why == is different from =
  • Every comparison returns True or False
  • 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.

python
print(10 > 3)
print(10 > 20)
text
True
False

Less than (<)

Returns True only when the left value is smaller.

python
print(10 < 20)
print(10 < 5)
text
True
False

Equal to (==)

== compares values, not variable names.

python
print(10 == 10)
print(10 == 20)
text
True
False

Not equal (!=)

Returns True when two values are different.

python
print(10 != 10)
print(10 != 20)
text
False
True

Greater than or equal (>=)

This includes equality.

python
print(10 >= 10)
print(10 >= 25)
text
True
False

Less than or equal (<=)

This also includes equality.

python
print(10 <= 10)
print(10 <= 5)
text
True
False

= vs ==

This is one of the most common beginner mistakes.

Operator Purpose
= Assign a value to a variable
== Compare two values

Assignment

python
a = 10

Comparison

python
print(a == 10)

Demo program

This prints a complete comparison table for two numbers.

python
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:

text
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 = False

Use 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:

python
10 >= 10
10 > 10

Why are the answers different? (>= allows equal; > does not.)

Exercise 2 — What does this return?

python
10 != 10

Explain in one sentence. (False — the values are equal.)

Exercise 3 — Compare your age with 18:

python
age = 21
print(age >= 18)

Quick knowledge check

  1. What type does a relational expression return?
    bool (True or False)

  2. Which operator checks equality?
    ==

  3. Is 10 <= 5 True?
    False

Summary

  • Python has six relational operators: >, <, ==, !=, >=, <=
  • Every comparison produces a boolean (True or False)
  • = 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…