proper name for python operator?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
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
Comparison Operators
Logical Operators
Bitwise Operators
Identity and Membership Operators
Assignment Operators
The * Operator (Multiple Meanings)
The ** Operator (Multiple Meanings)
The operator Module
Python's operator module provides function equivalents for all operators:
Dunder Methods Behind Operators
Every operator maps to a dunder (double underscore) method:
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:[[]] * 3creates[[], [], []]where all three inner lists are the same object. Use[[] for _ in range(3)]to create independent lists. - Using
isinstead of==for value comparison:ischecks object identity (same memory address), not value equality.a is bcan return False even whena == bis True for large integers or dynamically-created strings. - Operator precedence surprises:
2 ** 3 ** 2is2 ** 9 = 512(right-associative), not8 ** 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)]leavesy = 4accessible 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
operatormodule provides function equivalents for all operators - Operators are implemented via dunder methods (
__add__,__mul__,__eq__) which can be overridden in custom classes
Related reading
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.