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!!!')
Example 3: python calculator
num_one = int(input("Enter 1st number: "))
op = input("Enter operator: ")
num_two = int(input("Enter 2nd number: "))
if op == "+":
print(num_one + num_two)
elif op == "-":
print(num_one - num_two)
elif op == "*" or op == "x":
print(num_one * num_two)
elif op == "/":
print(num_one / num_two)
Example 4: how to make a python calculator
import time
def add():
n1 = input('Enter a number: ')
n2 = input('Enter another number: ')
time.sleep(1)
print(int(n1)+int(n2))
def multiply():
n1 = input('Enter a number: ')
n2 = input('Enter another number: ')
time.sleep(1)
print(int(n1)*int(n2))
def divide():
n1 = input('Enter a number: ')
n2 = input('Enter another number: ')
time.sleep(1)
print(int(n1)int(n2))
def subtract():
n1 = input('Enter a number: ')
n2 = input('Enter another number: ')
time.sleep(1)
print(int(n1)-int(n2))
run = True
while run:
ans = input('Select an opreation: ')
if(ans=='Addition'):
add()
if(ans=='Divide'):
divide()
if(ans=='Multiply'):
multiply()
if(ans=='Subtract'):
subtract()
Example 5: how to create calculator in python
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
choice = input("Enter choice(1/2/3/4): ")
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
break
else:
print("Invalid Input")
Example 6: calculator code
x = input("first number")
y = input("second number")
z = input("do you want to multiply, minus, divide or add? (x,-,/,+)")
y = int(y)
x = int(x)
if z == "+":
print (x + y)
if z == "/":
print (x / y)
if z == "x":
print (x * y)
if z == "-":
print (x - y)
else:
print("use x,-,/ or + next time!")