PY-101 ยท Operators
Comparison and logical operators
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Comparison operators such as ==, !=, < and >= compare two values and give True or False. Logical operators combine those results: and needs both sides True, or needs at least one, and not flips the value. They are the building blocks of every if statement.
- Compare values with the six comparison operators
- Combine conditions with and, or, not
- Avoid the = vs == mistake
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
age = 20has_ticket = Trueprint(age >= 18)print(age == 21)print(age != 21)print(age >= 18 and has_ticket)print(age < 13 or age > 65)print(not has_ticket)print(10 < age < 30)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
True
False
True
True
False
False
True ๐ฏ A year is a leap year if it divides by 4, except years that divide by 100, unless they also divide by 400. Set is_leap for year = 2024 using % with and/or, then print it.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
- The problemA delivery app says: free delivery on orders over โน499, or for members. Someone has to turn that sentence into code the app can check on every order.
- The Python idea
a == b a != b a < b a > b a <= b a >= bComparison operators such as ==, !=, < and >= compare two values and give True or False. - In real lifeA delivery app shows "Free delivery" when cart_total >= 499 or is_member. That one line is a comparison joined with or.
Start with a question
A delivery app says: free delivery on orders over โน499, or for members. Someone has to turn that sentence into code the app can check on every order.
Every decision a program makes, such as "is the password right?" or "is the cart over the free-delivery limit?", is a comparison.
Explain it like I'm 10
Comparisons are yes/no questions: is my age 18 or more? The answer is always True or False. and means both must be yes; or means at least one must be yes.
The technical explanation
== checks equality. A single = assigns. Mixing them up is the most common beginner mistake.
and is True only if both sides are True. or is True if at least one side is True. not turns True into False and back.
Python lets you chain comparisons: 10 < age < 30 means 10 < age and age < 30.
Comparing text is case-sensitive: "Yes" == "yes" is False.
Syntax
a == b a != b a < b a > b a <= b a >= b
x and y x or y not x Real-world scenario
A delivery app shows "Free delivery" when cart_total >= 499 or is_member. That one line is a comparison joined with or.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Strings and f-strings โQuestions people ask
What is the difference between = and == in Python?
= assigns a value to a variable. == compares two values and returns True or False. if x = 5 is a syntax error; you mean if x == 5.
What is the difference between == and is in Python?
== checks whether two values are equal. is checks whether they are the very same object. Use is only for None (x is None) and use == for everything else.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What is the difference between = and == in Python?
= assigns a value to a variable. == compares two values and returns True or False. if x = 5 is a syntax error; you mean if x == 5.
beginner What is the difference between == and is in Python?
== checks whether two values are equal. is checks whether they are the very same object. Use is only for None (x is None) and use == for everything else.
intermediate What does 5 == 5.0 give?
True. An int and a float with the same value compare equal.
intermediate What does True and False give?
False. and needs both sides to be True.
Quick revision
Remember
- Compare values with the six comparison operators
- Combine conditions with and, or, not
- Avoid the = vs == mistake
=puts a value in a box;==asks whether two values are equal.if x = 5is a syntax error; you meanif x == 5.
Syntax
a == b a != b a < b a > b a <= b a >= b
x and y x or y not x Official documentation
- Comparisons (docs.python.org)
- Boolean operations (docs.python.org)