Lesson 9: Comparison and logical operators
Comparison operators ask a question and answer True or False. Logical operators join those answers with and, or and not. One program shows all of them, including a chained comparison.
In short
- Comparison operators are ==, !=, >, <, >= and <=, and every one returns True or False.
- `and` needs both sides true, `or` needs one side true, `not` flips the answer.
- Python allows chained comparisons: `18 <= age < 60` means both tests at once.
- `=` assigns a value; `==` compares two values. Mixing them up is the most common beginner error.
6
comparison operators: ==, !=, >, <, >=, <=
Python docs
3
logical operators: and, or, not
Python docs
What this program shows
A comparison operator compares two values and gives one of two answers: True or False. That answer is a bool, the same type you met in lesson 1.
A logical operator joins two of those answers into one. The three are and, or and not.
The operators
| Operator | Read it as | Example | Answer |
|---|---|---|---|
== | is equal to | 5 == 5 | True |
!= | is not equal to | 5 != 5 | False |
> | is greater than | 7 > 3 | True |
< | is less than | 7 < 3 | False |
>= | is greater than or equal | 7 >= 7 | True |
<= | is less than or equal | 7 <= 6 | False |
The program
# compare.py — comparisons give True or False, logical operators join them
age = 20
marks = 72
passed = marks >= 40
print(age == 20) # True
print(age != 20) # False
print(marks > 90) # False
print(marks <= 72) # True
print(18 <= age < 60) # True: chained, age is in the range
print(passed) # True
print(age >= 18 and marks >= 40) # True: both sides true
print(age >= 18 or marks >= 90) # True: one side is enough
print(not passed) # False: not flips it
print(True and False) # False
print(True or False) # True
print(not True) # False
How it works, step by step
marks >= 40is a question. Python answers it and hands the answer topassed, sopassedholdsTrue.18 <= age < 60is two questions joined by Python for you: is age at least 18, and is age below 60? Both must be true.andgivesTrueonly when both sides are true. If either side is false, the answer isFalse.orgivesTruewhen at least one side is true. It is onlyFalsewhen both sides are false.nottakes one value and flips it:not TrueisFalse.- The last three lines show the same operators working on the words
TrueandFalsedirectly, because those are ordinary values.
What it prints
True False False True True True True True False False True False
Common mistakes
if marks = 40is an error. One equals sign assigns; two compare.5 == "5"isFalse. A number and text are never equal, even when they look the same.andis stricter thanor.True or False and FalseisTrue, becauseandruns first. Use brackets when mixing them.
Practice
Try these before moving on. Each one uses only what this lesson covered.
- Store your age and check whether you are between 18 and 60 with one chained comparison.
- Take marks in three subjects and print True if all three are at least 40.
- Print True if a number is either negative or greater than 100.
- Login check: a correct password is `"python123"`. Store what the user typed and print whether it matches.
Sources
Frequently asked questions
What is the difference between = and == in Python?
One equals sign assigns a value to a name, as in age = 20. Two equals signs compare two values and return True or False, as in age == 20. Using = inside a condition is a syntax error.
When should I use and instead of or?
Use and when every condition must hold — a student needs at least 40 in all subjects. Use or when one is enough — a number is negative or above 100. Reading the sentence out loud usually tells you which one you mean.
What does a chained comparison like 18 <= age < 60 do?
It checks both parts at once and is true only when age is at least 18 and below 60. It is the same as writing 18 <= age and age < 60, but shorter and easier to read.
Why does 5 == "5" return False?
Because an int and a str are different types. Python does not convert one to the other to make the comparison true. Convert first if that is what you meant: int("5") == 5.