Level 1 Python: Scientific Calculator

As 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 we could use the Python math module to perform operations outside of the basic add, subtract, divide, and multiply. 

Let’s use the math module to create a scientific calculator in Python. We’ll be building off of the original calculator program, so if you have that, feel free to load it up. There are four steps to building a scientific calculator in Python, they are:

  • Creating Function Definitions
  • The Operation Map
  • Getting the Desired Operation from the User
  • Mapping and Performing Operations

Creating Function Definitions

The first thing we need to do is create the functions for each operation we want our calculator to have. Last time we created four functions: add, subtract, divide, and multiply. This time, we’ll add four more functions. 

We’ll add square, square root (sqrt), log, and exponentiate. The square, square root, and log functions each take one parameter. Meanwhile, the exponentiate function takes two parameters like the other four functions we originally created.

import math
# create function declarations
def add(a, b):
    return a + b
def subtract(a, b):
    return a - b
def divide(a, b):
    return a/b
def multiply(a, b):
    return a*b
def square(a):
    return a**2
def sqrt(a):
    return math.sqrt(a)
def log(a):
    return math.log(a)
def exponentiate(a, b):
    return a**b

The Operation Map

Next, we’ll create an operation map. This is simply a dictionary that maps a string onto a function. This is one of the nice things about Python, we can set the function as the value in a dictionary automatically. The only string we’ll change from the actual function name is sqrt to “square root”.

# create map
function_map = {
    "add": add,
    "subtract": subtract,
    "divide": divide,
    "multiply": multiply,
    "square": square,
    "square root": sqrt,
    "log": log,
    "exponentiate": exponentiate

Getting the Desired Operation from the User

Now we have all our functions written and the dictionary map that maps strings to functions. Next, we’ll write the code to ask the user for the input string. This string has to be one of the strings that is a key in the dictionary. We’ll give the user a list of possible operations, so they know which ones are available.

# ask user for desired operation
op = input("Which operation would you like to do? Add, subtract, divide, multiply, square, square root, log, or exponentiate? ")

Mapping and Performing Operations

At this point, everything is set up except for the actual execution of the operations. Unlike the four-function calculator, we have two kinds of operations, ones that take one argument and ones that take two. That’s why we asked for the operation first this time.

We’ll have to check if the operation is one of the three operations that only takes one input. If it does, then we’ll ask for one number. We’ll use the map to get the function and then pass the user input number to it and print our result.

If we are using one of the operations that require two parameters, we’ll ask the user for two numbers. Once we have the two numbers, we’ll call the function map to get the function and then pass the two input parameters to it. Then, we’ll print out the value returned from the function.

if op in ["square", "square root", "log"]:
    a = float(input("What number would you like to perform your operation on? "))
    x = function_map[op](a)
    a = float(input("What is the first number? "))
    b = float(input("What is the second number? "))
    x = function_map[op](a, b)

Summary of Creating a Scientific Calculator in Python

In this post we extended the basic, four-function calculator into a scientific calculator. We added four functions, two of which use the math library, and three of which take only one parameter. We also extended the function dictionary. Then we changed up our input pattern to get the function first before asking for the numbers because the functions don’t take the same number of parameters anymore.

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

Leave a Reply

%d bloggers like this: