Python operators are foundational symbols and keywords that perform operations on values, variables, and data structures. Python supports a rich set of operators categorized into Arithmetic, Assignment (including Augmented and Walrus :=), Comparison, Logical, Bitwise, Identity, and Membership operators.
Under the hood, Python operators invoke Special Dunder Methods (Magic Methods like __add__, __sub__, __eq__, __contains__) on object classes, allowing custom classes to overload operator behaviors. Furthermore, Python provides the standard library operator module (import operator) which exposes functional callables matching all syntax operators.
Arithmetic operators perform mathematical calculations across numeric types (int, float, complex).
| Operator | Name | Example | Behavior & Description |
|---|---|---|---|
+ |
Addition | a + b |
Calculates sum of two numbers. |
- |
Subtraction | a - b |
Calculates difference between two numbers. |
* |
Multiplication | a * b |
Calculates product of two numbers. |
/ |
Float Division | a / b |
Computes exact division, always returning a float. |
// |
Floor Division | a // b |
Divides and truncates fractional decimal remainder. |
% |
Modulus | a % b |
Returns the remainder of division. |
** |
Exponentiation | a ** b |
Raises a to the power of b. |
# Arithmetic Calculations Example
add_res = 10 + 3 # 13
div_res = 10 / 3 # 3.3333333333333335
floor_res = 10 // 3 # 3
mod_res = 10 % 3 # 1
pow_res = 10 ** 3 # 1000Assignment operators assign values to variables and perform in-place mutation operations.
| Operator | Expression | Equivalent Statement | Behavior |
|---|---|---|---|
= |
x = 5 |
x = 5 |
Direct variable assignment. |
+= |
x += 3 |
x = x + 3 |
In-place addition. |
-= |
x -= 2 |
x = x - 2 |
In-place subtraction. |
*= |
x *= 4 |
x = x * 4 |
In-place multiplication. |
/= |
x /= 2 |
x = x / 2 |
In-place float division. |
//= |
x //= 2 |
x = x // 2 |
In-place floor division. |
%= |
x %= 3 |
x = x % 3 |
In-place modulo. |
**= |
x **= 2 |
x = x ** 2 |
In-place exponentiation. |
:= |
(x := expr) |
Assignment Expression | Walrus Operator (Assigns inline within expressions). |
Comparison operators evaluate relational conditions between operands and return boolean values (True or False).
| Operator | Description | Example | Result (x=10, y=5) |
|---|---|---|---|
== |
Equal to | x == y |
False |
!= |
Not equal to | x != y |
True |
< |
Less than | x < y |
False |
> |
Greater than | x > y |
True |
<= |
Less than or equal to | x <= y |
False |
>= |
Greater than or equal to | x >= y |
True |
Logical operators evaluate boolean conditions using short-circuiting logic.
and: ReturnsTrueif BOTH conditions evaluate toTrue. Short-circuits if first condition isFalse.or: ReturnsTrueif EITHER condition evaluates toTrue. Short-circuits if first condition isTrue.not: Reverses the boolean truth value of an expression.
Bitwise operators perform binary bit-level operations on integer representations.
| Operator | Name | Expression (a=12 (1100), b=5 (0101)) |
Result |
|---|---|---|---|
& |
Bitwise AND | a & b |
4 (0100) |
| |
Bitwise OR | a | b |
13 (1101) |
^ |
Bitwise XOR | a ^ b |
9 (1001) |
~ |
Bitwise NOT | ~a |
-13 (-(a + 1)) |
<< |
Bitwise Left Shift | a << 2 |
48 (110000) |
>> |
Bitwise Right Shift | a >> 1 |
6 (0110) |
Introduced in Python 3.8 (PEP 572), the Walrus operator (:=) enables assignment expressions, allowing variables to be assigned within an expression context.
# With Walrus Operator (:=)
if (n := len(fetch_input())) > 10:
print(f"Data length: {n}")When Python evaluates an operator expression like a + b, CPython translates it into an object method call type(a).__add__(a, b). Custom classes can overload these dunder methods to define domain-specific operator behavior.
class CustomVector2D:
def __init__(self, x: float, y: float) -> None:
self.x = float(x)
self.y = float(y)
def __add__(self, other: "CustomVector2D") -> "CustomVector2D":
"""Overloads the addition operator (+)"""
return CustomVector2D(self.x + other.x, self.y + other.y)
def __mul__(self, scalar: float) -> "CustomVector2D":
"""Overloads the scalar multiplication operator (*)"""
return CustomVector2D(self.x * scalar, self.y * scalar)
def __eq__(self, other: object) -> bool:
"""Overloads equality comparison (==)"""
return isinstance(other, CustomVector2D) and self.x == other.x and self.y == other.y
v1 = CustomVector2D(3, 4)
v2 = CustomVector2D(1, 2)
print(v1 + v2) # CustomVector2D(x=4.0, y=6.0)
print(v1 * 3) # CustomVector2D(x=9.0, y=12.0)The standard library operator module provides efficient, C-implemented functional callables corresponding to Python syntax operators:
import operator
# Functional operator invocations
add_res = operator.add(10, 5) # Same as 10 + 5
sub_res = operator.sub(10, 5) # Same as 10 - 5
mul_res = operator.mul(10, 5) # Same as 10 * 5
eq_res = operator.eq(10, 5) # Same as 10 == 5
contains_res = operator.contains([1, 2, 3], 2) # Same as 2 in [1, 2, 3]
# High-Performance Item & Attribute Getters for Sorting
students = [
{"name": "Alice", "score": 92},
{"name": "Bob", "score": 85},
{"name": "Charlie", "score": 95},
]
# Fast sorting using operator.itemgetter
sorted_students = sorted(students, key=operator.itemgetter("score"), reverse=True)
print(sorted_students[0]["name"]) # CharlieChecking membership with x in range(start, stop, step) executes in
import sys
r = range(0, 10_000_000, 5)
print(sys.getsizeof(r)) # 48 bytes (Constant RAM)
print(5_000_000 in r) # True (Instant O(1) evaluation)Using dir(range) exposes all attributes and sequence methods available on range objects:
r = range(1, 10)
public_methods = [attr for attr in dir(r) if not attr.startswith("__")]
print(public_methods) # ['count', 'index', 'start', 'step', 'stop']| Version | Feature / Behavior | Technical Impact |
|---|---|---|
| Python 2.7 | Classic Integer Division (10 / 3 == 3) |
/ truncated fractional digits on integers. Required xrange() for lazy evaluation. |
| Python 3.0 | True Division (10 / 3 == 3.333...) |
Introduced // for floor division. Replaced xrange with immutable range object. |
| Python 3.8 | Walrus Operator (:=) |
Introduced assignment expressions (PEP 572). |
| Python 3.13 | Bytecode Specialization (BINARY_OP) |
CPython adaptive interpreter optimizes binary operators, achieving 10–15% speedups. |
- Use
operator.itemgetterandoperator.attrgetterfor Sorting: Consistently faster than equivalentlambdafunctions due to C-level execution. - Implement Dunder Methods in Custom Classes: Implement
__add__,__sub__,__eq__for intuitive class design. - Use
//explicitly for integer math: Prevents unnecessary float conversions. - Leverage Walrus
:=in Loops: Avoids redundant function or length calls in conditional expressions.