Divmod Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The divmod() function, which is part of Python's standard library, accepts two numeric inputs and returns the quotient and remainder of their division as a tuple. divmod Python is helpful in many mathematical applications, such as determining whether a number is divisible or not and checking whether a number is prime or not.

Python divmod() Function

The divmod() function is part of Python's standard library. The divmod() function accepts two numeric inputs and returns the quotient and remainder of their division as a tuple.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Syntax of Python divmod() Function

Parameters of Python divmod() Function

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

num1

This is the numerator. It can either be an integer or a floating point number.

num2

This is the denominator. It can either be an integer or a floating point number.

Return Value of Python divmod() Function

The divmod Python returns a tuple (quotient, remainder) containing the division's quotient and remainder.

Exceptions of Python divmod() Function

  • If non-numeric arguments such as strings, objects, etc. are passed to the divmod Python, the divmod() function will return a TypeError.
  • If 0 is passed as the first argument, the divmod Python returns (0,0)
  • If 0 is passed as the second argument, the divmod Python returns a Zero Division Error.

How Does the divmod() Function in Python Work?

The divmod Python method accepts two parameters, num1, and num2, where num1 is the numerator and num2 is the denominator. The divmod Python method computes and returns both the quotient as num1//num2 and the remainder as num1%num2 as a tuple.
Note that the divmod Python method computes the division by using // operator. Hence the whole part of the quotient is returned by the divmod method. Let's see how // operator works for integers and floating point numbers.

If num1 and num2 are Integers

7//2 will return 3. As on dividing 7 by 2, the quotient is 3.5 and the whole part of the quotient is 3.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

If num1 or num2 is a Float

7.5//3 will return 2.0. As on dividing 7.5 by 3, the quotient is 2.5 and the whole part of the quotient is 2.0.

Uses of divmod() Function?

  • We can use divmod() method to check whether a number is prime or not. We can iterate over numbers in the range [1, num] and can count the numbers where the remainder is 0. If a number is prime, then this count should be 2 (one for number 1 and one for the number itself). If the count > 2 we can say that the number is Non-Prime, else the number is Prime.
  • We can also check whether a number is divisible or not. If the remainder is 0, then the number is divisible, else the number is not divisible.

Python divmod() Function Examples

Let's take 10 as the numerator and 2 as the denominator. The quotient should be 5 and the remainder should be 0.

Output:

As you can see in the output, the return value is a tuple having 5 and 0. Where 5 is the quotient and 0 is the remainder.

Python divmod() with Integer Arguments

Let's have another example with the numerator as 11 and the denominator as 3.

Output:

Now let's try by passing 0 as the denominator.

Output:

As you can see in the output, the divmod Python returns a ZeroDivisionError.

Python divmod() with Float Arguments

Output:

In all the above cases, one or both the arguments of divmod Python is a float, hence the divmod() method returns the quotient and the remainder as decimal numbers.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Python divmod() with Non-Numeric Arguments

Now let's try passing non-numeric arguments to divmod() method. We will pass a string as numerator and denominator.

Output:

As you can see in the output, it states that unsupported arguments are passed to the divmod() method.

Conclusion

  • The divmod() function accepts two numeric inputs and returns the quotient and remainder of their division as a tuple.
  • If non-numeric arguments such as strings, objects, etc. are passed to the divmod Python, the divmod() function will return a TypeError.
  • If 0 is passed as the first argument, the divmod Python returns (0,0)
  • If 0 is passed as the second argument, the divmod Python returns a Zero Division Error.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more