Python Modulo Operator

This tutorial will delve into the modulo operator's heart to learn its workings, the supported syntax, and its usage in various environments.

Python Modulo Operator

Whether you are just getting started in the development world or have tons of experience, you are familiar with arithmetic operations in programming languages.

Each programming language has a set of different symbols to represent the supported arithmetic operations. These include an asterisk (*) for multiplication, a dash for subtraction, a plus symbol for addition, and a forward slash for division.

However, another typical arithmetic operation used by developers is the modulo operation. This operation allows us to calculate the remainder by dividing two numerical values. The symbol for representing a modulo operation in Python is the % symbol.

Python Modulo Operator.

Modulo is a typical mathematical operator that allows us to get the remainder of dividing two values. In mathematical terms, a modulo operator fetches the value of a division sum.

In Python, we represent the modulo operator using the percentage syntax and follow the syntax of num1 % num2 to get the remainder of dividing these two values.

An example usage of the modulo operator is as shown:

>>> var = 100 % 3

As you can see from the example above, we place the modulo operator between the two operands. We can then assign the value or the remainder of the operation to a Python variable.

We can use the Python print() function to output the value:

>>> print(var)
1

In this case, we can see that the remainder of dividing 100 by 3 is 1.

If there is no remainder after dividing the two operands., the modulo operator will return a 0. An example is as shown:

>>> var = 100 % 10
>>> print(var)
0

In this case, since dividing 100 by 10 does not have a remainder, the modulo operator returns a value of 0.

Modulo Operator with Floating-Point Values

We can also use the modulo operator with floating-point values. If the input is integer, the modulo operator will return an integer value. However, if the operands are floats, the operator will return a floating-point value.

Let us try and find the remainder of dividing two floating point values as shown:

>>> var = 100.34 % 34.3
>>> print(var)
31.74000000000001

As you can see in the resulting values above, the operator will return a float.

Remember that this will apply even if only one of the operands is a float. For example:

>>> var = 100 % 34.3
>>> print(var)
31.400000000000006

As you can see, the modulo operator will also return a float since one of the operands is a floating-point value.

Using Modulo to Find Odd and Even Numbers

One of the most common tasks of the modulo operator is using it to find odd and even numbers.

From basic mathematics, you will remember that a number is even if its division with two does not contain a remainder. We can use this basic conclusion to create a block that checks the value from a modulo operator. If the value of dividing a number by two is zero, then that value is an even number otherwise, the value is an odd number.

>>> num = 112
>>> if num % 2 == 0:
...     print("Even!")
... else:
...     print("Odd!")
... 
Even!

In this case, we can see that the program returns the string Even! since dividing 112 by 2 has no remainder.

Conclusion

In this tutorial, you learned how to use the modulo operator in Python to determine the remainder of a division sum. We also covered the workings of the operator with integer and floating-point values and used it to determine whether a given value is odd or even.

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.