simple calculator using python code example
Example 1: simple python calculator
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
sum = float(num1) + float(num2)
sum2 = float(num1) - float(num2)
sum3 = float(num1) * float(num2)
sum4 = float(num1) / float(num2)
choice = input('Enter an operator, + = addition, - = subtraction, * = multiplication and / = division: ')
if choice == '+':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
if choice == '-':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum2))
if choice == '*':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum3))
if choice == '/':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum4))
Example 2: simple calculator in python
def sum1(first_input, second_input):
result1 = first_input + second_input
print("Your Answer is:-", result1)
return
def minus(first_input, second_input):
result2 = first_input - second_input
print("Your Answer is:-", result2)
return
def multi(first_input, second_input):
result3 = first_input * second_input
print("Your Answer is:-", result3)
return
def div(first_input, second_input):
result4 = first_input / second_input
result4 = float(round(result4))
print("Your Answer is:-", result4)
return
first_input = int(input("Enter your 1st Input:- "))
command = input("What you want to do (+, -, *, /):- ")
second_input = int(input("enter your 2nd Input:- "))
if command == "+":
sum1(first_input, second_input)
elif command == "-":
minus(first_input, second_input)
elif command == "*":
multi(first_input, second_input)
elif command == "/":
div(first_input, second_input)
else:
print("Command Error!")