You can use the operator module and a dictionary:
Copyimport operator
ops = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.div
}
op_char = input('enter a operand')
op_func = ops[op_char]
result = op_func(a, b)
Answer from Matthew Flaschen on Stack OverflowW3Schools
w3schools.com › python › python_operators.asp
Python Operators
Python Examples Python Compiler ... Python Bootcamp Python Certificate Python Training ... Operators are used to perform operations on variables and values....
Top answer 1 of 6
96
You can use the operator module and a dictionary:
Copyimport operator
ops = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.div
}
op_char = input('enter a operand')
op_func = ops[op_char]
result = op_func(a, b)
2 of 6
11
The operator module http://docs.python.org/library/operator.html exposes functions corresponding to practically all Python operators. You can map operator symbols to those functions to retrieve the proper function, then assign it to your op variable and compute op(a, b).
What sorts of lesser known operators and statements are there?
I think the matrix multiplication operator @ is somewhat unknown, I rarely see it but think it's neat. EDIT: Link to the pep . More on reddit.com
Infix operators
1.1m members in the Python community. News about the programming language Python. If you have something to teach others post here. If you have … More on reddit.com
The strange operator precedence of the logical NOT
Depends on the syntax. A unary operator should have a higher precedence than binary operators. You wouldn't want !a && b mean !(a && b), that would be horrifying. For a not token, it depends on the rest of the language. If you have function application like in ML or haskell with spaces, then not should behave like a function. If not is a special case, then I'd stick to whatever Python and C++ do, to not waste any of the "weirdness budget" on such a triviality. But whatever you do, make it consistent with the rest of the language. If you have no precedences and it's always left-to-right, then stick with that. More on reddit.com
Overriding operators for a subclass
I get the feeling this is an XY problem, but as you stated it I would just add some qualifier to the sort. class Animal: tail_length = None def __lt__(self, other): return (self.sort_priority, self.tail_length) < (other.sort_priority, other.tail_length) class Dog(Animal): sort_priority = 2 def __init__(self, tail_length): super().__init__() self.tail_length = tail_length class Human(Animal): sort_priority = 1 # Human priority is less than Dog priority, so all humans are "less than" dogs. rover = Dog(5) pluto = Dog(10) david = Human() print(rover < pluto) # True print(david < pluto) # True More on reddit.com
Videos
Python Operators Explained in 25 Minutes: A Complete Guide
2 | Operators | Python for Complete Beginners - YouTube
Python Operators for Beginners | Python tutorial - YouTube
16:05
Python Operators and Expressions: Arithmetic and Comparison - YouTube
11:37
10 Crazy Python Operators That I Rarely Use - YouTube
05:04
Logical operators in Python are easy 🔣 - YouTube
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.3 documentation
For example, if A and C are true but B is false, A and B and C does not evaluate the expression C. When used as a general value and not as a Boolean, the return value of a short-circuit operator is the last evaluated argument. It is possible to assign the result of a comparison or other Boolean expression to a variable. For example, >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance' >>> non_null = string1 or string2 or string3 >>> non_null 'Trondheim' Note that in Python, unlike C, assignment inside expressions must be done explicitly with the walrus operator :=. This avoids a common class of problems encountered in C programs: typing = in an expression when == was intended.
GeeksforGeeks
geeksforgeeks.org › python › python-operators
Python Operators - GeeksforGeeks
In Python programming, Operators in general are used to perform operations on values and variables.
Published December 2, 2025
Python
wiki.python.org › moin › BitwiseOperators
BitwiseOperators - Python Wiki
All of these operators share something in common -- they are "bitwise" operators. That is, they operate on numbers (normally), but instead of treating that number as if it were a single value, they treat it as if it were a string of bits, written in two's complement binary.
Python documentation
docs.python.org › 3 › reference › index.html
The Python Language Reference — Python 3.14.3 documentation
For C or C++ programmers, two additional manuals exist: Extending and Embedding the Python Interpreter describes the high-level picture of how to write a Python extension module, and the Python/C API reference manual describes the interfaces available to C/C++ programmers in detail. 1. Introduction · 1.1. Alternate Implementations · 1.2. Notation · 2. Lexical analysis · 2.1. Line structure · 2.2. Other tokens · 2.3. Names (identifiers and keywords) 2.4. Literals · 2.5. String and Bytes literals · 2.6. Numeric literals · 2.7. Operators and delimiters ·
Python Reference
python-reference.readthedocs.io › en › latest › docs › operators
Operators — Python Reference (The Right Way) 0.1 documentation
Shifts the bits of the first operand right by the specified number of bits.
W3Schools
w3schools.com › python › python_operators_precedence.asp
Python Operator Precedence
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... Operator precedence describes the order in which operations are performed.
GeeksforGeeks
geeksforgeeks.org › python › python-logical-operators
Python Logical Operators - GeeksforGeeks
Python logical operators are used to combine or modify conditions and return a Boolean result (True or False).
Published 2 weeks ago
W3Schools
w3schools.com › python › python_operators_arithmetic.asp
Python Arithmetic Operators
Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match
Python documentation
docs.python.org › 3 › reference › datamodel.html
3. Data model — Python 3.14.3 documentation
All data in a Python program is represented by objects or by relations between objects. Even code is represented by objects. Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The is operator ...
Codecademy
codecademy.com › docs › python › operators
Python | Operators | Codecademy
July 22, 2025 - Operators in Python programming are special symbols or keywords that perform operations on variables and values. They are fundamental building blocks that allow developers to manipulate data, perform calculations, make comparisons, and control ...
Learn Python
learnpython.org › en › Basic_Operators
Basic Operators - Learn Python - Free Interactive Python Tutorial
Another operator available is the modulo (%) operator, which returns the integer remainder of the division. dividend % divisor = remainder. ... Using two multiplication symbols makes a power relationship. squared = 7 ** 2 cubed = 2 ** 3 print(squared) print(cubed) Python supports concatenating strings using the addition operator: