how to make a calculator using python 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: calculator in python

# Define variables for number inputs..
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

# Input for operators
op = input("Enter operator: ")

# Functions for operations


def add(num1, num2):
    print('Result: ', num1 + num2)


def subtract(num1, num2):
    print('Result: ', num1 - num2)


def multiply(num1, num2):
    print('Result: ', num1 * num2)


def division(num1, num2):
    print('Result: ', num1 / num2)


# Using the functions
if op == '+':
    add(num1=num1, num2=num2)
elif op == '-':
    subtract(num1=num1, num2=num2)
elif op == '*':
    multiply(num1=num1, num2=num2)
elif op == '/':
    division(num1=num1, num2=num2)
else:
    print('Invalid operator')

Example 3: 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