Modulo operator (%) in Python Last Updated : 17 Feb, 2025 Comments Improve Suggest changes Like Article Like Report Modulo operator (%) in Python gives the remainder when one number is divided by another. Python allows both integers and floats as operands, unlike some other languages. It follows the Euclidean division rule, meaning the remainder always has the same sign as the divisor. It is used in finding even/odd numbers, cyclic patterns, and leap year calculations. The divmod(a, b) function can also be used to Example of Modulo operator (%):10 % 4 gives 2 because 10 divided by 4 leaves a remainder of 2. Python rem=10%4 print(rem) Output2 Table of ContentSyntax of Modulo operatorModulo Operator with IntegerModulo Operator with negative float number.Example using the Modulo OperatorZeroDivisionError in PythonSyntax of Modulo operatora % bHere, a is divided by b, and the remainder of that division is returned.Modulo Operator with IntegerStores the remainder obtained when dividing a by b, in c Python # inputs a = 13 b = 5 c = a % b print(a, "mod", b, "=", c, sep=" ") Output:13 mod 5 = 3Modulo Operator with negative float number.Stores the remainder obtained when dividing d by e, in f. For more examples, refer to How to Perform Modulo with Negative Values in Python. Python # inputs d = 15.0 e = -7.0 f = d % e print(d, "mod", e, "=", f, sep=" ") Output:15.0 mod -7.0 = -6.0Example using the Modulo OperatorSuppose, we want to calculate the remainder of every number from 1 to n when divided by a fixed number k. Python # function is defined for finding out # the remainder of every number from 1 to n def findRemainder(n, k): for i in range(1, n + 1): # rem will store the remainder # when i is divided by k. rem = i % k print(i, "mod", k, "=", rem, sep=" ") # Driver code if __name__ == "__main__": # inputs n = 5 k = 3 # function calling findRemainder(n, k) Output:1 mod 3 = 1 2 mod 3 = 2 3 mod 3 = 0 4 mod 3 = 1 5 mod 3 = 2ZeroDivisionError in PythonThe only Exception you get with the Python modulo operation is ZeroDivisionError. This happens if the divider operand of the modulo operator becomes zero. That means the right operand can’t be zero. Let’s see the following code to know about this Python exception. Python # inputs a = 14 b = 0 # exception handling try: print(a, 'mod', b, '=', a % b, sep=" ") except ZeroDivisionError as err: print('Cannot divide by zero!' + 'Change the value of the right operand.') Output:Cannot divide by zero! Change the value of the right operand. Comment More infoAdvertise with us E equbalzeeshan Follow Improve Article Tags : Python Python-Operators Practice Tags : pythonpython-operators Similar Reads Python Operators In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /, 6 min read Precedence and Associativity of Operators in Python In Python, operators have different levels of precedence, which determine the order in which they are evaluated. When multiple operators are present in an expression, the ones with higher precedence are evaluated first. In the case of operators with the same precedence, their associativity comes int 4 min read Python Arithmetic OperatorsPython Arithmetic Operators Python operators are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). OperatorDescriptionS 4 min read Difference between / vs. // operator in Python In Python, both / and // are used for division, but they behave quite differently. Let's dive into what they do and how they differ with simple examples./ Operator (True Division)The / operator performs true division.It always returns a floating-point number (even if the result is a whole number).It 2 min read Python - Star or Asterisk operator ( * ) The asterisk (*) operator in Python is a versatile tool used in various contexts. It is commonly used for multiplication, unpacking iterables, defining variable-length arguments in functions, and more.Uses of the asterisk ( * ) operator in PythonMultiplicationIn Multiplication, we multiply two numbe 3 min read What does the Double Star operator mean in Python? The ** (double star)operator in Python is used for exponentiation. It raises the number on the left to the power of the number on the right. For example:2 ** 3 returns 8 (since 2³ = 8)It is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python and is also known as Power Operator.Prec 2 min read Division Operators in Python Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. There are two types of division operators: Float divisionInteger division( Floor division)When an in 5 min read Modulo operator (%) in Python Modulo operator (%) in Python gives the remainder when one number is divided by another. Python allows both integers and floats as operands, unlike some other languages. It follows the Euclidean division rule, meaning the remainder always has the same sign as the divisor. It is used in finding even/ 4 min read Python Logical Operators Like