22.4 C
New York
Wednesday, April 24, 2024
HomeProgrammingPythonHow To Make A Simple Python 3 Calculator Using Functions

How To Make A Simple Python 3 Calculator Using Functions

Here you will learn how to make a simple Python calculator using functions. Perform division, multiplication, subtraction, and addition.

Here, you will learn how to make a simple Python 3 calculator. To understand this example, you should know basic Python programming concepts such as variables, operators, functions, and if-else statements.

Also ReadSending Emails Using Python With Image And PDF Attachments.

Steps To Make A Simple Calculator Using Python 3

Step 1: First, we will take two numbers input from the user using the input function.

Step 2: After that, we will create functions to perform arithmetic operations such as division, multiplication, subtraction, and addition. These functions will take two inputs (two numbers) and return the result of that operation.

To perform these operations, we will define four functions – multiplication(num1, num2), addition(num1, num2), subtraction(num1, num2), and divide(num1, num2).

Step 3: Next, we will provide the option to choose an operation (division, multiplication, subtraction, and addition). We can implement it by asking users to enter a digit to select the arithmetic operation they want to perform on the input numbers.

Users will see the following message on their console application – “Select operation 1-Division, 2-Multiplication, 3-Addition, 4-Subtraction.”

Step 4: Now, users can choose the operation they want to perform. Suppose the user selects the first operation; then they must enter 1, and then our program will display the operation result.

Python 3 code for a simple calculator

#Python Program to Make a Simple Calculator

def multiplication(num1, num2):
    return num1 * num2

def addition(num1, num2):
    return num1 + num2

def subtraction(num1, num2):
    return num1 - num2

def divide(num1, num2):
    return num1 / num2

value1 = int(input("Enter 1st number: "))
value2 = int(input("Enter 2nd number: "))

print("Select operation 1-Division, 2-Multiplication, 3-Addition, 4-Subtraction")
operation = int(input("Choose operation 1/2/3/4: "))

if operation == 1:
    print(value1, "/", value2, "=", divide(value1, value2))

elif operation == 2:
   print(value1, "*", value2, "=", multiplication(value1, value2))

elif operation == 3:
   print(value1, "+", value2, "=", addition(value1, value2))
elif operation == 4:
   print(value1, "-", value2, "=", subtraction(value1, value2))
   print("Enter correct operation")


Enter 1st Number: 2
Enter 2nd Number: 2
Select operation 1-Division, 2-Multiplication, 3-Addition, 4-Subtraction
Choose operation 1/2/3/4: 1
2 / 2 = 1.0

output python calculator

Try it yourself:

Summing Up

With this tutorial, you can create a basic Python 3 calculator. However, feel free to play with the code by adding more functionalities.

Stay tuned, and we will also share the tutorial to build a GUI calculator application using Python.

If you have any queries, please let me know in the comments section, and I will try my best to assist you.

Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x