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 = {
    "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.

Yujian Tang
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

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

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

4 thoughts on “Super Simple Python: Simple Calculator

Leave a Reply

%d bloggers like this: