calculator in python for beiners 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: python calculator
print('Calculator')
input_1 = input('First Number? ')
input_2 = input('Second Number? ')
try:
print(f'{input_1} + {input_2} is {float(input_1) + float(input_2)}')
print(f'{input_1} - {input_2} is {float(input_1) - float(input_2)}')
print(f'{input_1} X {input_2} is {float(input_1) * float(input_2)}')
print(f'{input_1} / {input_2} is {float(input_1) // float(input_2)}')
except Exception as e:
print(f'ERROR: {e}')