script for calculator in python 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!!!')
Example 3: 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()