# Python Program to Make a Simple Calculator

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

When it comes to working with numbers and evaluating mathematical equations, the Python programming language is an excellent choice. This quality can be used to create beneficial software.

This tutorial will walk you through creating a simple command-line calculator app in Python 3. While we’ll go over one method for creating this software, there are several ways to modify the code and develop a more sophisticated calculator.

To create our calculator, we’ll employ math operators, variables, conditional statements, functions, and user input.

## Program to Implement Simple Calculator

Below are the steps to implement the simple calculator in python:

### 1)Taking input from user

When a human enters equations for the computer to solve, it works best. We’ll begin developing our program where the human enters the numbers that they want the computer to operate with.

To accomplish this, we’ll use Python’s built-in input() function, which accepts user-generated keyboard input. We can supply a string to prompt the user inside the parenthesis of the input() function. The user’s input will be assigned to a variable.

We want the user to enter two numbers for this application, so make the software prompt for two numbers. We should include a space at the end of our string when asking for input so that the user’s input is separated from the prompting string.

# given two numbers
number1 = input("Enter the first number of which we wan to do perform calculation: ")
number2 = input("Enter the first number of which we wan to do perform calculation: ")

If you run this program a few times with different input, you’ll find that when prompted, you can enter anything you want, including words, symbols, whitespace, or just the enter key. This is due to the fact that input() accepts data as strings and is unaware that we are seeking for a number.

We want to utilize a number in this software for two reasons:

1. To allow it to execute mathematical calculations.
2. To ensure that the user’s input is a numerical string.

Depending on the calculator’s requirements, we may want to convert the string returned by the input() function to an integer or a float. We’ll wrap the input() function in the int() method to convert the input to the integer data type, as whole numbers suit our needs.

# given two numbers
number1 = int(input("Enter the first number of which we wan to do perform calculation: "))
number2 = int(input("Enter the first number of which we wan to do perform calculation: "))

### 2)Defining and implementing mathematical operators

Let’s now add operators to our Calculator software, such as addition, multiplication, division, and subtraction.

Below is the implementation:

# given two numbers
number1 = int(
input("Enter the first number of which we wan to do perform calculation: "))
number2 = int(
input("Enter the first number of which we wan to do perform calculation: "))
# adding the given two numbers
print('{} + {} = '.format(number1, number2))
print(number1 + number2)

# subtracting the given two numbers
print('{} - {} = '.format(number1, number2))
print(number1 - number2)

# multiplying the given two numbers
print('{} * {} = '.format(number1, number2))
print(number1 * number2)

# dividing the given two numbers
print('{} / {} = '.format(number1, number2))
print(number1 / number2)


Output:

Enter the first number of which we wan to do perform calculation: 19
Enter the first number of which we wan to do perform calculation: 13
19 + 13 =
32
19 - 13 =
6
19 * 13 =
247
19 / 13 =
1.4615384615384615

If you look at the result above, you’ll note that as soon as the user enters number 1 as 19 and number 2 as 13, the calculator does all of its operations.
We’ll have to utilize conditional statements and make the entire calculator software a user-choice based operation program if we want to limit the program to only performing one operation at a time.

### 3)Using conditional statements for user-choice based operation Program

So, to make the user realize what he or she is expected to choose, we’ll start by adding some information at the top of the program, along with a decision to make.

Below is the implementation:

givenChoice = input('''
Please select which type of operation which we want to apply\n
enter - for subtraction  operation\n
enter * for multiplication  operation\n
enter / for division  operation\n''')
# given two numbers
number1 = int(
input("Enter the first number of which we wan to do perform calculation: "))
number2 = int(
input("Enter the first number of which we wan to do perform calculation: "))
if givenChoice == "+":
# adding the given two numbers
print('{} + {} = '.format(number1, number2))
print(number1 + number2)
elif givenChoice == "-":
# subtracting the given two numbers
print('{} - {} = '.format(number1, number2))
print(number1 - number2)
elif givenChoice == "*":
# multiplying the given two numbers
print('{} * {} = '.format(number1, number2))
print(number1 * number2)
elif givenChoice == "/":
# dividing the given two numbers
print('{} / {} = '.format(number1, number2))
print(number1 / number2)
else:
print("You have entered the invalid operation")


Output:

Please select which type of operation which we want to apply

32