Python Can Be Your Calculator

There’s a ton of things that you can do with Python and you’ll learn many of them in this course. But before we dive into complex subjects, let’s have some fun with another simple task that you can do with Python. We are going to make Python our calculator.

Let’s start with something easy. So 4 plus 5 is 9, 9 times 7 is 63, minus 1 divided by 4 is minus 0.25. Easy. Repeating or periodic numbers are printed in a longer format. Let’s try 1 divided by 3. In math theory, when 1 is divided by 3, the digit 3 repeats forever after the decimal point. Of course, it’s hard to display something that repeats for ever. So instead, we have a representation showing lots of decimal places. Not too hard, right?

Let’s get the computer something a bit trickier. Let’s say we want to divide 2050 by 5, then subtract 32 and then divide the result by 9. To do this, we’ll need to use parentheses, just as we do in typical math problems. You can also use Python to get squares, cubes, or any power of n of a number. For example, let’s say we want to find out what 2 to the power of 10 is. To get Python to give us the answer, we use the double star operator.

If you’re starting to worry that this is becoming an algebra course, relax. We’re not going to do anything more complex than what we’ve just seen. If you’re thinking, «Why would I use Python instead of just a normal calculator?» That’s a valid question. Experimenting in this way, you get familiar with the language’s math capabilities. In IT jobs, there are many tasks that require you to use math calculations.

You might need to count how many times a certain word appears in a text, or work out the average time it takes for an operation to complete, or how much you have to compress an image to fit in certain size constraints. Whatever you need to calculate, writing a script can help you do it faster and with more accuracy. So you need to know what mathematical operations are available to you.

Python actually has a lot more advanced numeric capabilities that are used for data analysis, statistics, machine learning, and other scientific applications. We won’t get into these in this course. But if you want to learn more about them on your own, there’s a wealth of online resources available. Next up, a cheat sheet to help you with programming concepts that we’ve just covered. After that, it’s time for another quiz. This time with a few small coding exercises.

Remember, if something is unclear, you can re-watch the videos as many times as you need. Ready? You’ve got this.