HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

7) Numbers and Math Lesson

Create a Calculator with Python Operators

10 min to complete · By Martin Breuss

A straightforward use of any sort of numbers is calculations. You can take numbers and combine them using operators. Python comes with all your favorite math operators already included, so you can use it as a calculator out of the box.

Colorful illustration of a light bulb

Info: When you work with your local Python REPL, the result of a calculation gets displayed right away. If you are using the online coding environment provided below, you need to wrap your calculations in a function called print() in order to display the output. You will learn more about functions and how to use them in a later chapter; for now, just remember that you need to put your calculations inside of the brackets in order to display the output of your calculation.

Python Calculator Practice

Go ahead and dig out some math examples from your school days. Or find your latest shopping list. Or just make up some numbers! Punch the numbers into your Python interpreter or the coding playground below to see how you can use Python as a calculator:

# Perform some calculations below
print(2 + 5)
print(10 * 10)

You can use the common math operators such as +, -, *, / like you'd expect to make calculations with Python. It looks like you just found a possible use case for Python that can have everyday applications.

Use the Python Console

The next time you're reaching for your phone or an old-school calculator to figure out how much you spent on broccoli over the past month, try to use a Python console instead!

It's important to train by opening up your Python console, typing some code, and running it. However, drill aside, you probably have a calculator at hand that is more user-friendly than that.

Built-in calculator app on MacOS

But copying your calculator app with a less shiny, text-based interface isn't Python at peak performance. Because with Python, you can do much more than just add numbers!

Calculate With Python Variables

It's time to combine what you learned about variables with this new idea of using Python as a calculator. Unlike with a normal calculator interface, you can give names to your values in Python. Then, you can operate on those names instead of the bare numbers.

Example

Check out the following example that still uses numbers, but references them via variables:

too = 2
fast = 1
furious = -3

title = too + fast + too + furious
print(title)  # 2

Of course, this "calculation" doesn't make much sense in terms of the variable names you used here. It does output the number of the movie in the series, however. Coincidence? ¯\_(ツ)_/¯

Colorful illustration of a light bulb

Info: Note how you're referencing the numbers using variables. Using variables that hold values is a central concept in programming that you'll keep drilling.

You may have struggled with mathematics in school because symbols and numbers seem like unintuitive concepts to wrap your head around, which makes it hard to use them as building blocks for calculations!

Programming can help you out. When you write programs, you can give your own names to any values that you are working with. This can make writing calculations similar to writing a short essay.

Practice with Python REPL

Use your Python REPL or a code playground to write a different "calculation". Create your own variables and give them descriptive names, then try to set them in context with the output of the calculation. Go ahead and share your mini-calculation essay on Discord.

Don't be shy, and keep in mind that this calculation doesn't have to make any sense. It's just for fun and to get more familiar with the concept of using variables for performing calculations in programming.

Common Arithmetic Operators

You may have already played around with a lot of the arithmetic operators that you remember from your math education. In any case, check out this list of some of the operators that you can use with Python:

Operator Meaning Example Result
+ Addition 2 + 2 4
- Subtraction 4 - 2 2
* Multiplication 2 * 2 4
/ Division 10 / 5 2.0
** Exponent 3**2 9

Most of these might feel straightforward for you. However, there is one that you should pay closer attention to.

Python Division

When you look at the example for division, you can see that the result looks different than the other results. What is going on here? What type does this result have? Try this out in the code playground:

whole = 1
part_of_whole = whole / 2
print(part_of_whole)

# Check the type() of part_of_whole below

As you can see, Python knows that it cannot correctly represent the result of 1 / 2 as an integer data type, so it automatically converts the result of the calculation into a float.

Colorful illustration of a light bulb

Info: You can even perform operations on a mix of ints and floats. This is possible because Python can implicitly convert integers to floating-point numbers if necessary. When you perform a calculation that includes both types, you will always end up with a floating-point number.

As you can see, this means that data types aren't exactly fixed in Python. You can often convert a value from one data type into another. However, some aspects of the original value might get lost in this conversion. You'll look at this in more detail in the upcoming lesson.

Summary: Python Calculator

  • Python comes with a built-in calculator which is readily available in the REPL
  • Use descriptive variable names to make your calculations more understandable
  • Use the Python calculator instead of your desktop or phone calculator to build muscle memory and practice using the console
  • Python can automatically convert the result of a division to the float data type if required