Simple Calculator Program in Python

This example helps to learn how to create a simple calculator program to perform operations like addition, subtraction, multiplication, and division.

Source Code

The following is the source code to perform simple calculations. In this program, we will perform addition, subtraction, multiplication, and division operations.

Function to perform addition operation

def addition(num1, num2):

return num1 + number2

Function to perform subtraction operation

def subtraction(num1, num2):

return num1 — num2

Function to perform multiplication operation

def multiplication(num1, num2):

return num1 * num2

Function to perform division operation

def division(num1, num2):

return num1 / num2

Choose operation

print(“Select desired operation: \n”

“1.Add\n”

“2.Substraction\n”

“3.Multiplication\n”

“4.Division”)

while True:

# Take input from the user

operation = input(“Choose operation(1/2/3/4): “)

# Check if operation is one of the four options if operation in (‘1’, ‘2’, ‘3’, ‘4’):

number1 = float(input(“Enter first number: “))

number2 = float(input(“Enter second number: “))

if operation == ‘1’:

#Print sum of two number

print(number1, “+”, number2, “=”, addition(number1, number2))

elif operation == ‘2’:

# Print subtraction of two number

print(number1, “-”, number2, “=”, subtraction(number1, number2))

elif operation == ‘3’:

# Print multiplication of two number

print(number1, “*”, number2, “=”, multiplication(number1, number2))

elif operation == ‘4’:

# Print division of two number

print(number1, “/”, number2, “=”, division(number1, number2))

break else:

print(“Wrong operation selected”)

https://www.tutorialandexample.com/python-mysql/