Python Simple Calculator Program

By | July 26, 2020

Python Simple Calculator Program demonstrates a four function basic calculator. This Python program provides the functionalities of:

  1.  Addition
  2.  Subtraction
  3.  Multiplication and
  4.  Division

of two numbers.

Python Simple Calculator Program

Code for Simple Calculator Program

# This is a simple Python program
# to design a simple 4 function
# calculator using if -elif- else

# define functions  
def add(x, y):  
   """This function performs addition of two numbers""" 
   return x + y 
def subtract(x, y): 
   """This function performs subtraction of two numbers""" 
   return x - y 
def multiply(x, y): 
   """ This function performs multiplicaton of two numbers""" 
   return x * y 
def divide(x, y): 
   """This function performs division of two numbers"""  
   return x / y  
# take input from the user  
print("*** Python Calculator Menu ***")
print("      1.Add")  
print("      2.Subtract")  
print("      3.Multiply")  
print("      4.Divide")  
  
choice = input("Enter choice(1/2/3/4):")  
  
num1 = int(input("Please Enter first number: "))  
num2 = int(input("Please Enter second number: "))  
  
if choice == '1':  
   print("Adding : ",num1,"+",num2,"=", add(num1,num2))  
  
elif choice == '2':  
   print("Subtracting: ",num1,"-",num2,"=", subtract(num1,num2))  
  
elif choice == '3':  
   print("Multiplying: ",num1,"*",num2,"=", multiply(num1,num2))  
elif choice == '4':  
   print("Dividing: ",num1,"/",num2,"=", divide(num1,num2))  
else:  
   print("Invalid input, Enter 1 to 4 only")  

Sample Run and Output of The Program

*** Python Calculator Menu ***
      1.Add
      2.Subtract
      3.Multiply
      4.Divide
Enter choice(1/2/3/4):1
Please Enter first number: 1200
Please Enter second number: 2000
Adding :  1200 + 2000 = 3200

How This Python Program Works

This program displays a menu as shown below:

*** Python Calculator Menu ***
      1.Add
      2.Subtract
      3.Multiply
      4.Divide
Enter choice(1/2/3/4):

The user will enter his / her choice from 1 to 4.
The program will use a user defined function to calculate and display the result of the selected operation. For example if the user selects 1 then addition of two numbers will take place. No doubt, the user will enter these numbers when the proram displays suitable messages to do this. This Python program actually uses user defined functions to perform the four reqired arithematic operation. Hence there are four user defind functions to add, subtract, multiply and divide the two numbers.

*** Python Calculator Menu ***
      1.Add
      2.Subtract
      3.Multiply
      4.Divide
Enter choice(1/2/3/4):3
Please Enter first number: 6
Please Enter second number: 5
Multiplying:  6 * 5 = 30

Related Python Programs

Python 3 Four Function Calculator Program tkinter GUI

Use Switch statement for simple calculator C Program

C Program Sum and Average Calculator

Java Program to Calculate Area of Circle

C Program To Calculate Circumference of Circle

Four Function Calculator Program in C programming

Marks Percentage Calculator C Program

Employee Salary with Bonus Calculation C++ Program

 

 

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *