Python Program to Make a Simple Calculator | Calculator Program in Python

Posted in

Python Program to Make a Simple Calculator | Calculator Program in Python
vinaykhatri

Vinay Khatri
Last updated on March 28, 2024

    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 Calculator Program in Python

    • 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.

    Calculator Program in Python

    Python Code:

    def add(a, b):
        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 1 for Additon")
    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':
        print(num1,"+",num2,"=", add(num1,num2))
    
    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 1 for Addition
    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 1 for Addition
    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

    People are also reading:

    Leave a Comment on this Post

    0 Comments