Python Calculator
Calculator
This tutorial teaches you how to write a calculator with Python.
Please be aware that the code does not provide a visual calculator (GUI), but instead some basic functionality.
The code is basic and easy to understand. 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 calculator contains 3 main sections:
- Welcome message
- Operation functions
- While loop with conditional statements
The calculator allows the calculation of only 2 numbers and it can perform the following operations: addition (+), subtraction (-), multiplication (*), division (/).
Welcome message
The first part of the calculator is straightforward.
We need to create a welcome message, which includes what the program is and any relevant information.
This is achieved with a “print” statement. For better code readability and efficiency, we use the multiline string method instead of writing each line with a new “print” statement.
print("""This is a calculator. Guidelines: 1. You can calculate only 2 numbers. 2. The allowed operations are: (+) addition (-) subtraction (*) multiplication (/) division Have fun! """)
Operation functions
The operation functions relates to the execution of the 4 operators.
Since the calculator can perform 4 operations, we need 4 functions, each defining the different operator.
This part can be achieved with the Python functions.
def add(n1, n2): return n1 + n2 def subtract(n1, n2): return n1 - n2 def multiply(n1, n2): return n1 * n2 def divide(n1, n2): return n1 / n2
The “return” statement is produces values and is commonly used with mathematical expressions. For more here.
While loop with conditional statements
This section covers the calculator’s conditions. In other words, what to calculate and output as a result of the specified user input.
We are including a “try/except” statement, in order to tackle number errors. In addition, we implement switches. The concept of a switch operates similarly to a room’s lights switch, representing an on/off functionality. True = on and False = off.
A switch plays a vital role in exiting a while loop (which runs forever without it).
This part can be implemented with a “while” loop and conditional statements (e.g. if/else).
For nested “while” loops here.
switch = True while switch: try: num1 = float(input("Please choose the first number: \n")) num2 = float(input("Please choose the second number: \n")) except ValueError: print("Please enter a number.") continue op_switch = True while op_switch: operator = input("Please choose the operator: \n") if operator == "+": print(f"\nThe result of {num1} {operator} {num2} equals {add(num1, num2)}") op_switch = False elif operator == "-": print(f"\nThe result of {num1} {operator} {num2} equals {subtract(num1, num2)}") op_switch = False elif operator == "*": print(f"\nThe result of {num1} {operator} {num2} equals {multiply(num1, num2)}") op_switch = False elif operator == "/": print(f"\nThe result of {num1} {operator} {num2} equals {divide(num1, num2)}") op_switch = False else: print("Only operators allowed!") break
The “switch” is used to change the condition of the while loop. In others, to stop the loop from iterating forever.
The “if/elif/else” statements are fairly easy to comprehend. If the user inputs the (+) operator, the program displays the two numbers and operator as well as executes the specific calculation function. The rest follows the same logic.
The “break” statement stops the main loop.
Example
Below is an example of the execution of the calculator.

Full code of Basic Calculator
Below is the full Python code of a basic calculator.
print("""This is a calculator. Guidelines: 1. You can calculate only 2 numbers. 2. The allowed operations are: (+) addition (-) subtraction (*) multiplication (/) division Have fun! """) def add(n1, n2): return n1 + n2 def subtract(n1, n2): return n1 - n2 def multiply(n1, n2): return n1 * n2 def divide(n1, n2): return n1 / n2 switch = True while switch: try: num1 = float(input("Please choose the first number: \n")) num2 = float(input("Please choose the second number: \n")) except ValueError: print("Please enter a number.") continue op_switch = True while op_switch: operator = input("Please choose the operator: \n") if operator == "+": print(f"\nThe result of {num1} {operator} {num2} equals {add(num1, num2)}") op_switch = False elif operator == "-": print(f"\nThe result of {num1} {operator} {num2} equals {subtract(num1, num2)}") op_switch = False elif operator == "*": print(f"\nThe result of {num1} {operator} {num2} equals {multiply(num1, num2)}") op_switch = False elif operator == "/": print(f"\nThe result of {num1} {operator} {num2} equals {divide(num1, num2)}") op_switch = False else: print("Only operators allowed!") break