basic py calculator code example
Example 1: 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)
Example 2: python calculator
print("Choose operator (+,-,*,/):")
mode = input()
print("Choose first int:")
x = int(input())
print("Choose second int:")
y = int(input())
print("Your result:")
if mode == "+":
print(x+y)
elif mode == "-":
print(x-y)
elif mode == "*":
print(x*y)
elif mode == "/":
print(x/y)