Python Program to Make a Simple Calculator

(Last Updated On: 08/09/2023)

Python is a high-level, all-purpose programming language. Code readability is prioritised in its design philosophy, which makes heavy use of indentation. To perform mathematical operations like addition, subtraction, multiplication, and division, Python may be used as a calculator.

Python may also be used to do statistical and trigonometric computations. Speed, accuracy, and dependability are benefits. Python always follows your instructions and typically writes out more decimal places than the majority of calculators.

In this article, we will write a simple Python program to make a simple calculator. This program allows users to perform basic mathematical operations such as addition, subtraction, multiplication, and division. The program is executed using a while loop that continues until the user chooses to exit by selecting option 5.

Also Read: Create Calculator using Eval in Python.

Video Explanation to create a calculator in Python

Create a Simple Calculator in Python

Let’s break down the code to understand how it works. First, the user is presented with a menu of options to choose from, including addition, subtraction, multiplication, division, and exit. The user is asked to enter a choice from 1 to 5.

The input is stored in the “choice” variable, which is used to control the execution of the program. If the user selects a value between 1 and 4, they are prompted to enter two numbers. These numbers are stored in the variables x and y.

Next, the program uses an if-elif-else statement to determine which mathematical operation to perform based on the user’s choice. If the choice is 1, the program performs addition and displays the result. Similarly, if the choice is 2, the program performs subtraction, and if the choice is 3, the program performs multiplication. Finally, if the choice is 4, the program performs division.

If the user selects option 5, the program exits the while loop and terminates. If the user enters an invalid choice, the program displays an error message and asks the user to enter a valid choice. In conclusion, this simple Python program demonstrates how to create a calculator using a while loop, if-elif-else statements, and user inputs. The program can be easily modified to include more mathematical operations or additional features.

choice= 0
while choice!=5:
    print("1. Addition")
    print("2. Subtraction")
    print("3. Multiplication")
    print("4. Division")
    print("5. Exit")
    choice = int(input("Enter choice from 1 to 5:"))
    
    if choice in range(1,4):
        x = float(input("Enter first number:"))
        y = float(input("Enter Second number:"))
        if choice == 1:
            z = x + y
            print("Addition of  ",x," and ",y," is ",z)
        elif choice == 2:
            z = x - y
            print("Subtraction of  ",x," and ",y," is ",z)
        elif choice == 3:
            z = x * y
            print("Multiplication of  ",x," and ",y," is ",z)
        else:
            z = x / y
            print("Division of  ",x," and ",y," is ",z)
    elif choice == 5:
        break
    else:
        print("Invalid choice!! Please retry")
Output:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter choice from 1 to 5:1
Enter first number:2
Enter Second number:2
Addition of   2.0  and  2.0  is  4.0

Conclusion

This is a simple code written in Python that implements a basic calculator with 4 arithmetic operations (addition, subtraction, multiplication, and division).

It starts by setting the variable “choice” to 0. Then, it enters a while loop that continues as long as “choice” is not equal to 5. Within the loop, it displays a menu of the four operations and an exit option (choices 1 to 5).

The user inputs their choice, which is then stored as an integer in the variable “choice”. If the user selects one of the four arithmetic operations (choices 1 to 4), the code prompts the user for two numbers (x and y), performs the chosen operation, and prints the result. If the user selects 5, the loop breaks and the program exits. If the user inputs an invalid choice (other than 1 to 5), the code prints an error message. Now you know how to make a simple calculator using python, Happy learning!

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions

About The Author