Python Modulo Operator

Python Modulo Operator

We denote Python Modulo operator as %. We can also call it as Modulus Operator. Python Modulo is a mathematical operation and we can use it for calculating remainder. Basically, this module is used to get the remainder of a division problem. This modulo operator is a type of arithmetic operator. This module takes two values as input and the modulo operand and with the help of the operator it provides the desired output accordingly. The main rule which user should follow in the case of the modulo operator is that he divides the first number by the second number and then return the remainder in the form of output.

Syntax

The syntax of the Modulo operator is as follows:-

c = a % b, where a and b are the two inputs and a is divided by b and the remainder is stored as the output value of c.

There can also come a case when the value of remainder can come zero when we divide the first number by the second one. Just make sure that the user uses both the numbers as input must be of number data type. That too is in the format of either integer(int) or decimal(float).

We can also use Python Modulo for various forms of operations like math.fmod(), decimal. In addition, the user can use decimal in some operations of the class.

Modulo Operator with Integer(int) number

The user generally uses Integer numbers for calculation purposes in most cases. Let’s understand the use of the Modulo Operator and find how this operator is executed in solving mathematical problems:-

a = 15
b = 7
c = a%b
print(c)


Output:

1

It is clearly visible from the above example that a and b have the values as input in integer form. The logic can execute in c that will generate an output of our code and delivers the result.

Python Modulo Negative Number

The modulus of a negative number is by ignoring the minus sign. We denote the modulus of a number writing vertical lines around the number. Also that the modulus of a negative number is by multiplying it by βˆ’1 since, for example, βˆ’(βˆ’1) = 1.

Python mod()

The Python modulo operator will calculate the remainder of dividing two values. We represent this operator by the percentage sign (%). The percentage sign will represent the modulo operator. It returns the remainder of dividing two numbers.

Modulo Operator with Decimal(Float) number

We can use modulo for the same operation for decimal numbers just similar to integer data type. Let’s understand this also with the help of an example:

x = 27.2
y = 8.5
z = x % y
print(z)


Output

1.6999999999999993

So when we take the input in float data type then we get the resultant also in the form of float data type.

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!