No matter what you’re building, it’s likely that numbers will come up somewhere in your code. You may use numbers to store the prices of items on a menu for a restaurant app or to perform conversions between different currencies in a currency exchange app.
Beginners to Python commonly ask how to perform mathematical operations on numbers in their code. This tutorial will answer that question. We will help you navigate the basics of mathematical operators in Python. By the end of reading this tutorial, you’ll be expert at using Python’s built-in math operators.
What Is an Operator?
Before we discuss how to use Python’s math operators, we need to take a step back and talk about operators.
An operator is a special symbol that performs a specific action in a program. For instance, the minus sign (-) is an operator. It is used to perform a subtraction operation.
Operators come in all flavors, but for this tutorial we will focus on the Python math operators. You may also hear these referred to as “arithmetic operators.”
Addition and Subtraction
Now that we know the basics of operators, let’s delve into how to use Python’s math operators, starting with addition and subtraction.
The plus sign (+) allows you to perform addition in a program. The minus sign (-) allows you to perform subtraction. Let’s explore a few examples of these symbols in a program.
Suppose we want to add 5 and 10 together. We could do so using this code:
print(5 + 10)
Our code returns: 15.
Python developers often use variables in math equations. This can improve readability, because you can assign a label to each variable which will help you keep track of the purpose of the values with which you are working. For example, to subtract 19 from 27, we could use this code:
a = 27 b = 19 print(a - b)
Our code returns: 8.
In our code, “a” and “b” are variables. We set the value of “a” to 27 and the value of “b” to 19. Then, we subtracted “b” from “a”.
When working with Python’s math operators, we can use both positive and negative numbers (Assuming, of course, the numbers we use can yield a mathematically-accurate return. If you try to divide a number by 0, for instance, an error will be returned.) For example, we could add a positive number to a negative number, like so:
a = 8 b = -9 print(a + b)
Our code returns: -1.
So far, we’ve been working with integers. But we can also use floating-point numbers (decimal numbers) in mathematical operations in Python. If we specify a decimal number in a mathematical operation, the program will return a decimal number.
Suppose we want to add 8.2 to 4. We could do so using this code:
print(8.2 + 4)
Our code returns: 12.2.
As you can see, our program returned the sum of our two numbers, represented as a decimal number.
Multiplication and Division
The single asterisk sign (*) is used to multiply numbers in Python, and the single forward slash sign (/) is used to divide numbers in Python.
Let’s say we want to multiply 92 by 8. We could do so using this code:
a = 92 b = 8 print(a * b)
Our code returns: 736.
As you can see, we used the * operator to multiply our numbers.
Similarly, let’s say we want to divide two floating-point numbers. We could do so using this Python 3 code:
a = 18.2 b = 2 print(a / b)
Our code returns: 9.1.
In this code, we used the forward slash to indicate we wanted to divide our numbers.
It’s worth noting that when you’re dividing numbers using the forward slash in Python 3, the final result will always be an unrounded float (floating-point number). However, if you are dividing a number in Python 2 using the forward slash, the final result will be an integer.
So, when we run our above code in Python 2, the program returns: 9.
The percentage sign (%) functions as the Python modulo operator. This operator returns the mathematical remainder, rather than the final result, of a division operation.
Suppose we want to find the remainder of 20 divided by 6. We could do so using this code:
a = 20 b = 6 print(a % b)
Our code returns: 2.
20 can be divided by 6 three times, and the remainder is 2. Therefore, the program returns the value 2.
Two asterisks (**) make up the Python power operator. The power operator allows you to raise one number to the power of another number. In other words, the power operator allows you to multiply a number by itself a certain number of times.
So, suppose we wanted to calculate 5 to the power of 3—in other words, to multiply 5 by itself 3 times (5 x 5 x 5). We could do so using this code:
a = 5 b = 3 print(a ** b)
Our code returns: 125.
The power operator tells our program to raise the value of “a” to the exponent of the value of “b”. Thus, using the values we assigned, the program computed 5 to the value of 3 and returned the value 125.
Order of Operations (Operator Precedence)
In mathematics, problems are solved via a specific order of operations. Predetermined rules dictate the order in which you perform calculations containing multiple operations.
The same is true in Python. In fact, Python follows the standard order of operations used in mathematics.
Consider the following programming statement:
problem = 10 + 15 / 2
The answer to this problem, if read left to right and without regard for the standard order of operations, is 12.5. However, this is not the correct answer.
When you’re doing math problems, you must complete division operations before you complete addition operations. So, 15 should be divided by 2 first. Then, the result of that is added to 10. The correct answer is 17.5
Likewise, if we run this problem in Python, the program returns: 17.5.
The order of operations in math is as follows:
- Order (power / square roots)
This makes up the acronym BODMAS. When Python is working through a math problem, it will use this order.
Python Operator Reference Table
In this tutorial, we explored the main Python operators. Here is a reference table of what we covered:
|+||Adds two numbers.|
|–||Subtracts the right number from the left number.|
|*||Multiplies two numbers.|
|/||Divides the left number by the right number.|
|%||Calculates the remainder of a division sum.|
|**||Raises the left number to the power of the right.|
Python offers a wide range of mathematical operators that allow you to work with numbers in your code.
In this tutorial, we discussed how to use the addition, subtraction, multiplication, division, modulo, and power operators. We also discussed the order of operations Python follows when solving math problems. Now you’re ready to start using Python’s math operators like an expert!
Are you looking for a Python training program? Download the free Career Karma app today to talk with a career coach about how you can get the training you need to pursue a career as a Python developer.