Categories

## Super Simple Python: Simple Calculator

Super Simple Python is a series of Python projects you can do in under 15 minutes. In this episode, we’ll be covering how to build a simple calculator in under 30 lines of Python!

For a video version:

Unlike some of the Super Simple Python examples we’ve done, these don’t require any libraries!

## Defining the Calculator Functions

Since this is an episode of Super Simple Python, we’re going to be creating a Super Simple Calculator. This calculator will just perform addition, subtraction, multiplication, and division. Each of these functions will simply take two parameters, `a` and `b` and return the specified operation on `a` and `b`.

``````def add(a, b):
return a + b

def subtract(a, b):
return a - b

def multiply(a, b):
return a * b

def divide(a, b):
return a/b
``````

## Mapping the Calculator Functions

Once we’ve defined our functions, we’ll use a dictionary to map strings to the functions. One of the really nice things that’s unique to Python is that we can pass functions in as values in our dictionary. We’ll use an `ops` dictionary that contains our set of four operations mapped as a key-value pair of string to function.

Other than our dictionary of operations, we’ll also need a way to perform the specified operation on the two passed in values. Let’s create a function that takes three parameters: the first number, a string value corresponding to the operation, and the second number. This function will use the dictionary to map the string value to a function and then call that function on the numbers.

``````ops = {
"subtract": subtract,
"divide": divide,
"multiply": multiply
}

def perform(a, op, b):
function = ops[op]
return function(a, b)
``````

## Performing the Calculation

Everything is set up. Let’s do the calculation. We ask the user for three inputs, the numbers and the desired operation. Note that I wrapped the inputs of the numbers in an `int()` function, that is to turn these inputs from strings to integers. If you’d like to work with floating point numbers, you’ll have to use `float()` instead of `int()`. After we receive input from the user, we simply print out the returned value from the `perform` function we created earlier.

``````print("Input two numbers and an operator")
a = int(input("What is the first number? "))
op = input("Which operation would you like to perform?(add, subtract, multiply, or divide?) ")
b = int(input("What is the second number? "))
print(perform(a, op, b))
``````

An example printout should look like this:

What a coincidence:

{{#message}}{{{message}}}{{/message}}{{^message}}It appears your submission was successful. Even though the server responded OK, it is possible the submission was not processed. Please contact the developer of this form processor to improve this message. Learn More{{/message}}

Submitting…

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

One-Time
Monthly
Yearly

#### Make a yearly donation

Choose an amount

\$5.00
\$15.00
\$100.00
\$5.00
\$15.00
\$100.00
\$5.00
\$15.00
\$100.00

Or enter a custom amount

\$