Mod Divmod in Python Explained with Simple Examples | HackerRank Problem
Small built-in functions in Python often save a great deal of work when you first start learning the language.
The function divmod() is one example. At first glance, it doesn’t seem particularly noteworthy, but after using it, you’ll see how neat it makes your code.
The HackerRank Mod Divmod problem is briefly and easily explained in this post.
Summary of the Issue
A and B are the two numbers you will receive.
Printing three results is your task:
a // b → integer division
a % b → remainder
divmod(a, b) → both together
You can see how Python manages division and remainders in the background by solving this problem.
Example:
Input
177
10
Output
17
7
(17, 7)
This is what goes on:
When 177 is divided by 10, the quotient is 17.
The remainder, or leftover, is 7.
In a single operation, divmod(177, 10) yields (17, 7).
Solution for Code
a = int(input())
b = int(input())
print(a // b)
print(a % b)
print(divmod(a, b))

How It Operates
Receiving Input
a = int(input())
b = int(input())
You enter two numbers. The int() function converts the text input into integers.
Integer Division
print(a // b)
The // operator divides and drops everything after the decimal.
For example, 177 // 10 gives 17.
If you used /, the result would be 17.7.
Modulo Operator
print(a % b)
The % sign shows the remainder.
In this case, 177 % 10 is 7.
You will often use it to see if a number is even or odd or to find patterns in loops.
divmod() Function
print(divmod(a, b))
divmod() gives both results in one tuple.
divmod(177, 10) → (17, 7)
You can also take it apart:
q, r = divmod(177, 10)
print("Quotient:", q)
print("Remainder:", r)
Output:
Quotient: 17
Remainder: 7
The Reasons It’s Practical
This appears simple, but it teaches a lot: Mod Divmod in Python
Python teaches you how division operates.
You see the relationship between % and //.
You learn how to use tuples and unpack.
This is a real-world example. Assume you wish to change seconds to minutes and seconds:
m, s = divmod(125, 60)
print(m, "minutes", s, "seconds")
Output:
2 minutes 5 seconds
Small functions like this come in handy more often than you’d expect.
Final Output Recap
Input
177
10
Output
17 # Integer division
7 # Remainder
(17, 7) # Tuple using divmod()
Important Points
// provides the result in whole numbers.
The remainder is given by %.
Both results are combined by divmod().
For many actual programs, these are compact yet effective tools.
Wrap up
Although the Mod Divmod in Python challenge is brief, it establishes a solid foundation.
It demonstrates how Python manages math in a clear and effective manner.
You can use one line to find the quotient and remainder rather than two, and your code will remain clear and easy to read.
You can write better programs more quickly by becoming proficient with such small features.
They may seem insignificant now, but as a Python developer, they influence your thought process.
Learn more about Python’s official documentation on Python.org.
Visit YouTube to View the Complete Solution
Check out my YouTube tutorial, where I run and walk through this exact code in real time, if you want a step-by-step video explanation.
Related Post:
- Set .intersection() Operation in Python – Complete HackerRank Solution Explained Step-by-Step
- HackerRank Collections.deque() Solution in Python
- HackerRank Python Solution – Using itertools combinations_with_replacement | In 5 Easy Steps
In Conclusion:
The application of data science to the financial industry represents a fundamental change in the way the sector functions as well as a technological advancement. Applications of data science are changing conventional methods and creating new opportunities, from personalized financial services to predictive analytics. The collaboration between finance and data science will develop further as we all go forward, resulting in a more resilient, effective, and strong financial ecosystem. Those who make the most of data science will lead the way in financial innovations and success in this data-driven era.
Are you curious about what else data science can do?
Attend our free Master of Data Science with Power BI workshop if you want to learn more about data science or want to progress your career in the field. You will discover how the data science field operates and why employers are willing to pay high salaries in this field.
By learning every tool and technology from the ground up, you will be competently qualified for any data science role.
Register for this workshop on ConsoleFlare, and we’ll give you a call.
Why Console Flare, I wonder?
ConsoleFlare was recently listed as one of the Top 10 Most Promising Data Science Training Institutions of 2023.
You can study Data Science in Hindi using Console Flare, just like you do on a daily basis.
Their curriculum structure reflects Console Flare’s belief in “what to learn and what not to learn.” Only the knowledge required for data science was taken into consideration when designing their program.

