Creating an Advanced Calculator with Python.

Christopher Quiles
4 min readNov 2, 2020

--

This lab will show you how to create advanced calculators using Python.

This is my first Python project and have found this to be great place to start to grasp the fundamentals of using the Python’s programming language. This project will give us practice on getting input and numbers from users instead of strings.

There are many ways of creating a calculator in Python, and we’ll explore a few of them today. We’ll be using math operators, variables, conditional statements, functions, and handle user input to make our calculators.

Install Python:
It is recommended that you should have Python 3 installed. If you still need to install it, click on the bold link to follow the appropriate steps for to install python for your operating system.

Create a working directory:
After installing Python, create a working directory from your CLI so you can create your files to put this project in. You can name this folder whatever you feel comfortable with. For example I’ve named mine CODE.

Basic Calculator:

First off let’s create a file for the code. For example you can name this basic.py file name if you’d like as the example shows below.

Basically, what we are going to do here is create a simple calculator. We are going to get two numbers form the user and print those numbers to the screen. In this script we are asking to convert the string into numbers. A string in Python is a sequence of characters. This basic calculator will use (int) to convert what is in our parenthesis into integers which are also known as (whole numbers). However, it will not calculate decimals.

num1 = input("Enter a number: ")
num2 = input("Enter a number: ")
result = float(num1) + float(num2)

print(result)

Notice how we use float(s) instead of integers here. Float (floating point real values) − represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats let use calculate decimals, and of course whole numbers.

To run a python script enter ($ python xxxx.py). Then enter the numbers you want to add and subtract.

Here is out result before adding the floats.
Here is our result after adding the floats instead of integers.

Here is a link to the working code of our basic calculator.

Advanced Calculator:

Let’s build a more advanced calculator. Our last calculator was fine, except it could only do addition. The main thing we are going to be adding to this script is the operator, which will recognize addition, subtraction, multiplication, and division. Also, we will be adding if and elif statements to our script.

num1 = float(input("Enter first number: "))
op = (input("Enter operator: "))
num2 = float(input("Enter second number:" ))

if op == "+":
print(num1 + num2)
elif op == "_":
print(num1 - num2)
elif op == "/":
print(num1 / num2)
elif op == "*":
print(num1 * num2)
else:
print("Invalid operator")

“If statements are logical blocks used within programming. They’re conditional statements that tell a computer what to do with certain information.… So, if statements essentially mean: ‘If something is true, then do something, otherwise do something else.”

“In programming languages, an else statement is an alternative statement that is executed if the result of a previous test condition evaluates to false.”

To run a python script enter ($ python xxxx.py).

Here is our result from using multiplication with our advanced calculator.

We also used an “else” statement to report an invalid operator is something other than addition, subtraction, multiplication, or division is used.

Here is an example of when the user might receive Invalid Operator.

Here is a link to the working code of our Advanced Calculator.

Eval () Calculator:

Eval is an alternation calculator available and is much less code than our advanced calculator. “eval allows any string to be evaluated as a Python expression. It can accept arbitrary expressions (e.g. 1+2-3*4) not just binary operations. However, it should be used with caution in production environments because it can be used for malicious purposes.

calc = input("Type calculation:\n")

print("Answer: " + str(eval(calc)))

To run a python script enter ($ python xxxx.py).

It can accept arbitrary expressions (e.g. 1+2-3*4) not just binary operations.

Here is a link to the working code for the Eval Calculator.

Conclusion:

Like I mentioned in the beginning, there are many ways to create a calculator in Python these are just a few. Hopefully you found this project useful and gave you some beginner level experience with Python. I recommend saving these calculators for future projects and even improving them in the future. Thanks for the taking the time!

--

--

No responses yet