simple calculator in python using functions code example
Example 1: 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!!!')
Example 2: how to make a calculator in python
class Calculator:
def addition(a,b):
return a + b
def subtraction(a,b):
if a<b:
return b - a
else:
return a - b
def multiplication(a,b):
return a * b
def division(a,b):
if a<b:
return b / a
else:
return a / b
<C:/Users/username>python
>>> from main import Calculator
>>> result = Calculator.[addition|subtraction|multiplication|division](anyNumber, anyNumber)
>>> print(result)