Skip to main content

How to find the remainder of a division of two numbers in Python

How to find the remainder of a division of two numbers in Python.

Here's a step-by-step tutorial on how to find the remainder of a division of two numbers in Python:

  1. First, you need to understand the concept of division and remainder. When one number (dividend) is divided by another number (divisor), the remainder is the amount left over after dividing the dividend by the divisor.

  2. Open your Python IDE or text editor and create a new Python file.

  3. Declare two variables to hold the dividend and divisor values. For example, let's say we want to find the remainder of dividing 10 by 3:

dividend = 10
divisor = 3
  1. To find the remainder, you can use the modulus operator %. The modulus operator returns the remainder of the division operation.

  2. Use the modulus operator in Python to calculate the remainder of the division. Assign the result to a variable. For example:

remainder = dividend % divisor
  1. Print the remainder to see the result:
print("The remainder is:", remainder)
  1. Save the file and run it. You should see the output:
The remainder is: 1

Congratulations! You have successfully found the remainder of dividing two numbers in Python.

Here's the complete code:

dividend = 10
divisor = 3

remainder = dividend % divisor

print("The remainder is:", remainder)

Alternative approach: You can also use the divmod() function in Python to get both the quotient and remainder of a division operation in a single line. The divmod() function takes two arguments (dividend, divisor) and returns a tuple containing both values.

Here's an example using divmod():

dividend = 10
divisor = 3

quotient, remainder = divmod(dividend, divisor)

print("The quotient is:", quotient)
print("The remainder is:", remainder)

This will give the same output as before:

The quotient is: 3
The remainder is: 1

That's it! Now you know how to find the remainder of a division of two numbers in Python using the modulus operator or the divmod() function.