Learning navigation

Code with Animation

Operators & Identity

Master arithmetic, logical, comparison, and identity operators in Python, and learn the critical architectural difference between value equality (==) and object identity (is).

Python6 min readConcept 2 of 24

The Python Operator Landscape

Python provides a rich set of operators for performing mathematical calculations, logical deductions, bitwise manipulations, and object comparisons.

Arithmetic operators include standard addition (+), subtraction (-), multiplication (*), floating-point division (/), floor division (//), modulo remainder (%), and exponentiation (**).

Logical operators (and, or, not) operate on boolean expressions using short-circuit evaluation, returning the first operand that determines the final outcome.

Value Equality (==) vs Object Identity (is)

The single most important distinction in Python operations is between == and is.

The equality operator == checks value equivalence. It calls the object's __eq__() dunder method to verify whether two objects hold equivalent contents, regardless of where they live in memory.

The identity operator is checks memory address identity. It returns True if and only if both operands point to the exact same memory address (id(a) == id(b)).

Division Types and Pythonic Identity Checks

Python 3 distinguishes between / and //. Standard division 7 / 2 always returns a float (3.5), even if the division is exact (4 / 2 returns 2.0). Floor division 7 // 2 rounds down towards negative infinity, returning an integer (3).

Always use is when testing against singletons like None. Write if user is None: instead of if user == None:. Custom classes can override __eq__ to produce unexpected results with ==, but is can never be spoofed.

The Identity Comparator

Compare different pairs of values with both == and is. Watch the dual-sensor beam inspect content equality versus actual memory address identity.

# Values and identities:
x = 10
y = 10
# 1. == checks values:
x == y # True
# 2. is checks id():
x is y # True
# (shared address)
x0x7fa10010memory object xy0x7fa10010memory object y==x == y → True (Values Match)Compares values: 10 equals 10isx is y → True (Same Address)Same Object: CPython [-5, 256] cache reuse
★

Python Rule: Use == when comparing values (numbers, strings, lists). Reserve is for identity checks against singletons like None.

Integers between -5 and 256 are pre-allocated singletons in CPython's cache. x and y share the exact same address.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What is the key difference between `==` and `is` in Python?
  2. 2What is the output of `9 // 2` versus `9 / 2` in Python 3?
  3. 3Why does `a = [1, 2]; b = [1, 2]; a is b` evaluate to `False`?

Remember this

  • Use == to compare object values and content.
  • Use is strictly to compare identity (same memory address).
  • Always use 'is None' and 'is not None' for singleton checks.
  • Floor division // truncates toward negative infinity; / always returns a float.
  • Never rely on 'is' for integers or strings due to CPython caching behaviors.

Done with this concept?

Mark it complete to track your progress. No login needed.