We are presenting you with one of the shortest and simplest calculator programs in python you could ever found. This calculator will contain the most basic functions you have seen in your life.
We will be creating a calculator that will add, subtract, divide, and multiply two numbers.
We will run the program until we don’t want to exit using a while loop, so that we can calculate as many as calculations we want.
If you want the calculator program in python gui then click here.
First, we will ask the user to choose one of the four basic operations i.e. “Addition“, “Subtraction“, “Multiplication“, and “Division“.
print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Division")
We will also give choice to the user so that he can exit the program whenever he wants.
Now, we will store the value entered by the user using the variable “choice“.
choice = int(input("Enter your choice: "))
Now, we will see the code and we will use comments to explain to you this basic Calculator Program In Python.
Calculator Program In Python: Code
# creating while loop while True: # printing the available options print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Division") print("5. Exit") # asking user to Enter his choice choice = int(input("Enter your choice: ")) # checking the choice between 1 and 4 if (choice>=1 and choice<=4): # asking to enter two options print("Enter two numbers: ") # accepting first number num1 = int(input()) # accepting second number num2 = int(input()) # checking if number is 1 if choice == 1: # adding res = num1 + num2 # printing Addition print("Result = ", res) # checking if number is 2 elif choice == 2: # Subtracting res = num1 - num2 # printing Subtraction print("Result = ", res) # checking if number is 3 elif choice == 3: # Multiplication res = num1 * num2 # printing Result print("Result = ", res) # after checking all 3 choice, # only one operation left, i.e. for Division else: # Division res = num1 / num2 # printing Result print("Result = ", res) # checking if the choice is 5 elif choice == 5: # if choice is 5, we will exit the program exit() # everything, except the five choices is useless # so, we will print Wrong input..!! else: print("Wrong input..!!")
Calculator Program In Python: Output
1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit Enter your choice: 1 Enter two numbers: 12 23 Result = 35 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit Enter your choice: 2 Enter two numbers: 23 12 Result = 11 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit Enter your choice: 3 Enter two numbers: 12 23 Result = 276 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit Enter your choice: 4 Enter two numbers: 24 12 Result = 2.0 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit Enter your choice: 5
This progam is very basic level Calculator Program In Python for beginners. But, we can add more choice and operations according to our needs.
Thanks for reading
If you found something wrong in the article, then please let us know.
- Top 25 Pattern Programs in C++In this article, I will show you the Top 25 Pattern Programs in C++ that you must learn which will help you to understand the usage of nested loops. We will learn to create different geometrical shapes like Squares, triangles, Rhombus, etc. We have provided the code as well as output for all the patterns…
- Currency Converter in C++In this article, we will build a simple program for Currency Converter in C++. The user will be able to convert currencies like Dollar, Euro, Pound, and Rupee. This is a console application and very simple to understand as we only use a bunch of conditionals to write the entire program. Features Users can convert…
- SQLite | CRUD Operations in PythonCRUD stands for Create Read Update Delete. I will show you how to perform CRUD Operations in Python. You need basic Tkinter and SQLite knowledge before you read further. This app is straightforward, when you will open this app, a GUI with 4 green colored buttons will open to perform CRUD(create read update delete) operations….
- Number Guessing Game in C++In this article, we will build a simple Number Guessing Game in C++. It’s a game in which the player has to guess a secret number in a range that is generated by the computer and upon a wrong guess by the player, the game will give hints to the player to guess the correct…
- Image background remover in PythonHello friends, in this article, we will learn how to create an Image background remover in Python. I will first show you a simple program and then you will see a smile GUI made using tkinter. We need to install rembg, you can install rembg using the pip command: Image background remover in Python program…