Subscribe to the Islander Newsletter

Wise choice future islander! Nuggets of gold shall be sent your way!
Email address
Secure and Spam free...
How to Make a Calculator in Python The Easy Way

How to Make a Calculator in Python The Easy Way

Do you want to know how to create a calculator in Python?

If so, read on because you will definitely know how by the end of this blog post!

As a disclaimer before we begin, I want to make sure you know a few things:

First, this post assumes you know what a print statement is, what a variable is, the 3 main types of variables in Python, how to get input from a user and what if, elif and else statements are.

If you do not know what those properties are in Python, you need to review or learn them first before following along with this tutorial.

Otherwise (else for those who know Python :)), this tutorial will not make sense at all.

Additionally, I am going to use a text editor called PyCharm to write the code. You can use any text editor you want (ex. Atom), but personally, I find PyCharm to be easier to use since you don’t have to switch back and forth between the text editor and the Powershell to run your program. You just write the program, play it and the outputs come out.

I use the community edition and it’s absolutely free to download. You can download it here on jetbrains.com. If you need to download it, definitely watch this tutorial here.

I am also using Python 3.6 in this tutorial, so if there are some differences, please note that and upgrade or switch to Python 3.6 if need be.

With that said, open your text editor and let’s start making this calculator!

How to Create the Calculator

The first thing you are going to do is define solution as a blank variable with quotation marks. This is so you can manipulate it later on in the if-else statements we are going to write. If you don’t do this, you will not be able to run the if-else statements.

You’ll then define the inputs you want the user to put in. Obviously, they need to put in something in order for this calculator to run just any other calculator would.

So for the first 4 lines of the code, type this in:

How to Make a Calculator in Python The Easy Way

The user will manually enter the numbers they want and these numbers will actually be strings when they are inputted into Python.

In case you don’t know, a string is a data value that had text in between quotation marks. In other words, the numbers we type in will not be numbers when we type them into the input, so we will have to do a special operation later on to make them numbers.

Now the spaces at the end of the input phrases are there to not confuse the user that they need to write their answers next to the period. Hence, it makes everything grammatical sense.

For operation, it will remain as a string. The thing with operation is depending on what operation the user inputs, the program will perform a certain calculation. This is where if-else statements come in to account for the different conditions that can occur.

Now, write this next piece of code down on a sheet of paper (or go ahead and guess what I wrote down before you see it; you’ll understand this much better that way.):

How to Make a Calculator in Python The Easy Way

While I am explaining this, please note that Python is going in the order you wrote the program to perform the tasks you told it to do. Also, see this slowly so you can visualize it better.

This will help you see why you are writing what you are writing instead of most people who think in terms of the program going fast.

Here’s what Python is doing in English:

Python is taking all of the inputs we put in so far and it moves down to the if statement.

It reaches the if statement where it recognizes you assigned a Boolean value to it with operation == “Addition” = True. It will check to see if this Boolean value is true and if it is true, it will add the two numbers together.

The reason the conditions for the if, elif and else statements are Boolean values is because when we inputted the operation we want Python to perform, Python stores this input in the operation variable.

As a result, because we want to check its condition, there are 2 possible scenarios the condition can be, which are true and false.

Essentially, the input assigned to the operation variable becomes a data value in of itself and hence why it is a Boolean variable.

Also, when we equate operation to Addition, the reason it’s a double equal sign instead of normal equal sign is because the double equal sign is the convention for equating things in if-elif-else statements. If I put just one equal sign there, I would get an error since it’s an improper syntax.

However, if we go elsewhere from the if-elif-else statement Boolean values, you can use the normal equal sign without any problem since you are simply equating a variable to a data value. This is why solution doesn’t have a double equal sign. It’s not an if-elif-else Boolean value.

Now note where we wrote float() in each of the statements. This is to convert your inputted numbers that are strings into actual numbers. Additionally, because we want to account for decimal values, we want to use float() over int(). If we use int(), the program will round the imputed numbers to its closest integer and the final answers will be wrong.

Now if the Boolean value for operation == “Addition” = True, it will skip every conditional statement that is there and go straight to the print(solution) to print the solution variable (our final answer) since there is nothing else to check before it.

However, if the Boolean value for operation == “Addition” = False, then it will go onto the next item in the if-else statement chain. Remember, Python is going in the order you wrote the code in when you wrote the program.

In this case, we have 3 other Boolean conditions that have unique operations attached to them, hence why we wrote the elif statements there instead of going straight to else.

Python will check if each of these conditions are true. If so, Python will run the operations below them. If not, it will move onto the next one and see if that condition is true and so on.

Now if these independent conditions in the if and elif statements are all false, Python will see if an else statement is up next since all other scenarios are true. In this case, there is an else statement and it will run whatever is in it. Basically, if the user inputs something aside from “Addition,” “Subtraction,” “Multiplication” and “Division,” Python will tell the user to run the program again.

Unfortunately, I could be wrong about this, you cannot get Python to go back to the beginning of the if-elif-else chain if you put an operation aside from the ones in the if-elif statements. You have to run the program again to reassign a new data value to operation.

Now once it checks all of the conditions in the if-elif-else chain, Python will go to the next operation on the program, which is to print out the solution variable.

After else is run, solution will be printed out as an empty string since nothing aside from quotation marks got assigned to it.

Now if any of the if-elif conditions are true, since solution is given data values, it becomes part of the script and hence, solution will print out.

And in this is the program from start to finish.

You are basically taking the numbers from the user and how they want to do their calculation and based on the type of calculation the user desires, the program will do that particular calculation with the two numbers you give it.

When you run it in Python, you should get something like this when you try to multiply them:

How to Make a Calculator in Python The Easy Way

Now if I tried the same numbers, but I wanted to subtract them, I would get this:

How to Make a Calculator in Python The Easy Way

For addition,

How to Make a Calculator in Python The Easy Way

And for division,

How to Make a Calculator in Python The Easy Way

For your reference, this is what the final code should look like as a whole:

How to Make a Calculator in Python The Easy Way

And that’s it!

That’s how you build a calculator in Python!

If you have any questions about this tutorial or if you want to say something positive about it, feel free to leave your comments below. I am more than happy to assist you!

Until next time,

This is Evan signing off.

PS: If you want more Python tutorials and wickedly mind-blowing blog posts that will skyrocket your productivity in no time, get you to your full potential and help you dominate college like an almighty god, then subscribe in the action box below to Join the Island, the greatest blog in the world, to receive these posts the moment they are published!

PPS: I’m thinking of a way this code can be improved. If are the first to tell me what the secret improvement is and why it’s the secret improvement, I’ll give you a secret gift just for you 🙂 So go ahead, give it a try! You have nothing to lose!

Evan Cruz
+ posts

Evan Cruz is the founder of Join the Island, the website committed to helping young adults become massively productive and reach their full potential.

He has been featured on Vox, OnlineU, and UpJourney. He has also a cited human relations expert and college expert.

He graduated Magna Cum Laude with a Bachelor of Science Degree in Civil Engineering.

Read more about Evan and Join the Island here.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Islander Newsletter

Wise choice future islander! Nuggets of gold shall be sent your way!
Email address
Secure and Spam free...