Python Program to Make a Simple Calculator

By | October 3, 2021 Here in this article, we have provided a python source code for a simple calculator that can perform basic arithmetic operations such as Addition, Multiplication, Division, subtraction, and modules.

Prerequisite topics to create this program.

• Python Input, Output
• Python Data types
• Python User-defined Functions
• Python Arithmetic Operators
• Python If…..elif statements.

Steps

• Create 4 different functions add(), sub(), mul(), and div(), for four different operaitons.
• First, ask the user to select the operation.
• Ask the user to enter two values for the operations.
• according to the user operator selection call the corresponding function.

Python Program to Make a Simple Calculator

Code:

return a + b

def sub(a, b):
return a - b

def mul(a, b):
return a * b

def div(a, b):
return a / b

print("Select the Operation.")
print("Enter 2 for Subtraction")
print("Enter 3 for Multiplication")
print("Enter 4 for Division")

operation = input()

num1 = float(input("Enter the First number: "))
num2 = float(input("Enter the Second number: "))

if operation == '1':

elif operation == '2':
print(num1,"-",num2,"=", sub(num1,num2))

elif operation == '3':
print(num1,"*",num2,"=", mul(num1,num2))

elif operation == '4':
print(num1,"/",num2,"=", div(num1,num2))

else:
print("Please Enter 1 or 2 or 3 or 4")

Output 1:

Select the Operation.
Enter 2 for Subtraction
Enter 3 for Multiplication
Enter 4 for Division
4
Enter the First number: 12
Enter the Second number: 4
12.0 / 4.0 = 3.0

Output 2:

Select the Operation.
Enter 2 for Subtraction
Enter 3 for Multiplication
Enter 4 for Division
2
Enter the First number: 23
Enter the Second number: 42
23.0 - 42.0 = -19.0 