+ INFORMATION

Share on social networks!

Your first calculator in Python

How to make a calculator in Python?

Last week we talked to you about the while loops in Python, its characteristics and operation. Well, this week let's make use of them and we are going to schedule a simple calculator to practice.

If you want to continue learning about programming you can do so at EIP with our master in Advanced Programming in Python for Big Data, Hacking and Machine Learning.

how to make a calculator in python

We begin by collecting the two numbers with which our calculator is going to work, we create a variable for the selection of the operation that we want our calculator to perform and we create our loop with the list of options where the condition would be the user's choice to exit the program by pressing option 6.

Screenshot 2021 05 11 at 11.16.31

After in operation function that you want to perform, would fall within one option or another of our 'if' or 'elif' conditional statements.

In option 4, division we convert the result into float to be able to print decimals, since otherwise it will only print the integer part, ignoring all decimals.

We are going to put it to the test, We enter the numbers 7 and 3.

nwoJvpEhMjwBts DOutLbKpTKEiO2fRDE 1tojyY9VL20kUFOQnuPseK4VcGkescp4INSp2vVc dW6Inn0SfXWWKJsp1hfZ 4LxdipEyKv306iQNUX3RaN4mLh8bKI74 p2AqGkVofH6oNnv g

Let's add them up, so we mark option 1 of our fantastic calculator and since we are wonderful programmers, it prints the line with the result of the sum, 7 + 3 = 10.

We try the rest of the operations and we see that they all work like a charm.


The option 5 It allows us to change the numbers to work with.

gTkpXV mSa2579STL4e46cVIz8wndmLrxislKGHW391R4vyZNUlRTevh2vNTAslL8LKQBU71qXmQ6y PLyFsznN yTH8QgxtjeRH vDjks v0h3bwDq300tbWVEdlgKq04A fFVu dikLpbfmQ

And the last option turns off our calculator by printing the goodbye message.

mJIhjAV87yGjDM8gSnna5ZWDkni7pCHLrMIerWGcTKHxpRC6tONXfPs5Q2SLcxRS8gHxSxeeIKss9dJJz zXTRn2adqGJow8OZdygGY2uOZ PZwyu2Bm37Aydm9uXKtA8trr7EkeB0Jho rI9Q

Subscribe to our newsletter to stay up to date with all the news

EIP International Business School informs you that the data in this form will be processed by Mainjobs Internacional Educativa y Tecnológica, SA as the person responsible for this website. The purpose of collecting and processing personal data is to respond to the query made as well as to send information about the services of the data controller. Legitimation is the consent of the interested party.
You can exercise your rights of access, rectification, limitation and deletion of data in compliance@grupomainjobs.com as well as the right to file a claim with the supervisory authority. You can consult additional and detailed information on Data Protection in the Privacy Policy that you will find in our Web page
marter-in-python

Leave a comment