Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Divmod() Function in Python?
3.
divmod() Syntax
4.
Divmod() Parameter Values
5.
Divmod() Return value
5.1.
Example
5.1.1.
Input
5.1.2.
Output
6.
Usage of Divmod() Function
6.1.
Example 1: Python divmod() with Integer
6.1.1.
Implementation
6.2.
Python
6.2.1.
Output
6.3.
Example 2: Python divmod() with Floating Point numbers
6.3.1.
Implementation
6.4.
Python
6.4.1.
Output
6.5.
Example 3: Python divmod() with Non - Numeric
6.5.1.
Implementation
6.6.
Python
6.6.1.
Output
7.
What are the Applications of Divmod() Function
7.1.
Concept
7.1.1.
Example
7.2.
Python
7.2.1.
Output
8.
Exceptions of Python divmod() Function
9.
Frequently Asked Questions
9.1.
What is Python?
9.2.
How to calculate divmod in Python?
9.3.
What is the diff between == and === in Python?
9.4.
What is the difference between divmod and mod in Python?
9.5.
Is Divmod in Python a built-in function?
9.6.
What is the function of div and mod in Python?
9.7.
Is divmod faster in Python?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

Divmod() Function in Python

Introduction

The programming language Python is high-level and versatile. It is a universal language used for various functions across the programming field. Have you ever come across the divmod in Python? Did you ace it, or did you face a problem understanding it? Whatever the case is, we will help you understand the divmod in Python better.

divmod in python

Recommended topics, Floor Division in Python, and Convert String to List Python

What is Divmod() Function in Python?

The divmod in Python is a part of Python's standard library. It accepts two integers as inputs and delivers the quotient and remainder of their division as a tuple(quotient and remainder). Divmod in Python is helpful in many mathematical applications, such as determining whether or not a number is divisible and whether or not it is prime.

Also see, Merge Sort Python

divmod() Syntax

The syntax of divmod in Python is 

divmod(p,q)
Where p is the numerator, q is the denominator

Divmod() Parameter Values

It takes two parameter values. Let us take an example to understand divmod() parameter values. 

Suppose x=8, y=2; here, the parameters are x and y. Here x is the numerator, and y is the denominator. Here, x can be an integer or floating value. And y can be an integer or a floating value. Both the x%y and x//y are calculated by the divmod() function, and both values are returned. Now let us solve this example.

divmod(x,y)= divmod(8,2) 

The output here will be (4,0). As 8 is divisible by 2. The remainder will be 0, and the quotient will be 4. And both these values are returned by the function. 

Divmod() Return value

We are already aware of the divmod() function. This function takes two parameters, x, and y. Both the x%y (i.e. quotient) and x//y (i.e. remainder) are calculated by the divmod() function, and both values are returned. So, the values returned by the function are quotient and remainder. Now let us see an example to understand the returning value.

Suppose x= 7, y=2

divmod(x,y)=  divmod(7,2)

The value returned by this example will be (3,1). As 7 is not divisible by 2. So, the quotient is 3, and the remainder is 1. Both values are returned in the output. 

Example

Let us look into an example to understand it better.

Input

print("Enter x: ")
x = int(input())

print("Enter y: ")
y = int(input())

out_div = divmod(x,y)
print("Quotient: ",out_div[0])
print("Remainder: ",out_div[1])

Output

Int Output div_mod

Usage of Divmod() Function

Two parameters—x and y—are required by the divmod() method, where x serves as the numerator and y serves as the denominator. Divmod in Python calculates both x // y and x% y, and both values are returned.

The // operator calculates the division in divmod in Python method. As a result, divmod in Python returns the entirety of the quotient. Let's examine how the // operator behaves when dealing with floating point and integer values.

Example 1: Python divmod() with Integer

We know that 14//4 will yield 3. 

For example, 14 divided by 4 yields a quotient of 3.5, and the whole part of the quotient is 3. Similarly, 5//2 will give us 2 as quotient.

Implementation

  • Python

Python

print("Enter x: ")
x = int(input())

print("Enter y: ")
y = int(input())

out_div = divmod(x,y)
print("Quotient: ",out_div[0])
print("Remainder: ",out_div[1])

Output

Int Output div_mod

Example 2: Python divmod() with Floating Point numbers

The output of 12.5//5 is 2.0. 12.5 divided by 5 yields a quotient of 2.5, and the whole part of the quotient is 2.0.

Implementation

  • Python

Python

print("Enter x: ")
x = float(input())

print("Enter y: ")
y = float(input())

out_div = divmod(x,y)
print("Quotient: ",out_div[0])
print("Remainder: ",out_div[1])

Output

Float output divmod

Here, 2.0 is the quotient, and the remainder is 2.5.

Example 3: Python divmod() with Non - Numeric

Let us pass some non-numeric parameters in the divmod() function and see what does the output comes. Let us pass string values as numerator and denominator. 

Implementation

  • Python

Python

print("Enter x: ")
x = str(input())

print("Enter y: ")
y = str(input())

out_div = divmod(x,y)
print("Quotient: ",out_div[0])
print("Remainder: ",out_div[1])

Output

non-numeric output divmod

You can compile it with online python compiler.

What are the Applications of Divmod() Function

We can use divmod in Python to check whether a given number is prime. Similarly, we can also use divmod in Python to calculate the sum of digits of a number, reverse a number, divisibility by 0, divisibility check, etc.

Concept

When we begin dividing a number by each number, starting with itself and going to 1, we can use divmod in Python to keep track of the reminders that it creates. Because no integer other than itself divides a prime number correctly, and the count of zero remainders is just one. The integer is not prime if the number of zero remainders is more than 1.

Example

  • Python

Python

print("Enter a number")
num = int(input())
p = num

# Counting the number of remainders with a value of zero
count = 0
while p != 0:
q, r = divmod(num, p)
p -= 1
if r == 0:
count += 1

if count > 2:
print(num, 'is not Prime')
else:
print(num, 'is Prime')

Output

Prime or not

Must Read  C Program to Reverse a Number

Exceptions of Python divmod() Function

Exceptions in Python typically refer to situations where something goes wrong during the execution of code. However, the divmod() function itself doesn't raise any exceptions under normal circumstances. It's straightforward and works with any numeric data types like integers, floats, etc.

Here are a few situations where exceptions might occur, but they're not directly related to the divmod() function itself:

1. ZeroDivisionError: If the second argument (divisor) is zero, Python raises a ZeroDivisionError because division by zero is not allowed.

result = divmod(10, 0)  # Raises ZeroDivisionError

 

2. TypeError: If either of the arguments is not a numeric type, a TypeError will be raised.

result = divmod("10", 3)  # Raises TypeError

However, these exceptions are not specific to the divmod() function but rather general exceptions that can occur in various situations where arithmetic operations are involved.

Frequently Asked Questions

What is Python?

Python is a well-liked general-purpose, interactive, object-oriented, and high-level programming language. Python is dynamically typed and garbage collected.

How to calculate divmod in Python?

It's a built-in function. Directly call it. It takes two parameters. Divmod(x,y) yields two results, i.e. both the x%y and x//y are calculated by the divmod() function, and both values are returned. So, the values returned by the function is quotient and remainder. Divmod(5,2) will return (2,1).

What is the diff between == and === in Python?

While == is used to compare two variables, it ignores the datatype of the variables. In contrast, === checks to determine whether two operands are equal in both datatype and value.

What is the difference between divmod and mod in Python?

Divmod in python calculates the quotient and remainder of the values provided in the argument. Both the x%y and x//y are calculated by the divmod() function, and both values are returned. While Mod (modulus) returns the amount left over after dividing two numbers.

Is Divmod in Python a built-in function?

Divmod in Python is used to determine the quotient, an integer value before the decimal point following division.

What is the function of div and mod in Python?

Div and mod in Python return the quotient and remainder respectively when dividing two numbers using integer division and modulus operators.

Is divmod faster in Python?

Yes, divmod is generally faster than performing separate division and modulus operations in Python due to optimized implementation.

Conclusion

In this article, you gained some insight into divmod in Python. You also learned about the applications of the divmod function. We hope that you could understand the function easily.

Recommended readings:

Check out our Data Structures and Algorithms-guided path to learn Data Structures and Algorithms. Also, check out some of the Guided PathsContests, and Interview Experiences to gain an edge only on Coding Ninjas Studio.

Cheers!

Live masterclass