123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

Python Program To Make A Simple Calculator

Profile Picture
By Author: Shailendra Bramhvanshi
Total Articles: 1
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

In this article, we are going to learn a simple way to create a calculator in Python. There are many other ways to create advanced or scientific calculators, but we will first see how to create a simple calculator. We have come up with the best video explanation you have ever watched on the internet, where you can understand the logic and code to get the desired output. You can check our YouTube channel.

Let’s start with the coding part.

Code: Create a calculator in Python

In this calculator example, we will do basic mathematical operations with the help of a calculator.

Let’s start with our program here. This is one method to create calculator program in python.

Method 1: Create a calculator in Python

Check out the detailed source code to get the correct output

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
Now, read the explanation of the code line by line for Method 1 to Create a calculator in Python and understand more clearly.
At the start, we have declared a variable choice and assigned value 0.
Later we will store users’ choices into it.
In the next line, we have initiated the while loop, and this while loop will continue to execute unless the user enters choice 5, which is an exit.
So, inside the while loop, we have written 5 print statements for your choice for Addition, Subtraction, multiplication, and division and at the end exit.
We will take user input from the input statement, convert it into an integer, and assign its value to choice.
In the next line, we have checked if the user has entered the correct choice from 1 to 4, then only we will process the calculation part, else we will check if the choice is 5, then terminate the program and tell you to enter the proper choice.
So, if we ask users to enter two numbers, we will convert them into a float for better calculation.
Then we will start conditional checking with the help of if elif and else statements.
In the first if statement, we have checked if the user has entered 1. If yes, we will add two numbers and print the result.
Then we have checked for if the user has entered 2, then do subtraction with a proper message with the result.
In the next line with the Elif statement, we have checked if the choice is 3 or not. If it is 3 then do multiplication of two numbers and then print the multiplication result of two numbers.
And finally, we will write another statement, and we will do division and print the result of the same operation.
Here the basic operations of calculation are not for the mail if condition we have elif condition where the user has entered 5 as a choice.
This is a checkpoint to break while loop and at the end check if the user does not have to enter any of the above choices then show the message as an invalid choice, please retry the same.
Let’s check out the output.
As soon as we run this code system will prompt us 5 statements to choose 1 among 5; we have entered 1.
Then again system ask for two numbers; we have entered 5 and 10
Then the system will check for conditions to check the choice of the user.
In our case, we have selected choice 1, which in addition, hence the system will enter the first if condition and give us a result of 15.
Due to the while loop system will again prompt us for entering choice, we will enter 5 then the system will break the loop.
Here we have completed our first method to create a calculator.
We also offer a Complete Python Certification Course Online. You can reach out to us at https://newtum.com/ or call us at 84229 96372.

Total Views: 260Word Count: 922See All articles From Author

Add Comment

Computer Programming Articles

1. Career Opportunities After Completing The Best Business Analytics Courses In Bhopal
Author: Rohan Rajput

2. Network Security Essentials For It Professionals
Author: TCCI - Tririd Computer Coaching Institute

3. Emerging Programming Languages To Watch In 2026
Author: TCCI - Tririd Computer Coaching Institute

4. Python Scipy Tutorial: Simplifying Scientific And Technical Computing
Author: Tech Point

5. Is Scalatra The Right Fit For Your Web Framework Needs? Why You Should Hire Scalatra Developers Today
Author: Stellanova GlobalTech

6. Why Office 365 Is The Smartest Choice For Modern Businesses
Author: michellumb44

7. Why Choose Bhopal For The Best Business Analytics Courses? A Student’s Guide
Author: Rohan Rajput

8. Top Devops Certification Course In Chennai | Industry-ready Skills
Author: login360

9. Beyond The Code: Key Considerations When You Hire Software Developers For Skill And Cultural Alignment
Author: Stellanova GlobalTech

10. How Cism Certification Can Boost Your Cybersecurity Career
Author: Johnny

11. Which Is The Best Programming Language To Build A Mobile App?
Author: DianApps

12. Explore Career Growth With The Best Business Analytics Courses In Bhopal
Author: Rohan Rajput

13. From Zero To Hero: Python Pygame Tutorial For Aspiring Game Developers
Author: Tech Point

14. Beginner’s Python Pytorch Tutorial: Start Your Ai Journey Today
Author: Tech Point

15. Top 10 Cloud Security Threats And How To Prevent Them
Author: GIGABITTechnologies

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: