HuntExams Academy logo
Python ยท Chapter 11 of 45

Python Operators

Operators are special symbols that perform operations. Python has arithmetic, comparison, logical, assignment, membership and identity operators.

Operator precedence follows standard math โ€” use parentheses when unsure.

Categories

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

Assignment shortcuts

`x += 1` is the same as `x = x + 1`. Works with -=, *=, /=, //=, %=, **=.

Example 1 (python)
x = 10
x += 5
x *= 2
print(x)
Output
30

10 + 5 = 15, then 15 * 2 = 30.

Example 2 (python)
fruits = ["apple", "banana"]
print("apple" in fruits)
print("grape" not in fruits)
Output
True
True

`in` tests membership in any sequence.

Key points

  • Arithmetic, comparison, logical, assignment, membership, identity.
  • `==` compares values; `is` compares identity (same object in memory).
  • `in` tests membership.
  • Use parentheses to make precedence explicit.
๐Ÿ’ก Note: Only use `is` for singletons like `None` โ€” `if x is None:`. For everything else use `==`.

๐Ÿ“ Quick Quiz

1. Which operator tests membership?

2. `5 ** 2` equals:

3. The correct way to check for None is: