Articles

# Calculator program in python using function

In this python programming tutorial we are going to learn:

• calculator program in python using function

### Calculator program in python using function

The python program to make a calculator is as follows:

``````# Owner : TutorialsInhand Author : Devjeet Roy

n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
return n1+n2

def subtract():
n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
return n1 - n2

def multiply():
n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
return n1 * n2

def divide():
n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
return n1 / n2

if __name__ == "__main__":
while True:
choice = input("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit\n")

if choice == '1':

elif choice == '2':
print("The difference is:",subtract())

elif choice == '3':
print("The product is:",multiply())

elif choice == '4':
print("The quotient is:",divide())

elif choice == '5':
break

else:
print("Enter correct choice!!")``````

The output of the python program to make a calculator is as follows:

``````PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
2. Subtract
3. Multiply
4. Divide
5. Exit
1
Enter first number: 5
Enter second number: 7
The sum is: 12.0
2. Subtract
3. Multiply
4. Divide
5. Exit
2
Enter first number: 12
Enter second number: 2
The difference is: 10.0
2. Subtract
3. Multiply
4. Divide
5. Exit
3
Enter first number: 2
Enter second number: 4
The product is: 8.0
2. Subtract
3. Multiply
4. Divide
5. Exit
5
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> ``````

### Few important tips about the program

1. We have implemented the concept of functions to design the simple calculator.

2. Function enables us to give a modular approach to our code.

python program to make a calculator:

You can use calculator program in python using function shown here to create your own calculator.

Python Numbers Program

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com