code in python for calculator 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: basic calculator in python
print("My Personal Python calculator")
num1 = int(input('Enter the first number: '))
num2 = int(input('Enter the second number: '))
opr = input('Enter the type of operation you want to perform between your chosen two numbers: ')
if opr=='+':
ans = num1 + num2
print(f'Your final answer is {ans}')
elif opr == '- ':
ans = num1 - num2
print(f'Your final answer is {ans}')
elif opr=='*':
ans = num1 * num2;
print(f'Your final answer is {ans}')
elif opr=='/':
ans = num1 / num2
print(f'Your final answer is {ans}')
else:
print('Invalid Entry!!!')