Program to Make Simple Calculator in Python

Program to Make Simple Calculator in Python. This program contains arithmetic operators, print, input functions and if else statement. This is for beginners, class 11,12 and B Tech computer science students. Download source code.

Program to Make Simple Calculator in Python

Simple Calculator

Performs the different arithmetical operations, such as addition, subtraction, multiplication and division.

Approach:

  • We can choose the desired operation from the option of a, b, c, and d.
  • We can take two numbers, and if… elif… else, branching is used for executing the particular operation.
  • We will use add(), subtract(), multiply() and divide() function for evaluation the respective operation in the calculator.

Source code :

def add(P, Q):    
     
   return P + Q   
def subtract(P, Q):   
     
   return P - Q   
def multiply(P, Q):   
     
   return P * Q   
def divide(P, Q):   
       
   return P / Q    
    
print ("Please select the operation.")    
print ("a. Add")    
print ("b. Subtract")    
print ("c. Multiply")    
print ("d. Divide")    
    
choice = input("Please enter choice (a/ b/ c/ d): ")    
    
num_1 = int (input ("Please enter the first number: "))    
num_2 = int (input ("Please enter the second number: "))    
    
if choice == 'a':    
   print (num_1, " + ", num_2, " = ", add(num_1, num_2))    
    
elif choice == 'b':    
   print (num_1, " - ", num_2, " = ", subtract(num_1, num_2))    
    
elif choice == 'c':    
   print (num_1, " * ", num_2, " = ", multiply(num_1, num_2))    
elif choice == 'd':    
   print (num_1, " / ", num_2, " = ", divide(num_1, num_2))    
else:    
   print ("This is an invalid input")    

Download Source code : For Download Click on the below Button

Step wise Source code :

Step 1 : This function is used for adding two numbers   
def add(P, Q):
   return P + Q   
Step 2 : This function is used for subtracting two numbers   
def subtract(P, Q):      
   return P – Q   
Step 3 : This function is used for multiplying two numbers 
def multiply(P, Q):   
        return P * Q   
Step 4 :  This function is used for dividing two numbers    
def divide(P, Q):      
   return P / Q    
Step 5 : Now we will take inputs from the user    
print (“Please select the operation.”)    
print (“a. Add”)    
print (“b. Subtract”)    
print (“c. Multiply”)    
print (“d. Divide”)    
choice = input(“Please enter choice (a/ b/ c/ d): “)    
num_1 = int (input (“Please enter the first number: “))    
num_2 = int (input (“Please enter the second number: “))    
Step 6 : if and else statement
if choice == ‘a’:    
   print (num_1, ” + “, num_2, ” = “, add(num_1, num_2))    
elif choice == ‘b’:    
   print (num_1, ” – “, num_2, ” = “, subtract(num_1, num_2))    
elif choice == ‘c’:    
   print (num1, ” * “, num2, ” = “, multiply(num1, num2))    
elif choice == ‘d’:    
   print (num_1, ” / “, num_2, ” = “, divide(num_1, num_2))    
else:    
   print (“This is an invalid input”)    

Output :

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
Please select the operation.
a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d): c
Please enter the first number: 25
Please enter the second number: 4
25  *  4  =  100
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
Please select the operation.
a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d): d
Please enter the first number: 25
Please enter the second number: 4
25  /  4  =  6.25
>>> 

Output Explanation:

Step 1 :We can choose the desired operation from the option of a, b, c, and d.
Please select the operation. a. Add b. Subtract c. Multiply d. Divide Please enter choice (a/ b/ c/ d): c
Step 2 : First number is captured.
Please enter the first number: 25
Step 3 : Second number is captured.
Please enter the second number: 4
Step 4 : Multiplication of both number is done and printed on the screen.
25 * 4 = 100

Conclusion :

The program is running fine.

Thank you for reading and learning from the Program to Make Simple Calculator in Python post. Program to Make Simple Calculator in Python. This program contains arithmetic operators, print, input functions and if else statement. This is for beginners, class 11,12 and B Tech computer science students. For more Python programs click the link below –
Python Programs Archives – CS Study
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study

Leave a Comment

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