Introduction
Python operators have official names defined in the language reference and the operator module. The * is the "multiplication operator" when used with numbers, the "repetition operator" with sequences, and the "unpacking operator" in function calls and assignments. The ** is the "exponentiation operator" for math and the "dictionary unpacking operator" in function calls. Knowing the proper names helps when reading documentation, searching for help, and discussing code.
Complete Operator Name Reference
Arithmetic Operators
1x + y # Addition operator
2x - y # Subtraction operator
3x * y # Multiplication operator
4x / y # Division operator (true division)
5x // y # Floor division operator (integer division)
6x % y # Modulo operator (remainder)
7x ** y # Exponentiation operator (power)
8-x # Unary negation operator
9+x # Unary plus operator
1print(7 + 3) # 10
2print(7 - 3) # 4
3print(7 * 3) # 21
4print(7 / 3) # 2.3333...
5print(7 // 3) # 2
6print(7 % 3) # 1
7print(2 ** 10) # 1024
Comparison Operators
1x == y # Equality operator
2x != y # Inequality operator (not-equal)
3x < y # Less-than operator
4x > y # Greater-than operator
5x <= y # Less-than-or-equal operator
6x >= y # Greater-than-or-equal operator
Logical Operators
x and y # Logical AND (short-circuit)
x or y # Logical OR (short-circuit)
not x # Logical NOT (boolean negation)
Bitwise Operators
1x & y # Bitwise AND operator
2x | y # Bitwise OR operator
3x ^ y # Bitwise XOR operator (exclusive or)
4~x # Bitwise NOT operator (complement)
5x << n # Left shift operator
6x >> n # Right shift operator
1print(0b1010 & 0b1100) # 0b1000 = 8 (AND)
2print(0b1010 | 0b1100) # 0b1110 = 14 (OR)
3print(0b1010 ^ 0b1100) # 0b0110 = 6 (XOR)
4print(1 << 3) # 8 (left shift)
5print(16 >> 2) # 4 (right shift)
Identity and Membership Operators
1x is y # Identity operator (same object in memory)
2x is not y # Negated identity operator
3x in y # Membership operator (containment test)
4x not in y # Negated membership operator
1a = [1, 2, 3]
2b = a
3c = [1, 2, 3]
4
5print(a is b) # True — same object
6print(a is c) # False — different objects, same content
7print(a == c) # True — equal content
8
9print(2 in a) # True
10print(5 not in a) # True
Assignment Operators
1x = y # Assignment operator
2x += y # Addition assignment (augmented assignment)
3x -= y # Subtraction assignment
4x *= y # Multiplication assignment
5x /= y # Division assignment
6x //= y # Floor division assignment
7x %= y # Modulo assignment
8x **= y # Exponentiation assignment
9x &= y # Bitwise AND assignment
10x |= y # Bitwise OR assignment
11x ^= y # Bitwise XOR assignment
12x <<= y # Left shift assignment
13x >>= y # Right shift assignment
14x := y # Walrus operator (assignment expression, Python 3.8+)
The * Operator (Multiple Meanings)
1# 1. Multiplication operator
2print(3 * 4) # 12
3
4# 2. Repetition operator (sequence repetition)
5print("ha" * 3) # "hahaha"
6print([0] * 5) # [0, 0, 0, 0, 0]
7
8# 3. Unpacking operator (iterable unpacking)
9first, *rest = [1, 2, 3, 4, 5]
10print(first) # 1
11print(rest) # [2, 3, 4, 5]
12
13# 4. Splat operator (argument unpacking in function calls)
14def add(a, b, c):
15 return a + b + c
16
17args = [1, 2, 3]
18print(add(*args)) # 6
19
20# 5. Variadic positional parameter (in function definitions)
21def func(*args):
22 print(args)
23
24func(1, 2, 3) # (1, 2, 3)
25
26# 6. Keyword-only argument separator
27def func(a, b, *, key): # key must be passed as keyword
28 pass
The ** Operator (Multiple Meanings)
1# 1. Exponentiation operator
2print(2 ** 10) # 1024
3
4# 2. Dictionary unpacking operator
5defaults = {"color": "blue", "size": 10}
6overrides = {"size": 20, "weight": 5}
7merged = {**defaults, **overrides}
8print(merged) # {'color': 'blue', 'size': 20, 'weight': 5}
9
10# 3. Keyword argument unpacking in function calls
11def greet(name, greeting):
12 print(f"{greeting}, {name}")
13
14kwargs = {"name": "Alice", "greeting": "Hello"}
15greet(**kwargs) # "Hello, Alice"
16
17# 4. Variadic keyword parameter (in function definitions)
18def func(**kwargs):
19 print(kwargs)
20
21func(a=1, b=2) # {'a': 1, 'b': 2}
The operator Module
Python's operator module provides function equivalents for all operators:
1import operator
2
3# Arithmetic
4operator.add(3, 4) # 7 — same as 3 + 4
5operator.mul(3, 4) # 12 — same as 3 * 4
6operator.pow(2, 10) # 1024 — same as 2 ** 10
7operator.floordiv(7, 3) # 2 — same as 7 // 3
8operator.mod(7, 3) # 1 — same as 7 % 3
9
10# Comparison
11operator.eq(3, 3) # True
12operator.lt(3, 5) # True
13
14# Useful with functional programming
15from functools import reduce
16numbers = [1, 2, 3, 4, 5]
17product = reduce(operator.mul, numbers) # 120
Dunder Methods Behind Operators
Every operator maps to a dunder (double underscore) method:
1class Vector:
2 def __init__(self, x, y):
3 self.x = x
4 self.y = y
5
6 def __add__(self, other): # + operator
7 return Vector(self.x + other.x, self.y + other.y)
8
9 def __mul__(self, scalar): # * operator
10 return Vector(self.x * scalar, self.y * scalar)
11
12 def __eq__(self, other): # == operator
13 return self.x == other.x and self.y == other.y
14
15 def __repr__(self):
16 return f"Vector({self.x}, {self.y})"
17
18v1 = Vector(1, 2)
19v2 = Vector(3, 4)
20print(v1 + v2) # Vector(4, 6) — calls __add__
21print(v1 * 3) # Vector(3, 6) — calls __mul__
Common Pitfalls
Confusing / and //: / is true division (returns float), // is floor division (returns int for int operands). In Python 2, / was integer division for ints, causing confusion when porting to Python 3.
Mutable default with * repetition: [[]] * 3 creates [[], [], []] where all three inner lists are the same object. Use [[] for _ in range(3)] to create independent lists.
Using is instead of == for value comparison: is checks object identity (same memory address), not value equality. a is b can return False even when a == b is True for large integers or dynamically-created strings.
Operator precedence surprises: 2 ** 3 ** 2 is 2 ** 9 = 512 (right-associative), not 8 ** 2 = 64. Use parentheses when mixing ** with other operators.
Walrus operator := scope: The walrus operator creates a variable in the enclosing scope, not just within the expression. [y := x for x in range(5)] leaves y = 4 accessible after the comprehension.
Summary
Every Python operator has an official name (multiplication, floor division, bitwise XOR, etc.)
* has 6 different meanings depending on context (multiplication, repetition, unpacking, splat, variadic, keyword separator)
** has 4 meanings (exponentiation, dict unpacking, kwarg unpacking, variadic keyword parameter)
The operator module provides function equivalents for all operators
Operators are implemented via dunder methods (__add__, __mul__, __eq__) which can be overridden in custom classes