Python Tutorial 0/52 lessons ~6 min read Lesson 8

    Operators

    Python has the usual arithmetic, comparison, logical, bitwise, and assignment operators — plus identity (is) and membership (in) operators that beginners often confuse with equa…

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Python has the usual arithmetic, comparison, logical, bitwise, and assignment operators — plus identity (is) and membership (in) operators that beginners often confuse with equality.

    Understanding the topic

    Core concepts to understand:

    • Arithmetic: + - * / // % **.
    • Comparison: == != < > <= >=.
    • Logical: and or not.
    • Identity: is, is not.
    • Membership: in, not in.

    Syntax reference

    Visual flow / code:

    python
    # Arithmetic
    print(7 / 2) # 3.5 (true division)
    print(7 // 2) # 3 (floor division)
    print(7 % 2) # 1 (modulo)
    print(2 ** 10) # 1024 (power)
    # Identity vs equality
    a = [1, 2]
    b = [1, 2]
    print(a == b) # True (same value)
    print(a is b) # False (different objects)
    # Membership
    print(2 in [1, 2, 3]) # True

    Execution workflow

    1Operators Workflow
    1 / 4

    Arithmetic

    + - * / // % **.

    Real-world use

    Using is to compare values instead of == is one of the most common beginner bugs — is compares identity, not equality.

    Best practices

    • Use == for value comparison.
    • Use is None (never == None).

    Common mistakes

    • is with strings/ints may work due to interning — don't rely on it.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestion<code>is</code> vs <code>==</code>?+

    Answer

    == compares values by calling __eq__, while is checks whether two names refer to the exact same object, which is identity comparison like comparing id() values. Two equal lists are == but not is. Never use is to compare numbers or strings, because small-object caching and interning make results implementation-dependent.

    Follow-up

    Why does 256 is 256 behave differently from 1000 is 1000 in some contexts?
    2BeginnerQuestionWhat does <code>//</code> do?+

    Answer

    // is floor division: it divides and rounds down toward negative infinity. For ints it returns an int, so 7 // 2 is 3 and -7 // 2 is -4, not -3. With floats it returns a float that is floored, such as 7.0 // 2 giving 3.0.

    Follow-up

    How is // related to the % operator?
    3IntermediateQuestionWhy <code>is None</code>?+

    Answer

    None is a singleton, so identity is the correct and cheapest check, and x is None cannot be fooled by a custom __eq__. A class could define __eq__ so that x == None returns True, and objects like NumPy arrays return arrays from ==. PEP 8 explicitly recommends is None and is not None.

    Summary

    In summary: == for values, is for identity. Always is None.

    Ready to mark this lesson complete?Track your journey across the entire course.