How to store and use mathematical operators as python variable

python ^ operator
python eval
python pass operator as argument
python range
a == b python
operation python
python round
math in python

I am new to python and was trying to write something like this below (code A) so it does eactly like code B. I want to make use of user input of mathematical operators as do_what variable. How can we write this code (A) in python so it would work like code B?

code A

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
do_what = input("Enter a calculation symbol for calculation you want to perform: ")
result = float(num1) do_what float(num2)
print("result is: " + str(result))

code B

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
result = int(num1) + int(num2)
print("result is: " + str(result))

You can use the operator module for common operators and make a lookup dictionary to map symbols to functions. If you want operators not in that module, you can simply define custom functions and add look them up the same way:

import operator

operatorlookup = {
    '+': operator.add,
    '-': operator.sub,
    '*': operator.mul,
    '/': operator.truediv
}

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
do_what = input("Enter calculation symbols for calculation you want to perform: ")

op = operatorlookup.get(do_what)

if op is not None:
    result = op(float(num1), float(num2))
else:
    result = "Unknown operator"
print("result is: " + str(result))

Python Math Operations, 2 and 3 are operands (constants here, but they could be variables) a is the named memory location where the result of the addition operator is stored programming languages, you do not need to 'declare' a variable before you use it​. Let’s start out simple and add two numbers together, store the result in a variable and print out the result. a = 4b = 2c = a + bprint(c) Even though we used variables for the addition, we could just as well have entered numbers instead. As I am sure you can imagine, the above prints out the number 6. Division.


You might also be interested in knowing about the inbuilt function eval. This reduces the if and else loops to a single statement for this particular example of yours

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
do_what = input("Enter calculation symbols for calculation you want to perform: ")

result = eval(num1 + do_what + num2)
print("result is: %s" %result)

# Enter a number: 3
# Enter another number: 18
# Enter calculation symbols for calculation you want to perform: *
# result is: 54

EDIT

To make eval slightly safe in this particular case, you can perhaps use something like this

if do_what in ['+', '-', '*', '**', '/']:
    result = eval(num1 + do_what + num2)
    print("result is: %s" %result)
else:
    print ("Enter an arithmetic operator")

How To Do Math in Python 3 with Operators, Here is a quick reference table of math-related operators in Python. In fact, you can use the Python programming language as a calculator. into the print statement, we can initialize variables to stand for integer values: If you would like to keep reading about numbers in Python, you can continue onto  Python Arithmetic Operators Example - Assume variable a holds 10 and variable b holds 20, then −


num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
do_what = input("Enter calculation symbols for calculation you want to perform: ")
if do_what=='+':
    result = float(num1) + float(num2)
elif do_what=='-':
    result = float(num1) - float(num2)
elif do_what=='*':
    result = float(num1) * float(num2)
elif do_what=='/':
    result = float(num1) / float(num2)
print("result is: " + str(result))

4 Operators and functions with Python, Arithmetic operators should be familiar from everyday life, Python uses them in It is simply the convention to name a variable with strSomething if it is storing a  There is no analogue of int() here, since operators are keywords in the language and not values. Once you've compared that input string to your list of operands and determined the operator that it corresponds to, you can use the Python standard library's operator module to calculate the result of applying the operator to your two operands.


If security is not your concern you could use eval to achieve this

result = eval(str(num1) + do_what + str(num2))

The problem is that eval literally evaluates everything you write as python code, so don't do this if you mean for this to be used by others

2. Variables, expressions and statements, When you type a large integer, you might be tempted to use commas Python follows the same precedence rules for its mathematical operators that Store each word in a separate variable, then print out the sentence on one line using print. Introduction to Arithmetic Operators in Python. Similar to any other programming language, Arithmetic operators in python are nothing but the symbols/ elements used for representing a specific mathematical and logical operation that is to be performed on a value or a variable assigned with a value.


Python operators - working with operators in Python, in Python. We define various types of operators and create expressions using operators. The assignment operator = assigns a value to a variable. In mathematics The following is a table of arithmetic operators in Python programming language. Characters are special numbers stored in specific tables, like ASCII. Python Operators. Operators are used to perform operations on variables and values. Python divides the operators in the following groups: Arithmetic operators


Python Arithmetic Operators Example, Python Arithmetic Operators Example - Assume variable a holds 10 and variable b holds 20, then − Python Arithmetic Operators on Strings. In this Python Arithmetic operators example, We are using two variables a and b of string data type. Next, we are going to use these two variables to show the problems we generally face while performing arithmetic operations on String Data type.


Python - Basic Operators, Python Arithmetic Operators. Assume variable a holds 10 and variable b holds 20​, then −. [ Show Example ]. Operator, Description, Example. For example, in math the plus sign or + is the operator that indicates addition. In Python, we will see some familiar operators that are brought over from math, but other operators we will use are specific to computer programming. Here is a quick reference table of math-related operators in Python.