Coding/Learn/python/comparison operators
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnComparison Operators
13 / 32 0%
Lesson 13 of 32
6 mins read

Comparison Operators

PreviousNext Lesson
In Plain English

Comparison operators compare two values and produce a boolean answer: True or False. Whether you are checking if a password matches, verifying account balances, or testing game boundaries, comparison operators are your tools.

The Mental Model

"Imagine a bouncer at a club door checking IDs. If your age >= 21, the bouncer says True (you may enter). If your age < 21, the bouncer says False (denied)."

Deep Dive: How It Works

== (Equal to): Checks if two values are identical. Note: Use == (two equals) for comparisons, not = (one equal, which assigns variables).

!= (Not equal to): Returns True if values are different, False if they match.

> (Greater than) and < (Less than): Standard mathematical comparisons.

>= (Greater than or equal to) and <= (Less than or equal to).

Chained Comparisons: Python uniquely allows chained checks like 10 < x < 20, which is equivalent to 10 < x and x < 20.

Syntax Blueprint

a == b  # True if equal
a != b  # True if NOT equal
a > b   # Greater than
a <= b  # Less than or equal to

All comparison expressions evaluate directly to a boolean True or False.

PointCore Rules to Remember

Point star
== vs =: == asks "are these equal?". = means "store this value".
Point star
!= for Inequality: The exclamation mark ! represents NOT in programming.
Point star
Chained Checks: Python supports natural range checks like 0 <= score <= 100.

Common Beginner Traps & How to Fix Them

Mistake:Using single = in comparisons: if score = 100:

Why it happens: Single equal sign is assignment and causes a SyntaxError inside if conditions.

How to fix: Always use double equals == for comparisons.

Pro Tip: In Python, string comparisons are case-sensitive: "apple" == "Apple" is False. Use .lower() to compare strings case-insensitively.

Live Interactive Example

Hit Run Code to see it live
Comparison Operator Matrix
Python 3.12
1
2
3
4
5
6
7
8
9
10
Output Console
Click "Run Code" to view the rendered output.
How it works: Each comparison expression immediately resolves to a boolean truth value.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Test equality comparison

  • PointCheck if 25 * 4 is equal to 100 using == and print the result.
1
2
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates