Comparison Operators in Python
Comparison operators in Python (==, !=, <, >, <=, >=) compare two values and produce a boolean True or False, which you then combine with and, or, and not to build the conditions used in if statements.
Every program you've written so far does the exact same thing every single time you run it — same input, same output, no thinking, no choice involved. But a real app needs to look at something, decide something, and react differently depending on what it found. That decision-making starts with comparison operators in Python and the boolean values they produce.
This page covers the modulus operator, every comparison operator, and how to combine conditions with and, or, and not — the building blocks for if statements.
What you will learn
- What the modulus operator
%actually computes - Every comparison operator:
==,!=,>,<,>=,<= - The classic
=vs==bug that trips up every beginner - How to combine conditions with
and,or,not - Why Python treats non-zero numbers as truthy
The modulus operator: the remainder you forgot about
You already know how to divide numbers, but there are actually two results hiding inside every division problem. When you divide 7 by 3, the answer is 2 — but there is 1 left over that did not fit. That leftover piece is called the remainder. Python gives you the remainder using the % symbol, the modulus operator:
quotient = 7 / 3 # 2.3333... the result of dividing
remainder = 7 % 3 # 1 the leftover pieceThink of it like sharing sweets. If you have 7 sweets and 3 friends, each friend gets 2 sweets, and you have 1 sweet left over that does not divide evenly — that leftover 1 is exactly what 7 % 3 gives you.
The most common use of % is checking whether a number divides perfectly, with nothing left over. If x % y equals zero, then y divides evenly into x:
10 % 2 # 0 (10 divides evenly by 2, so it's even)
10 % 3 # 1 (10 does NOT divide evenly by 3)You can also use it to pull out specific digits from a number, since the last digit of any number is just whatever is left after dividing by 10:
x = 12345
x % 10 # 5 just the last digit
x % 100 # 45 the last two digitsYou will reach for % more often than you expect — especially when working with even/odd numbers, repeating patterns, or clocks.
Boolean expressions: questions with yes or no answers
Before a program can make a decision, it needs to be able to ask a question. In Python, every question has exactly two possible answers: True or False. An expression that gives you one of these two answers is called a boolean expression.
5 == 5 # True (yes, 5 is equal to 5)
5 == 6 # False (no, 5 is not equal to 6)True and False are not words or strings — they are a special type of value in Python called bool. There are only two of them in the whole language, and every decision your program makes comes down to one of them.
| Operator | Meaning | Example |
|---|---|---|
| == | equal to | x == y |
| != | not equal to | x != y |
| > | greater than | x > y |
| < | less than | x < y |
| >= | greater than or equal | x >= y |
| <= | less than or equal | x <= y |
Logical operators: combining conditions
Sometimes a single question is not enough — what if you need to check two things at once? Python gives you three tools for combining conditions, and they work almost exactly like the same words in English:
x > 0 and x < 10 # True only if BOTH are true (x is between 0 and 10)
n % 2 == 0 or n % 3 == 0 # True if AT LEAST ONE is true (n divides by 2 or by 3)
not (x > y) # True if the thing after not is FALSEandrequires both sides to be true. If either side is false, the whole thing is false.oronly needs one side to be true. It is only false when both sides are false.notsimply flips whatever comes after it — true becomes false, false becomes true.
For short-circuit evaluation, truth tables, and the difference between and/or versus the bitwise &/|, see the dedicated Logical Operators in Python page.
Truthiness: what counts as true or false beyond bool
Python is flexible about what counts as "true" or "false" outside of actual booleans. Any non-zero number is treated as true, and zero is treated as false:
17 and True # True (17 is non-zero, so it counts as true)
0 and True # False (0 is treated as false)This flexibility can be useful later, but for now — until you are comfortable — stick to writing out your conditions explicitly with ==, !=, >, and so on. It keeps things clear.
Common mistakes
- Writing
x = 5when you meant to compare withx == 5 - Writing
=<or=>instead of<=and>= - Forgetting that
andneeds both sides true, whileoronly needs one - Assuming
%gives you the division result instead of the remainder
Common questions
What is the difference between = and == in Python?
A single = assigns a value to a variable, like x = 5. A double == compares two values and returns True or False, like x == 5. Confusing the two is one of the most common beginner mistakes.
What does the % operator do in Python?
The % (modulus) operator returns the remainder of dividing the left number by the right number. For example, 7 % 3 equals 1. It's commonly used to check if a number divides evenly (x % y == 0).
What is the difference between and and or in Python?
and returns True only if both conditions are true. or returns True if at least one condition is true. not flips a boolean value from True to False or vice versa.
Why does Python treat 0 as False?
Python has a concept called truthiness: any non-zero number is treated as true in a boolean context, while zero is treated as false. This lets you write conditions like if count: instead of if count != 0:.
Is there a =< or => operator in Python?
No. Python only supports <= (less than or equal) and >= (greater than or equal), always with the comparison symbol first.
Explore
Related topics
Keep going — these sit next to this concept in a real learning path.
Conditionals in Python (if, elif, else)
if, elif, else, pass, and nested conditionals.
Logical Operators in Python
Deep dive into and, or, not, short-circuiting, and truth tables.
Operators and Operands in Python
The arithmetic operators these comparisons build on.
Python course hub
All free Python explainers and the path into Agentic practice.