Lesson 8 of 60
โฎPrev Nextโฏ

PY-101 ยท Operators

Comparison and logical operators

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

8of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Comparison and logical operators, running
    1. age = 20
    2. has_ticket = True
    3. print(age >= 18)
    4. print(age == 21)
    5. print(age != 21)
    6. print(age >= 18 and has_ticket)
    7. print(age < 13 or age > 65)
    8. print(not has_ticket)
    9. print(10 < age < 30)
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideaa == b a != b a < b a > b a <= b a >= bComparison operators such as ==, !=, < and >= compare two values and give True or False.
    3. 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

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    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.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What does 5 == 5.0 give?
    2. 2. What does True and False give?
    3. 3. What does False or True give?
    4. 4. Which line checks whether x equals 10?
    5. 5. What does "Apple" == "apple" give?

    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 = 5 is a syntax error; you mean if 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