Example 1: simple python calculator
#Store number variables for the two numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
#the sum of the two numbers variable
sum = float(num1) + float(num2)
sum2 = float(num1) - float(num2)
sum3 = float(num1) * float(num2)
sum4 = float(num1) / float(num2)
#what operator to use
choice = input('Enter an operator, + = addition, - = subtraction, * = multiplication and / = division: ')
#different sums based on the operators
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
#My Personal Python calculator
print("My Personal Python calculator")
#inputs
num1 = int(input('Enter the first number: '))
num2 = int(input('Enter the second number: '))
#opration req
opr = input('Enter the type of operation you want to perform between your chosen two numbers: ')
#calculation
if opr=='+':
ans = num1 + num2
# displaying the answer
print(f'Your final answer is {ans}')
elif opr == '- ':
ans = num1 - num2
# displaying the answer
print(f'Your final answer is {ans}')
elif opr=='*':
ans = num1 * num2;
# displaying the answer
print(f'Your final answer is {ans}')
elif opr=='/':
ans = num1 / num2
# displaying the answer
print(f'Your final answer is {ans}')
else:
print('Invalid Entry!!!')
Example 3: python calculator
print("Enter Your Choice 1(Add)/2(Sub)/3(Divide)/4(Multiply)")
num = int(input())
if num == 1:
print("Enter Number 1 : ")
add1 = int(input())
print("Enter Number 2 : ")
add2 = int(input())
sum = add1 + add2
print("The Sum Is ", sum)
elif num == 2:
print("Enter Number 1 : ")
sub1 = int(input())
print("Enter Number 2 : ")
sub2 = int(input())
difference = sub1 - sub2
print("The Difference Is ", difference)
elif num == 3:
print("Enter Number 1 : ")
div1 = float(input())
print("Enter Number 2 : ")
div2 = float(input())
division = div1 / div2
print("The Division Is ", division)
elif num == 4:
print("Enter Number 1 : ")
mul1 = int(input())
print("Enter Number 2 : ")
mul2 = int(input())
multiply = mul1 * mul2
print("The Difference Is ", multiply)
else:
print("enter a valid Number")
Example 4: how to make a calculator in python
# This will be one of the most advanced results you will find.
# We will be using classes and simple operators like +,-,*,/
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
# You can do this in terminal.
<C:/Users/username>python
>>> from main import Calculator
>>> result = Calculator.[addition|subtraction|multiplication|division](anyNumber, anyNumber)
>>> print(result)
Example 5: how to make a calculator in python
#You can change Text at Inputs
num1 = float(input("Enter Firt Number:"))
num2 = float(input("Enter Second Number"))
result= 0
op = str(input("Enter an op(operator)"))
#Gets Input Type--------
try:
#U can make this in while loop ;-)
if op=="+":
result = num1 + num2
else if op=="-":
result = num1 - num2
else if op=="*":
result = num1 * num2
else if op=="/":
result = num1 / num2
else:
print("Invalid Input")
print("The result is " + result)
except ZeroDivisionError:
print("U cant do that")
Example 6: how to make a calculator in python
def add(num1, num2):
return num1 + num2
def subtract(num1, num2):
return num1 - num2
def multiply(num1, num2):
return num1 * num2
def divide(num1, num2):
return num1 / num2