- 123
The modulo operator % in Python is used to find the remainder of a division operation. It is an arithmetic operator that can be used with both integers and floats1.
Example
a = 13b = 5c = a % bprint(a, "mod", b, "=", c) # Output: 13 mod 5 = 3Usage with Different Data Types
Integers
result = 10 % 3print(result) # Output: 1Floats
result = 10.5 % 3print(result) # Output: 1.5Handling Negative Numbers
The result of the modulo operation with negative numbers is determined by the sign of the divisor2.
result = -7 % 3print(result) # Output: 2Common Use Cases
Checking Even or Odd Numbers
number = 15if number % 2 == 0:print("Even")else:print("Odd")Implementing Time Calculations
total_minutes = 130hours = total_minutes // 60minutes = total_minutes % 60print(f"{hours} hours and {minutes} minutes") # Output: 2 hours and 10 minutesNote: The only exception you get with the Python modulo operation is ZeroDivisionError, which occurs if the divisor is zero2.
Learn more✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links. Modulo operator (%) in Python - GeeksforGeeks
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. Output: See more
Python Modulo in Practice: How to Use the % Operator
Oct 26, 2020 · Learn how to use the modulo operator (%), which returns the remainder of dividing two numbers, in Python. See examples of modulo with …
- Estimated Reading Time: 8 mins
What is the result of % (modulo operator / percent …
Jun 14, 2024 · Learn how to use the % (modulo) operator in Python to get the remainder of a division operation. See examples, explanations, and differences with other languages and string formatting.
Python Modulo Operator (%) - Python Tutorial
Learn how to use the modulo operator (%) in Python to calculate the remainder of division. See how to check if a number is even or odd, and how to convert between units using the modulo …
- Estimated Reading Time: 1 min
- People also ask
Modulo (%) in Python: A Complete Guide (10
Learn how to use the modulo operator (%), also known as the percentage operator, in Python. Find out how to calculate the remainder of division, check if a number is odd or even, and handle negative numbers with modulo.
Python Modulo - Using the % Operator - Python Geeks
Learn how to use the modulo operator (%), which calculates the remainder of a division operation, in Python. See examples of modulo in looping, arithmetic, and indexing.
Python Modulo Operator: Understanding % in Python
May 27, 2022 · Learn how the modulo operator (%) returns the remainder of dividing two numbers in Python. See how to use it with different numeric data types, negative values, and practical examples.
How To Use The % Operator: A Set of Python Modulo …
Learn how to use the % operator in Python to calculate the remainder of division. Discover the mathematical significance of modulo arithmetic and how it works with positive and negative operands.
Python Modulus: Unveiling the Remainder Operator - CodeRivers
Jan 24, 2025 · In the realm of Python programming, the modulus operator (%) is a powerful and versatile tool. It allows developers to find the remainder when one number is divided by …
Python Modulo operator: How Modulo (%) works in Python - A …
Jan 6, 2019 · In Python and generally speaking, the modulo (or modulus) refers to the remainder from the division of the first argument to the second. The symbol used to get the modulo is …
Related searches for Modulus in Python
- Some results have been removed