# Simple Calculator Using Python | RecentBlogger

In this blog post, we will learn to create a simple and basic calculator using Python Programming Language. Are you a beginner? Then try out this project and boost up your skills.

You can make below Math Operations:

We'll show the examples with and without Python Functions. We shall get the input from the user regarding which Math operation to perform and then execute the output.
You can get the entire code from my GitHub.

### What are the Prerequisites?

You should have Python 3 installed onto your local computer and have a programming environment set up on the machine, if not yet! Then install Python 3 and set up your machine by clicking here!

### Example -1 (Without Python Functions)

In the below example we write a simple calculator program using Python without any Python Functions.

Now, let's analyze the above program line by line. Displaying the Math operations available to the user using the print() function.

print("Choose the Math Operation:\n
- Subtraction\n
* Multiplication\n
Division")

Next, we get the user input whether to Add ( + ) or Subtract ( - ) or Multiply ( * ) or Divide ( / ).

choice = input("Enter your choice (+ - * /): ")

We add conditional statements to check whether the user entered input is valid as per the given choices or not, by using the if-else statements. Only if valid, we proceed or it goes into else part.

if choice in ('+','-','*','/'):
number_1 = float(input("Enter the first number: "))
number_2 = float(input("Enter the Second number: "))
else:
print("Invalid Input")

After the valid input is received for the choice variable we proceed with getting the inputs (number_1 & number_2) to perform the calculations.

if choice == '+':
output = number_1 + number_2
print(f"Output:\n {number_1} + {number_2} = {output}")
elif choice == '-':
output = number_1 - number_2
print(f"Output:\n {number_1} - {number_2} = {output}")
elif choice == '*':
output = number_1 * number_2
print(f"Output:\n {number_1} * {number_2} = {output}")
elif choice == '/':
output = number_1 / number_2
print(f"Output:\n {number_1} / {number_2} = {output}")

Here, we do perform the Math Operation based on the choice variable and print out the respective output. The final output is below.

Choose the Math Operation:
- Subtraction
* Multiplication
/ Division
Enter your choice (+ - * /): +
Enter the first number: 9
Enter the Second number: 6
Output:
9.0 + 6.0 = 15.0

### Example - 2 (With Python Functions)

In this example, we shall write using Python Functions.

Now, let's analyze the code.
We get the user input for choosing the Math operation first. Validate it and ask for inputs to perform operations. Same as the previous code. The only difference here is that we use Python functions to perform respective operations. The functions are as below.

return a + b

def subtract(a, b):
return a - b

def multiply(a, b):
return a * b

def divide(a, b):
return a / b

User-defined functions add(), subtract(), multiply() and divide().
Later, we use a while loop to continue the process till it meets the desired break condition.

while True:
choice = input("Enter your choice (+ - * /): ")
if choice in ('+','-','*','/'):
number_1 = float(input("Enter the first number: "))
number_2 = float(input("Enter the Second number: "))
if choice == '+':
print(f"Output:\n {number_1} + {number_2} =", add(number_1, number_2))
elif choice == '-':
print(f"Output:\n {number_1} - {number_2} =", subtract(number_1, number_2))
elif choice == '*':
print(f"Output:\n {number_1} * {number_2} =", multiply(number_1, number_2))
elif choice == '/':
print(f"Output:\n {number_1} / {number_2} =", divide(number_1, number_2))
proceed = input("Do you wanna continue? ( y/n ): ")
if proceed == 'n':
break
else:
print("Invalid Input!")

Now. let's see the final output below.

Choose the Math Operation:
- Subtraction
* Multiplication
/ Division
Enter your choice (+ - * /): *
Enter the first number: 9
Enter the Second number: 6
Output:
9.0 * 6.0 = 54.0

Do you wanna continue? ( y/n ): y
Enter your choice (+ - * /): /
Enter the first number: 9
Enter the Second number: 6
Output:
9.0 / 6.0 = 1.5

Do you wanna continue? ( y/n ): n

Hope you guys gained some knowledge today!

#### You can get the entire code from my GitHub here.For more posts do check out here.

1. 