# Calculator Program in Python

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
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"
"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")````

Output

Here is the output:

CASE 1:

CASE 2:

Explanation:

Here, a user asks to enter two numbers. These numbers are stored in two different variables. Then select the desired option to perform operations. After this, the function will return the selected operation that represents different arithmetic operations. After taking input, we check the selected operation using if...elif...else statements. If the operation is valid, the value will be evaluated based on the respective operations, and finally, print the result. If we select any other input, it will print the wrong operation, and the loop continues until a valid option is selected.