# Operators & Identity

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

Subject: Python | Lesson 2 of 24 | 6 min read
Interactive lesson: https://codewithanimation.in/python/operators
Previous: [Variables & Dynamic Typing](https://codewithanimation.in/python/variables-and-types.md) | Next: [Conditionals & Truthy Logic](https://codewithanimation.in/python/if-else.md)

## 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.

## Common mistake: The Small Integer Caching Trap

In CPython, small integers between -5 and 256 are pre-allocated at startup as global singletons. If you write `x = 100` and `y = 100`, `x is y` returns `True` because both point to the cached singleton.

However, if you write `x = 1000` and `y = 1000`, CPython allocates two distinct integer objects in memory. Now `x == y` is `True`, but `x is y` is `False`! Never use `is` to compare numbers or user strings; always use `==` for values.

## Tip: Short-Circuit Return Values

In Python, `and` and `or` do not just return `True` or `False`. They return the actual value of the operand that decided the outcome: `None or 'default'` evaluates directly to `'default'`.

## Try it: 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.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/python/operators

## Check yourself

### Question 1

What is the key difference between `==` and `is` in Python?

- A. == compares value equality (contents), while is compares memory address identity (id).
- B. == compares memory addresses, while is compares string representations.
- C. They are completely identical and can be used interchangeably.

Correct answer: A

Equality `==` evaluates whether two objects contain equivalent values via `__eq__`. Identity `is` evaluates whether two references point to the exact same object in memory.

- Why not B: Incorrect. The equality operator == compares contents, while is compares memory addresses.
- Why not C: Incorrect. They perform fundamentally different checks.

### Question 2

What is the output of `9 // 2` versus `9 / 2` in Python 3?

- A. 4 and 4.5
- B. 4.5 and 4
- C. 4 and 4

Correct answer: A

The floor division operator `//` rounds down to the nearest whole integer (4), while `/` always computes true float division (4.5).

- Why not B: Incorrect. // is floor division and / is float division.
- Why not C: Incorrect. In Python 3, the single slash / always produces a float.

### Question 3

Why does `a = [1, 2]; b = [1, 2]; a is b` evaluate to `False`?

- A. Each bracket literal [1, 2] allocates a separate, distinct list object in heap memory.
- B. Lists cannot be compared using identity operators.
- C. Because the numbers inside the list have different memory addresses.

Correct answer: A

Each time a list literal `[...]` is evaluated, Python creates a new list instance in memory. Even though their contents match (`a == b` is True), their memory addresses differ (`a is b` is False).

- Why not B: Incorrect. Any two objects can be compared with is.
- Why not C: Incorrect. The check fails because the list containers themselves have different memory addresses.

## 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.
