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 = {
"add": add,
"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:

## Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

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.

#### Make a one-time donation

#### Make a monthly donation

#### Make a yearly donation

Choose an amount

Or enter a custom amount

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly
## 4 replies on “Super Simple Python: Simple Calculator”

[…] conversions. We’re going to build a unit convertor in Python in much the same way we built the Super Simple Python Calculator. We’ll build a set of functions and then use a dictionary to map strings to those […]

[…] We don’t need to import any libraries for this function. For this episode we’re going to simply be building some functions out and using them like we did in the Unit Convertor, and the Calculator. […]

[…] subtraction, division, and multiplication. We actually went over all of these in our post on How to Build Your Own Simple Calculator. Here we’ll go over what the operators are. The operators are pretty much the same as you’d […]

[…] part of the Super Simple Python series, we made a basic, four-function calculator. Since then, we’ve also covered how to do math in Python. From the math tutorial, we learned how […]