Use a lookup table:
import operator
ops = { "+": operator.add, "-": operator.sub } # etc.
print(ops["+"](1,1)) # prints 2
More Related Contents:
- Does Python’s `all` function use short circuit evaluation?
- Why does 1+++2 = 3?
- Math Expression Evaluation
- How to have one colorbar for all subplots
- Scope of python variable in for loop
- unknown error: session deleted because of page crash from unknown error: cannot determine loading status from tab crashed with ChromeDriver Selenium
- How to change values in a tuple?
- How to make a surface with a transparent background in pygame
- Python: load variables in a dict into namespace
- Python argparse – Add argument to multiple subparsers
- Longest common substring from more than two strings
- Why is using thread locals in Django bad?
- How to open multiple webpages in separate tabs within a browser using selenium-webdriver and python
- Count consecutive characters
- In Python, how do I iterate over a dictionary in sorted key order?
- How do I print functions as they are called?
- Counting python method calls within another method
- Difference between dict.clear() and assigning {} in Python
- python logging specific level only
- How can I provide a “callback” to an API?
- How to remove duplicate items from a list using list comprehension? [duplicate]
- How to sort multidimensional array by column?
- python encoding utf-8
- sum each value in a list of tuples
- Why is `with open()` better for opening files in Python?
- How to add all images from a folder to a button (in my frame) [closed]
- string encoding and decoding?
- How to count top 10 most common values in a dict in python
- Adding a matplotlib legend
- Psycopg2, Postgresql, Python: Fastest way to bulk-insert