Python Code for Calculator

Python is a versatile programming language known for its simplicity. With the help of Python, we can perform various tasks. Here we are going to create a simple calculator in Python, This calculator can be used for performing basic arithmetic tasks like subtraction, multiplication, addition, and division.

To create a calculator in Python you can refer following Python code.

a = int(input("Enter the first number "))
b = int(input("Enter the second number "))
c = int (input("press 1 for addition \npress 2 for subtraction \npress 3 for multiplication \npress 4 for division "))

if c== 1: 
    print(f"result = {a+b}")

elif c== 2:
    print(f"result = {a-b}")

elif c==3:
    print(f"result = {a*b}")

elif c== 4:
    print(f"result = {a/b}")

else:
    print("Invalid input") 

Output –

Python Code for Calculator

The above code will perform the basic mathematics operation only but we can add more complex calculations here for example we can add a function to calculate the square root of a number etc.

See Also

Leave a Comment