python calculator code code example

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: 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 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 calculator using idle

def calculate():
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')

    number_1 = int(input('Please enter the first number: '))
    number_2 = int(input('Please enter the second number: '))

    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)

    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)

    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)

    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)

    else:
        print('You have not typed a valid operator, please run the program again.')

    # Add again() function to calculate() function
    again()

def again():
    calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')

    if calc_again.upper() == 'Y':
        calculate()
    elif calc_again.upper() == 'N':
        print('See you later.')
    else:
        again()

calculate()

Example 5: how to make a python calculator

#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 6: python bmi calculator code

print("Welcome to BMI calculator, by Abthahi Tazrian.")

# Offer Measurement Conversions

Offer_Conversions = input("Would you like to convert your imperial units to metric? (yes / no): ")

if Offer_Conversions == "yes":
    Imperial_Weight = input("What is your weight in pounds?: ")
    Imperial_Height = input("What is your height in feet (decimals)?: ")

    Imperial_Weight = float(Imperial_Weight)
    Imperial_Height = float(Imperial_Height)

    Metric_Converted_Weight = Imperial_Weight * 2.205
    Metric_Converted_Height = Imperial_Height * 30.48

    Metric_Converted_Weight = str(Metric_Converted_Weight)
    Metric_Converted_Height = str(Metric_Converted_Height)

    print("Your metric weight is " + Metric_Converted_Weight + " kg, please note this for the next stage.")
    print("Your metric height is " + Metric_Converted_Height + " cm, please note this for the next stage.")
    print("--")
elif Offer_Conversions == "no":
    print("--")

# Data Collection

Age = input("Put in your age: ")
Weight = input("Put in your weight in KG: ")
Height = input("Put in your height in CM: ")

# BMI Formula Calculation

Weight = float(Weight)
Height = float(Height)

Height_Squared = Height * Height
BMI_Formula_Assisted = Weight / Height_Squared
BMI_Formula_Completed = BMI_Formula_Assisted * 10000

# Health Chart Display

BMI_Formula_Completed = str(BMI_Formula_Completed)

print("You have a BMI score of " + BMI_Formula_Completed + ".")

BMI_Formula_Completed = float(BMI_Formula_Completed)

if BMI_Formula_Completed <= 18.5:
    print("You are underweight, consider gaining weight to bring your BMI to between 20 and 25.")
elif BMI_Formula_Completed <= 25:
    print("You are perfectly healthy, with a BMI within a healthy range of between 20 and 25.")
elif BMI_Formula_Completed <= 30:
    print("You are overweight, speak to a doctor about setting yourself a new target BMI between 20 and 25.")
elif BMI_Formula_Completed >= 30.1:
    print("You are obese, you need to lower your BMI to between 20 and 25 or else you may be at risks.")