Python Modulo Operator
Whether you are just getting started in the world of development or have tons of experience, you familiar with the concept of artithmetic operations in programming languages.
Each programming has a set of different symbols to represent the supported artithmetic operations. These include an asterisk (*) for multiplication, a dash for substraction, a plus symbol for addition and a forward slash for division.
However, there is another common arithmetic operations used by developers known as the modulo operation. This operation allows us to calculate the remainder from dividing two numerical values. The symbol for representing a modulo operation in Python is the %
symbol.
In this tutorial, we are going to delve into the heart of the modulo operator to learn of its workings, the supported syntax and usage in various environments.
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 is used to fetch the value of a division sum.
In Python, we represent the modulo operator using the percentage syntax and follows 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 assing 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 values, 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.
Keep in mind that this will apply even if only one of the operand 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 task 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 with 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 using it to determine whether a given value is odd or even.