Monday, October 26, 2020

Python Modulo in Practice: How to Use the % Operator

Python supports a wide range of arithmetic operators that you can use when working with numbers in your code. One of these operators is the modulo operator (%), which returns the remainder of dividing two numbers.

In this tutorial, you’ll learn:

  • How modulo works in mathematics
  • How to use the Python modulo operator with different numeric types
  • How Python calculates the results of a modulo operation
  • How to override .__mod__() in your classes to use them with the modulo operator
  • How to use the Python modulo operator to solve real-world problems

The Python modulo operator can sometimes be overlooked. But having a good understanding of this operator will give you an invaluable tool in your Python tool belt.

Modulo in Mathematics#

The term modulo comes from a branch of mathematics called modular arithmetic. Modular arithmetic deals with integer arithmetic on a circular number line that has a fixed set of numbers. All arithmetic operations performed on this number line will wrap around when they reach a certain number called the modulus.

A classic example of modulo in modular arithmetic is the twelve-hour clock. A twelve-hour clock has a fixed set of values, from 1 to 12. When counting on a twelve-hour clock, you count up to the modulus 12 and then wrap back to 1. A twelve-hour clock can be classified as “modulo 12,” sometimes shortened to “mod 12.”

The modulo operator is used when you want to compare a number with the modulus and get the equivalent number constrained to the range of the modulus.

For example, say you want to determine what time it would be nine hours after 8:00 a.m. On a twelve-hour clock, you can’t simply add 9 to 8 because you would get 17. You need to take the result, 17, and use mod to get its equivalent value in a twelve-hour context:

8 o'clock + 9 = 17 o'clock
17 mod 12 = 5

17 mod 12 returns 5. This means that nine hours past 8:00 a.m. is 5:00 p.m. You determined this by taking the number 17 and applying it to a mod 12 context.

Now, if you think about it, 17 and 5 are equivalent in a mod 12 context. If you were to look at the hour hand at 5:00 and 17:00, it would be in the same position. Modular arithmetic has an equation to describe this relationship:

a ≡ b (mod n)

This equation reads “a and b are congruent modulo n.” This means that a and b are equivalent in mod n as they have the same remainder when divided by n. In the above equation, n is the modulus for both a and b. Using the values 17 and 5 from before, the equation would look like this:

17 ≡ 5 (mod 12)

This reads “17 and 5 are congruent modulo 12.” 17 and 5 have the same remainder, 5, when divided by 12. So in mod 12, the numbers 17 and 5 are equivalent.

You can confirm this using division:

17 / 12 = 1 R 5
5 / 12 = 0 R 5

Both of the operations have the same remainder, 5, so they’re equivalent modulo 12.

Now, this may seem like a lot of math for a Python operator, but having this knowledge will prepare you to use the modulo operator in the examples later in this tutorial. In the next section, you’ll look at the basics of using the Python modulo operator with the numeric types int and float.

Python Modulo Operator Basics#

The modulo operator, like the other arithmetic operators, can be used with the numeric types int and float. As you’ll see later on, it can also be used with other types like math.fmod(), decimal.Decimal, and your own classes.

Modulo Operator With int#

Most of the time you’ll use the modulo operator with integers. The modulo operator, when used with two positive integers, will return the remainder of standard Euclidean division:

Read the full article at https://realpython.com/python-modulo-operator/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...