Use a lookup table:
import operator
ops = { "+": operator.add, "-": operator.sub } # etc.
print(ops["+"](1,1)) # prints 2
Related Contents:
- Import a module from a relative path
- How do I change the figure size with subplots?
- Stripping everything but alphanumeric chars from a string in Python
- Loop backwards using indices
- Python Requests – No connection adapters
- How to get Linux console window width in Python
- How to uninstall a package installed with pip install –user
- How to plot in multiple subplots
- How to check a string for specific characters?
- How can I account for period (AM/PM) using strftime?
- Logging within pytest tests
- Python Dictionary to URL Parameters
- What is the difference between drawing plots using plot, axes or figure in matplotlib?
- Sorting dictionary keys in python [duplicate]
- pip installing in global site-packages instead of virtualenv
- Save list of DataFrames to multisheet Excel spreadsheet
- How can I make an EXE file from a Python program? [duplicate]
- How to create an SQL View with SQLAlchemy?
- gunicorn.errors.HaltServer: django
- Can’t set attributes on instance of “object” class
- str performance in python
- How to convert list of numpy arrays into single numpy array?
- How to read datetime back from sqlite as a datetime instead of string in Python?
- Importing installed package from script with the same name raises “AttributeError: module has no attribute” or an ImportError or NameError
- Using asyncio.Queue for producer-consumer flow
- Calculating the area under a curve given a set of coordinates, without knowing the function
- Renaming Column Names in Pandas Groupby function [duplicate]
- Skipping every other element after the first [duplicate]
- How can I efficiently cut out part of a video?
- Can I use Django F() objects with string concatenation?
- What is y axis in seaborn distplot?
- Search in lists of lists by given index
- What is the easiest way to install BLAS and LAPACK for scipy?
- Does python have a “use strict;” and “use warnings;” like in perl?
- Opencv polylines function in python throws exception
- How can I tell the Django ORM to reverse the order of query results?
- What do all the distributions available in scipy.stats look like?
- How can I set the x-axis as datetimes on a bokeh plot?
- Install mysql-python (Windows)
- Python base 36 encoding
- How do I get the weights of a layer in Keras?
- Remove whitespace in Python using string.whitespace
- Argparse optional boolean [duplicate]
- Best way to pop many elements from a Python dict
- how to catch A ‘UNIQUE constraint failed’ 404 in django
- Enable Python to Connect to MySQL via SSH Tunnelling
- Pandas: unique dataframe
- Building multi-regression model throws error: `Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).`
- Python: UserWarning: This pattern has match groups. To actually get the groups, use str.extract
- Unable to install psycopg2 (pip install psycopg2)