How to Create a Calculator using python in 2022?

In this article, you will learn How to Create a Calculator using python under the console using the Python function.

In the image below, you can see all the codes used.

Create a Calculator using python
Codes to Calculator in Python

Explainations and Steps to Create a Calculator using python

As you can see, in the first step we create four functions. The first function, which is the addition function, takes the two parameters X and Y and adds them together. The subtract function for subtraction, the multiply function for multiplication, and the divide function for division.

def addition(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x / y

We then tell the user to select one of the following options.

In the next step, we create a function called cal and put our commands in it.

In the next step, we create a variable called the choice to get input from the user, to find out which math operation the user wants to perform.

print("Select operation.")


def cal():
    choice = input("Enter choice(1/2/3/4): ")

In the next step, with the conditional, if statement, we check whether the input we got from the user is in Tapel (referring to the mathematical operations that we defined) or if the command is executed correctly. In the next step, it receives two numbers from the user by typing float.

tuple_num = ('1', '2', '3', '4')

    if choice in tuple_num:
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))

In the next step, we use the conditional if statement again and say that if the user input is equal to 1, it will print the first number plus the second number and equal to the expression Addition (num1, num2).

Here we call the addition function and pass the received numbers to it to perform the addition operation on them. As you can see, with the elif command, we check the other numbers and perform the mathematical operations. And at the end, with the else command, we say that if the number entered in the tuple does not exist, it will display the Invalid Input.

if choice == '1':
            print(num1, "+", num2, "=", addition(num1, num2))

        elif choice == '2':
            print(num1, "-", num2, "=", subtract(num1, num2))

        elif choice == '3':
            print(num1, "*", num2, "=", multiply(num1, num2))

        elif choice == '4':
            print(num1, "/", num2, "=", divide(num1, num2))

        print("Invalid Input")

As you can see, in each line, by calling a function and passing a number to it, mathematical operations can be performed.

Finally, we call the main function, which is cal (), to run the program.


Show More

Rahila Munji

Rahila Munji is the Co-founder of She is a student in the IT department at the University of Mumbai. She is doing Blogging for over 5 years and worked with several websites such as WikiGain, Technig, and IntoGuide. I hope her expertise will be helpful to you.

Related Articles

Back to top button

AdBlock Detected

Please Turn off Your Ad Block.