9) Operators and Booleans Lesson

Python Arithmetic Operators

8 min to complete · By Martin Breuss

You've already used arithmetic operators to perform mathematical operations in the section about numbers and math. Many common mathematical operators work the same in Python, which is also why you can use Python directly as your calculator.

However, there are also a few arithmetic operators that might seem a little unfamiliar.

List of Available Arithmetic Operators

The following table summarizes all available arithmetic operators in Python:

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

Additional Arithmetic Operators

While you've already worked with the more familiar ones, there are a few new ones stuck to the bottom of this table that you'll take a closer look at now.

Modulo

The modulo operator, also sometimes called modulus, allows you to divide by a number and get the remainder as your result:

5 / 2   # Normal division  OUTPUT: 2.5
5 % 2   # Modulo           OUTPUT: 1

In this example, you can see that 5 % 2 will return 1. Using the more familiar division (/), 5 / 2 will return a float because 5 can't be divided by 2 without a remainder. Instead of getting a float as a result, the result of using the modulo operator gives you what remains when you take every possible number 2 out of 5.

2 fits twice in 5, which comes up to 4. What remains as a difference between the original number 5 and 4 is called the remainder, which is 1 in this case. This remainder is what the modulo operator returns.

There is another closely related concept to the modulo, which is called floor division and handles the other side of this operation.

Floor Division

Floor division performs a division, but instead of creating a float with the remainder in the fraction, it discards the remainder:

5 // 2  # Floor division   OUTPUT: 2

How often the divisor fits into the dividend is the result of your floor division. 2 fits in 5 two times, so the floor division returns 2.

The default behavior of the more familiar division is to implicitly change the data type of the value to a floating-point number if the division cannot be done without fractions.

Floor division avoids changing the data type. It performs a division to the last possible full number and discards the remainder.

Exponent

The third operator you haven't used before is the exponent. You might be familiar with exponentiation from arithmetics, but you probably haven't seen it written with the double-star symbol (**).

You use the exponent to set a value to the power of another value, for example:

2 ** 2  # 4
2 ** 3  # 8

If you write an exponential calculation, like the ones shown above, then make sure that you keep the two asterisks stuck together. The exponent operator is a single operator that consists of two asterisks (**). If you accidentally put a space in between these two symbols, Python will be confused and let you know by displaying a SyntaxError: invalid syntax.

Playground: Arithmetic Operator Practice

Revisit the section on numbers and math and apply some of the additional arithmetic operators you learned about in this lesson on integers and floating point numbers.

print(5 * 2)   # 10 
print(5 % 2)   # Modulo           OUTPUT: 1
print(5 / 2)   # Normal division  OUTPUT: 2.5
print(5 // 2)  # Floor division   OUTPUT: 2
print(2 ** 2)  # 4
print(2 ** 3)  # 8

Since the arithmetic operators gave you a chance to revisit the int and float data types, it's only fair to also give the str data type a chance to make a glorious comeback.

Summary: Python Arithmetic Operators

  • Arithmetic operators allow you to perform mathematical operations in Python
  • Most of the operators are commonly used in everyday life
  • The modulo operator returns the remainder after dividing two numbers
  • Floor division removes the remainder after division and provides only the whole integer value
  • The exponent value sets the power to a number or sets the number of times you multiply a number by itself

List of Python Arithmetic Operators

  • Addition is +
  • Subtraction is -
  • Multiplication is *
  • Division is /
  • Modulo is %
  • Floor division is //
  • Exponent is **