All 7 operator categories โ arithmetic, comparison, logical, bitwise, assignment, identity, and membership โ with every example run in the notebook.
Operators are used to perform operations on variables and values. Python has 7 categories of operators:
This note walks through each category with the exact examples run in the notebook.
/ always returns a float (true division); // returns floor division (integer part only).** is the exponentiation operator.and, or, not work on truthy/falsy values, not just True/False.&, |, ~, >>, <<) work at the binary bit level.++/-- โ always use += 1 / -= 1.is/is not โ identity (same object in memory); ==/!= โ value equality.in/not in โ membership check inside sequences.Setup: x = 5, y = 2
| Operator | Expression | Result | Meaning |
|---|---|---|---|
+ | print(x + y) | 7 | Addition |
- | print(x - y) | 3 | Subtraction |
* | print(x * y) | 10 | Multiplication |
/ | print(x / y) | 2.5 | True division (always returns float) |
% | print(x % y) | 1 | Modulus (remainder) |
** | print(x ** y) | 25 | Exponentiation (5ยฒ) |
// | print(x // 2) | 2 | Floor division (drops decimal part) |
x = 5
y = 2
print(x + y) # 7
print(x - y) # 3
print(x * y) # 10
print(x / y) # 2.5
print(x % y) # 1
print(x ** y) # 25
print(x // 2) # 2
๐ก Tip:/vs//โ/gives a precise float result,//gives only the whole number part (rounded down, not truncated โ matters for negative numbers).
Setup: x = 5, y = 2 (continued from above)
| Operator | Expression | Result | Meaning |
|---|---|---|---|
> | print(x > y) | True | Greater than |
< | print(x < y) | False | Less than |
>= | print(x >= y) | True | Greater than or equal to |
<= | print(x <= y) | False | Less than or equal to |
== | print(x == y) | False | Equal to (value comparison) |
!= | print(x != y) | True | Not equal to |
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False
print(x == y) # False
print(x != y) # True
โ ๏ธ Don't confuse==with=.=is assignment;==is comparison.
Setup: x = True, y = False
| Operator | Expression | Result | Meaning |
|---|---|---|---|
or | print(x or y) | True | True if at least one operand is True |
and | print(x and y) | False | True only if both operands are True |
not | print(not y) | True | Inverts the boolean value |
x = True
y = False
print(x or y) # True
print(x and y) # False
print(not y) # True
Important Points:
or โ short-circuits and returns True as soon as one operand is True.and โ short-circuits and returns False as soon as one operand is False.not โ simply flips True โ False.Setup: x = 2, y = 3 (binary: x = 010, y = 011)
| Operator | Expression | Result | Meaning |
|---|---|---|---|
& | print(x & y) | 2 | Bitwise AND |
| | print(x | y) | 3 | Bitwise OR |
>> | print(x >> 2) | 0 | Right shift by 2 bits |
<< | print(y << 3) | 24 | Left shift by 3 bits |
~ | print(~x) | -3 | Bitwise NOT (complement) |
x = 2 # binary: 010
y = 3 # binary: 011
print(x & y) # 2 โ 010 & 011 = 010
print(x | y) # 3 โ 010 | 011 = 011
print(x >> 2) # 0 โ shifts bits right, drops off the end
print(y << 3) # 24 โ 011 << 3 = 011000 (binary) = 24
print(~x) # -3 โ bitwise complement: ~x = -(x+1)
How each works (bit-level):
& (AND): Compares each bit position; result bit is 1 only if both bits are 1.
010
& 011
-----
010 → 2
| (OR): Result bit is 1 if either bit is 1.
010
| 011
-----
011 → 3
>> (Right shift): Shifts all bits to the right, dropping bits off the end (equivalent to floor-dividing by 2^n).<< (Left shift): Shifts all bits to the left, filling with zeros (equivalent to multiplying by 2^n).~ (NOT): Flips every bit; mathematically ~x = -(x + 1).๐ก Tip: Left shift bynโ multiply by2โฟ; right shift bynโ floor-divide by2โฟ.
a = 3
print(a)
# Output: 3
| Operator | Meaning | Example | Equivalent to |
|---|---|---|---|
+= | Add and assign | a += 3 | a = a + 3 |
-= | Subtract and assign | a -= 3 | a = a - 3 |
*= | Multiply and assign | a *= 3 | a = a * 3 |
&= | Bitwise AND and assign | a &= 3 | a = a & 3 |
a += 3 # same as: a = a + 3
print(a)
# Output: 6 (started from a = 3)
a -= 3
a *= 3
a &= 3
๐ก Tip: Compound assignment operators make code shorter and slightly more efficient than writing the full expression.
a++
++a
SyntaxError: invalid syntax
a++ or ++a (unlike C/C++/Java).++a doesn't error by itself in isolation (+ treated as unary plus applied twice), but a++ is invalid syntax.a += 1is, is not)Purpose: Check whether two variables point to the same object in memory โ not whether their values are equal.
| Expression | Meaning |
|---|---|
a is b | True if a and b reference the same object |
a is not b | True if a and b reference different objects |
a = 3
b = 3
print(a is b)
# Output: True
Why True? Python caches/interns small integers, so a and b may point to the same object in memory.
a = "Hello"
b = "Hello"
print(a is b)
# Output: True
Why True? Python interns short, simple string literals, so both variables reference the same string object.
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)
# Output: False
Why False? Lists are mutable โ even with identical contents, a and b are separate objects. Python never interns lists.
a = "Hello-world"
b = "Hello-world"
print(a is b)
# Output: False
print(a is not b)
# Output: True
Why False? Strings with certain characters (like -) are generally not interned by Python, even though the values are equal.
== to compare values, and is only when you need to compare object identity (e.g., x is None). String/int interning is a CPython implementation detail โ don't rely on it in real code.in, not in)Purpose: Check whether a value exists within a sequence (string, list, tuple, etc.)
| Expression | Meaning |
|---|---|
value in sequence | True if value exists in sequence |
value not in sequence | True if value does NOT exist in sequence |
x = "Delhi"
print("D" in x)
# Output: True
x = "Delhi"
print("D" not in x)
# Output: False
x = [1, 2, 3]
print(5 in x)
# Output: False
| Category | Operators |
|---|---|
| Arithmetic | +, -, *, /, %, **, // |
| Comparison | >, <, >=, <=, ==, != |
| Logical | and, or, not |
| Bitwise | &, |, ~, >>, << |
| Assignment | =, +=, -=, *=, &=, etc. |
| Identity | is, is not |
| Membership | in, not in |
/ โ float division; // โ floor division. Don't mix them up.and/or/not short-circuit โ evaluation stops as soon as the result is determined.+=, -=, *=, &= etc. are shorthand for reassignment โ there is no ++/-- in Python.is compares identity (memory address); == compares value.is can misleadingly return True.b = a).in/not in work on any iterable: strings, lists, tuples, sets, dicts (checks keys).+ - * / % ** // โ / gives float, // gives floor int.> < >= <= == != โ returns True/False.and (both true), or (at least one true), not (inverts).& AND, | OR, ~ NOT (-(x+1)), >> right shift (รท2โฟ), << left shift (ร2โฟ).+=, -=, *=, &= = shorthand; no ++/-- in Python โ use a += 1.is/is not): Same object in memory, not same value. Small ints/simple strings โ often interned (True); lists/complex strings โ usually False.in/not in): Checks if a value exists inside a string/list/etc.== for value comparison, is only for identity checks (like is None).