Overview
Python Calculator is one of the easiest projects ever a programmer found! It is not just about adding and subtracting a few numbers but it is more about playing with numbers and Python operators along with having fun displaying the result using the Python print() function.
What’s Next!
In this comprehensive Python Calculator guide, we will see how Python programming language is a powerful tool for coding from a simple Python calculator to the difficult one with error handling.
Table of Contents
What is the Python Calculator?
A Python calculator is simple like a real-world calculator. It is performed in the same way, we use other calculators. It is operated on numbers with a few common mathematics operators such as addition (+), subtraction (-), multiplication (*), and division (/). Apart from this, in Python we extend the usage of a calculator and use Python conditional statements, def() function, and while loop to handle the error.
Let’s move ahead and start the journey with the simple calculator!
Python simple Calculator
num1 = int(input("Enter the first number:"))
num2 = int(input("Enter the second number:"))
print() #one line space
print("The addition of ", num1, "+", num2, "=", num1 + num2)
print("The subtraction of ", num1, "-", num2, "=", num1 - num2)
print("The multiplication of ", num1, "*", num2, "=", num1 * num2)
print("The division of ", num1, "/", num2, "=", num1 / num2)
print("The module of ", num1, "%", num2, "=", num1 % num2)
print("The exponential of ", num1, "**", num2, "=", num1**num2)
Explanation: The program starts by asking the user to enter the two numbers, one by one, using the Python input() function. Next, the Python function is used to display the statement/result as per the operator used with the numbers.
Python Calculator – If else, while loop
def calculator():
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print() # one line space
operator = input("Enter the operator: ")
print() # one line space
if operator == "+":
print("The addition of ", num1, "+", num2, "=", num1 + num2)
elif operator == "-":
print("The subtraction of ", num1, "-", num2, "=", num1 - num2)
elif operator == "*":
print("The multiplication of ", num1, "*", num2, "=", num1 * num2)
elif operator == "/":
print("The division of ", num1, "/", num2, "=", num1 / num2)
elif operator == "%":
print("The module of ", num1, "%", num2, "=", num1 % num2)
elif operator == "**":
print("The exponential of ", num1, "**", num2, "=", num1**num2)
else:
print("Invalid operator")
calculator()
while True:
print()
ask_user = input("Do you want to try again?")
print()
if ask_user == "yes":
calculator()
else:
break
Explanation: The program starts by asking the user the numbers and operators using the Python input() function. Once the user enters both numbers the operator is performed with numbers, and the program initiates with an if-else conditional statement.
If the operator is valid:
The conditional statement is designed simply to operate, if the user enters the valid operator the program operates with numbers respective to the operator and displays the result.
The program moves to the while loop, it asks the user whether he/she wants to perform other calculations or not. If the user enters anything except “yes”, the program moves to the else statement and it stops, otherwise the program continues again to perform the calculation from starting.
If the operator is invalid:
On the other hand, if the user enters the wrong operator, then the program shows a message of “Invalid operator” and according to the while loop, the program asks the user to continue the program or not. If the user enters anything except “yes”, the program moves to the else statement, and the program stops, otherwise the program continues again to perform the calculation from starting.
Note: The whole program from asking the numbers to displaying the result is written inside a function called a calculator(). Once the first time calculation is done, by calling the function inside from the while loop, it asks users whether to continue the program or not.
Python Calculator – Error handling
def calculator():
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print() # one line space
print("Operators:", "+", "-", "*", "/", "%", "**")
while True:
print() # one line space
operator = input("Enter the operator: ")
print() # one line space
if operator == "+":
print("The addition of ", num1, "+", num2, "=", num1 + num2)
break
elif operator == "-":
print("The subtraction of ", num1, "-", num2, "=", num1 - num2)
break
elif operator == "*":
print("The multiplication of ", num1, "*", num2, "=", num1 * num2)
break
elif operator == "/":
print("The division of ", num1, "/", num2, "=", num1 / num2)
break
elif operator == "%":
print("The module of ", num1, "%", num2, "=", num1 % num2)
break
elif operator == "**":
print("The exponential of ", num1, "**", num2, "=", num1**num2)
break
else:
print("Invalid operator")
calculator()
while True:
print() # one line space
ask_user = input("Do you want to try again?")
print() # one line space
if ask_user == "yes":
calculator()
else:
break
Explanation: The program works as same as the previous example, except for one difference when the user enters the invalid operator, the program again asks the user at the moment to re-enter the operator. Once the program receives the valid operator, it continues to perform the calculation. Rest implementation is as same as the previous example.