Python Projects

Python Basic Calculator


Basic Calculator

This page teaches you how to create a basic calculator with Python.

Please note that the code does not provide a visual calculator (GUI), but instead some basic functionality.

The code is very basic and quite straightforward. If you are not familiar with Python coding, please check these tutorials first.

Let’s start.

We use Python version 3.10+ and PyCharm (IDE).

Plan

This basic calculator consist of 3 main parts:

  1. Welcome message
  2. User input
  3. Conditional statements

The basic calculator can calculate only 2 numbers and allows the following operations: addition (+), subtraction (-), multiplication (*), division (/).

Welcome message

The first part of the calculator is very easy.

We need to write a welcome message, which includes what the program is and any guidelines.

This can be simply achieved with a “print” statement. For better code efficiency, we use the multiline string method instead of writing each line with a new “print” statement.

print("""This is a basic calculator.

Guidelines:
1. You can calculate only 2 numbers.
2. The allowed operations are:
(+) addition
(-) subtraction
(*) multiplication
(/) division

Have fun!
""")

User input

The user input section is also fairly easy.

Since the calculator can operate with only 2 numbers, we need only 3 inputs: first number, operator, and a second number.

This part can be achieved with the “input” statement.

number1 = float(input("Please choose the first number: \n"))
operator = input("Please choose the operator: \n")
number2 = float(input("Please choose the second number: \n"))

We covert the numbers inputs to floats (decimal numbers, e.g. 2.16) through the use of casting. This allows the user to input also decimal numbers, and not just integers (whole numbers).

If we do not use casting to either integers or floats, the program will execute the input as strings, and consequently produce an error when trying to calculate.

Conditional statements

This section covers the calculator’s conditions. In other words, what to calculate and output as a consequence of the specific user input.

We can easily do this part with conditional statements (e.g. if/else).

result = []

if operator == "+":
    result = number1 + number2
elif operator == "-":
    result = number1 - number2
elif operator == "*":
    result = number1 * number2
elif operator == "/":
    result = number1 / number2

print("The result is:", result)

The “result = []” is used to store the result.

The “if/elif/else” statements are quite straightforward. If the user inputs the (+) operator, the program adds the two numbers and stores the result in the variable “result”. The rest follows the same logic.

Finally, we display the outcome through a “print” statement.

Example

The following is an example of the execution of the basic calculator.

example Python basic calculator

Full code of Basic Calculator

Below is the full Python code of a basic calculator.

print("""This is a basic calculator.

Guidelines:
1. You can calculate only 2 numbers.
2. The allowed operations are:
(+) addition
(-) subtraction
(*) multiplication
(/) division

Have fun!
""")

number1 = float(input("Please choose the first number: \n"))
operator = input("Please choose the operator: \n")
number2 = float(input("Please choose the second number: \n"))

result = []

if operator == "+":
    result = number1 + number2
elif operator == "-":
    result = number1 - number2
elif operator == "*":
    result = number1 * number2
elif operator == "/":
    result = number1 / number2

print("The result is:", result)

Next: Python Calculator

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.