Python Basics · Control Flow · Lesson 8 of 36
Comparison and Logic
Concept
Comparison and Logic
==, !=, <, and > compare values. and, or, and not combine conditions, spelled out instead of using symbols.
Chained comparisons like 0 < age < 18 also work in Python and read almost like plain English, unlike most other languages.
By the end of this lesson
- Explain the core idea behind Comparison and Logic.
- Predict output before running a change.
- Test one realistic and one unusual input.
- Use the result to make the next decision.
How to study this page
- 1. Read one concept.
- 2. Change the example.
- 3. Run, compare, and explain.
- 4. Complete the challenge below.
Syntax
a == b
a and b
a or b
not aExampleRunnable
age = 20
has_id = True
if age >= 18 and has_id:
print("Entry allowed")Try it Yourself »A chained comparison
age = 15
if 13 <= age < 20:
print("Teenager")Self-check before continuing
Without looking at the example, describe what changes when you modify one input in Comparison and Logic. Then reopen the editor and prove your explanation with a small test.
You are ready to continue when you can predict, test, and explain the result.
Your turn
Write a condition using or that checks if a day is Saturday or Sunday.
Loading editor…
Console