Hello Programmers, In this blog post, We will learn to code a simple calculator in Python. Let’s discuss them in detail:-
Code and output for calculator code in python is :-
# Python program for calculator
# Function to add two numbers
def add(num1, num2):
return num1 + num2
# Function to subtract two numbers
def subtract(num1, num2):
return num1 – num2
# Function to multiply two numbers
def multiply(num1, num2):
return num1 * num2
# Function to divide two numbers
def divide(num1, num2):
return num1 / num2
print(“Please select operation –\n“
“1. Add\n“
“2. Subtract\n“
“3. Multiply\n“
“4. Divide\n“)
select = int(input(“Select operations form 1, 2, 3, 4 :”))
num_1 = int(input(“Enter first number: “))
num_2 = int(input(“Enter second number: “))
if select == 1:
print(num_1, “+”, num_2, “=”,
add(num_1, num_2))
elif select == 2:
print(num_1, “-“, num_2, “=”,
subtract(num_1, num_2))
elif select == 3:
print(num_1, “*”, num_2, “=”,
multiply(num_1, num_2))
elif select == 4:
print(num_1, “/”, num_2, “=”,
divide(num_1, num_2))
else:
print(“Invalid input”)
Output :-
Please select operation –
1. Add
2. Subtract
3. Multiply
4. Divide
Select operations form 1, 2, 3, 4 :1
Enter first number: 23
Enter second number: 12
23 + 12 = 35
Please select operation –
1. Add
2. Subtract
3. Multiply
4. Divide
Select operations form 1, 2, 3, 4 :3
Enter first number: 21
Enter second number: 7
21 * 7 = 147
Hope this blog post was helpful to you and you have got the answer to your question. Thank you for visiting our blog. If you have any doubts about any coding questions then let us know in the comments section we will answer them as soon as possible. Till then check out our more blog posts.