Lesser-Known Arithmetic Operators in Python

Go beyond addition, subtraction, multiplication, and division

Lesser-Known Arithmetic Operators in Python
Photo by Helloquence on Unsplash

As a non-math person who got into programming on my own, I’ve found gaps in my general knowledge that have little to do with aptitude and more to do with exposure.

Here are three handy arithmetic (math) operators that you may be unaware of, but will be helpful in your programming journey.


Exponentiation Operator

The exponentiation operator ** replaces the traditional caret ^ from scientific calculators of old.

Instead of multiplying a variable together three times to cube it, or use an external library like Math.pow() in JavaScript, simply use the double asterisk to raise the left operand to the power of the right.x = 5
x_cubed = x ** 3
x_to_the_eighth = x ** 8


Integer Division Operator

Have you ever needed to truncate a value by removing its decimal places? JavaScript has the Math.floor() method to round down; however, this is unnecessary in Python.

The double forward slash in Python is known as the integer division operator. Essentially, it will divide the left by the right, and only keep the whole number component.x = 8
y = 3
z = x // yprint(z) # 2


Modulus Operator

If the integer division operator gives us the whole, then we also need a way to find the part.

Modulus is an operation that is overlooked in math class, but can be very helpful in programming and data science. When using the modulus operator %, the remainder of the division is returned.x = 10
y = 4
z = x % yprint(z) # 2

If you’re having trouble thinking of ways of using the modulus operator, I most commonly use it to check for an odd or even integer.x = 11if x % 2:
  print("odd number")
else:
  print("even number")

By returning the remainder from a division of 2, the remainder will either be 0 (even number) or 1 (odd number).

Subscribe to Dreams of Fortunes and Cookies

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe