calculator in python code example

Example 1: calculator in python

# try to run this in repl.it to get the better experience

from replit import clear
# from art import logo

def add(n1, n2):
  return n1 + n2

def subtract(n1, n2):
  return n1 - n2

def multiply(n1, n2):
  return n1 * n2

def divide(n1, n2):
  return n1 / n2

operations = {
  "+": add,
  "-": subtract,
  "*": multiply,
  "/": divide
}

def calculator():
  print(logo)

  num1 = float(input("What's the first number?: "))
  for symbol in operations:
    print(symbol)
  should_continue = True
 
  while should_continue:
    operation_symbol = input("Pick an operation: ")
    num2 = float(input("What's the next number?: "))
    calculation_function = operations[operation_symbol]
    answer = calculation_function(num1, num2)
    print(f"{num1} {operation_symbol} {num2} = {answer}")

    if input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calculation: ") == 'y':
      num1 = answer
    else:
      should_continue = False
      clear()
      calculator()

calculator()

Example 2: calculator in python

def mutiply (x):
    return 5*x
o = mutiply(10)
print(o)

Example 3: Calculator in python

num1 = input("Enter a Number : ")
num2 = input("Enter a Number : ")
result = (num1 * num2)
print(result)
# And then print out the result

Example 4: calculator in python

#Calculator in python
#No modules required
qus = input('')
if(qus=='ADDITON'):
  no1 = int(input())
  no2 = int(input())
  print(no1+no2)
  
if(qus=='MULTIPLICATION'):
  no1 = int(input())
  no2 = int(input())
  print(no1*no2)

if(qus=='DIVISION'):
  no1 = int(input())
  no2 = int(input())
  print(no1/no2)
  
if(qus=='SUBTRACTION'):
  no1 = int(input())
  no2 = int(input())
  print(no1-no2)

Example 5: calculator in python

Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
Enter first number: 15
Enter second number: 14
15.0 * 14.0 = 210.0

Example 6: 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 7: calculator in python

def mutiply (x):
    return _*x
o = mutiply(_)
print(o)