Simple Calculator in Python

Simple calculator in Python | In this post, We will create a calculator, It will be a basic calculator in python with CUI (character user interface) which can perform the different arithmetical operations, such as add(+), subtract(-), multiply(*), and divide(/) of two numbers.

Example:-
Input: x=4 and y=2

Output:-
Add(+) = x+y = 4+2 = 6
Subtract(-) = x-y = 4-2 = 2
Multiply(x) = x*y = 4*2 = 8
Divide(/) = x/y = 4/2 = 2

Python Program to Make a Simple Calculator

This is the simplest and easiest way to make a simple calculator in python. We will take two numbers while declaring the variables and select operation (+, -, *, /). Then, find operations and results will be displayed on the screen using the if-else statement.

# Python program to make a simple calculator

# take inputs
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# choise operation
print("Operation: +, -, *, /")
select = input("Select operations: ")

# check operations and display result
# add(+) two numbers
if select == "+":
    print(num1, "+", num2, "=", num1+num2)

# subtract(-) two numbers
elif select == "-":
    print(num1, "-", num2, "=", num1-num2)

# multiplies(*) two numbers
elif select == "*":
    print(num1, "*", num2, "=", num1*num2)

# divides(/) two numbers
elif select == "/":
    print(num1, "/", num2, "=", num1/num2)

else:
    print("Invalid input")

Output for the different input values:-

Enter first number: 2
Enter second number: 3
Operation: +, -, *, /
Select operations: +
2.0 + 3.0 = 5.0

Enter first number: 5
Enter second number: 2.3
Operation: +, -, *, /
Select operations: –
5.0 – 2.3 = 2.7

Enter first number: 2.1
Enter second number: 3.5
Operation: +, -, *, /
Select operations: *
2.1 * 3.5 = 7.3500000000000005

Enter first number: 9
Enter second number: 2
Operation: +, -, *, /
Select operations: /
9.0 / 2.0 = 4.5

In this program, inputs are scanned using the input() function and stored in variable num1 and num2.

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

Select operation from Add(+), Subtract(-), Multiply(*) and Divide(/).

print("Operation: +, -, *, /")
select = input("Select operations: ")

Finally, check operation using if-else statement and operator result will be displayed on the screen.

# add(+) two numbers
if select == "+":
    print(num1, "+", num2, "=", num1+num2)

# subtract(-) two numbers
elif select == "-":
    print(num1, "-", num2, "=", num1-num2)

# multiplies(*) two numbers
elif select == "*":
    print(num1, "*", num2, "=", num1*num2)

# divides(/) two numbers
elif select == "/":
    print(num1, "/", num2, "=", num1/num2)

else:
    print("Invalid input")

Simple Calculator Program using Functions

We can also take the help of a function to create a simple calculator in python. A function is a block of code that performs a specific task.

# Python program to make a simple calculator using function

# This function adds two numbers
def add(a, b): 
    return a + b 
  
# This function subtracts two numbers
def subtract(a, b): 
    return a - b 
  
# This function multiplies two numbers
def multiply(a, b): 
    return a * b 
  
# This function divides two numbers
def divide(a, b): 
    return a / b 

# take inputs
num1 = float(input("Enter first number: ")) 
num2 = float(input("Enter second number: "))

# choise operation
print("Operation: +, -, *, /") 
select = input("Select operations: ")

# check operations and display result
if select == "+":
    print(num1, "+", num2, "=", add(num1, num2)) 
  
elif select == "-": 
    print(num1, "-", num2, "=", subtract(num1, num2)) 
  
elif select == "*": 
    print(num1, "*", num2, "=", multiply(num1, num2)) 
  
elif select == "/": 
    print(num1, "/", num2, "=", divide(num1, num2)) 

else: 
    print("Invalid input")

Output:-

Enter first number: 5.3
Enter second number: 2.32
Operation: +, -, *, /
Select operations: +
5.3 + 2.32 = 7.619999999999999

In this Program, we will be the first defined functions.

# This function adds two numbers
def add(a, b):
return a + b

# This function subtracts two numbers
def subtract(a, b):
return a - b

# This function multiplies two numbers
def multiply(a, b):
return a * b

# This function divides two numbers
def divide(a, b):
return a / b

Inputs are scanned using input() function and select operation from Add(+), Subtract(-), Multiply(*) and Divide(/). Then, check operation using if-else statement and call the function for displayed result.

Also See:- Python Program to Solve Quadratic Equation

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *