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 Overflow
🌐
W3Schools
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....
Discussions

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
🌐 r/Python
31
28
November 28, 2020
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
🌐 r/Python
2
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
🌐 r/ProgrammingLanguages
30
39
June 12, 2024
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
🌐 r/learnpython
6
5
August 30, 2023
🌐
Python
docs.python.org › 3 › library › operator.html
operator — Standard operators as functions
Source code: Lib/operator.py The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python. For example, operator.add(x, y) is equivalent to the expres...
🌐
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.
Find elsewhere
🌐
Ksrdatavision
blog.ksrdatavision.com › home › operators in python
Master Python Operators: A Simple Step-by-Step Guide
November 22, 2025 - Learn Python Operators with this step-by-step guide. Understand arithmetic, logical, and comparison operators to boost your coding skills. Start now!
🌐
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 ·
🌐
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.
🌐
PYnative
pynative.com › home › python › python operators
Python Operators – PYnative
November 14, 2021 - Learn Python Arithmetic, Relational, Assignment, Logical, Membership, Identity, Bitwise operators with examples
🌐
NetworkLessons
networklessons.com › home › python › python operators
Python Operators
May 8, 2020 - Python operators perform a variety of operations including arithemtic, assignment, comparison, logical, identity, membership, and bitwise operators.
🌐
Tutorial Teacher
tutorialsteacher.com › python › python-operators
Python Operators: Arithmetic, Assignment, Comparison, Logical, Identity, Membership, Bitwise
Operators are special symbols that perform some operation on operands and returns the result. For example, 5 + 6 is an expression where + is an operator that performs arithmetic add operation on numeric left operand 5 and the right side operand ...
🌐
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 › tutorial › introduction.html
3. An Informal Introduction to Python — Python 3.14.3 documentation
The interpreter acts as a simple calculator: you can type an expression into it and it will write the value. Expression syntax is straightforward: the operators +, -, * and / can be used to perform arithmetic; parentheses (()) can be used for ...
🌐
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 ...
🌐
Plain English
python.plainenglish.io › mastering-python-operators-a-complete-and-comprehensive-guide-1d87fa888189
Mastering Python Operators: A Complete and Comprehensive Guide
March 6, 2024 - ... In programming, operators are symbols or keywords that perform specific operations on one or more operands (values or variables). Python provides a wide range of operators to manipulate data and control the flow of execution.
🌐
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: