Python built-in function ~ divmod ~ Let's get the quotient and remainder of division at the same time

Introduction

In this article, I'll cover Python's "divmod function". The "divmod function" is a convenient way to get the quotient and remainder when dividing. Let's deepen our knowledge of Python by becoming able to use the "divmod" function.

Quotient and remainder in normal division

In normal division in Python, use "/" to find the quotient and "%" to find the remainder.

#quotient
12 / 4
>>>3.0
#quotient
12 / 5
>>>2.4
#Truncate the decimal point
12 // 5
>>>2
#remainder
12 % 4
>>> 0
#remainder
12 % 5
>>> 2

divmod function

Now, let's use the "divmod function" immediately. Let's check the code first.

quotient, remainder = divmod(12, 5)
print(quotient)   #2
print(remainder)  #2

As you can see, we have two variables, the first variable contains the quotient and the second variable contains the remainder. And the point is that the first variable contains the "quotient with the decimal point truncated". In other words, it is the same as when using "//".

Next, let's check what happens when we receive it with one variable.

tuple = divmod(12, 5)
print(tuple)   #(2, 2)

You can receive it as a tuple like this. When you want to take it out

print(tuple[0], tuple[1])
#2 2

Then you can take it out.

Comparison

Finally, let's compare the normal quotient and remainder when using the "divmod function".

#usually
quotient = 12 / 5
remainder = 12 % 5
print(quotient)   #2.4
print(remainder)  #2
#divmod function
quotient, remainder = divmod(12 / 5)
print(quotient)   #2
print(remainder)  #2

It's only one line, but you can see that it's shortened. If even one line gets shorter, there is no reason not to use it, so let's use it positively.

Finally

In this article, I covered Python's "divmod function". We will continue to write articles about useful modules and built-in functions, so please take a look if you like.

Recommended Posts

Python built-in function ~ divmod ~ Let's get the quotient and remainder of division at the same time
python memo: enumerate () -get index and element of list at the same time and turn for statement
[python] Get quotient and remainder
Get a datetime instance at any time of the day in Python
Get the caller of a function in Python
At the time of python update on ubuntu
Visualize data and understand correlation at the same time
Try to get the function list of Python> os package
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ②
wxPython: Draw animation and graph drawing at the same time
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ①
Get the current date and time in Python, considering the time difference
[Python 3.8 ~] Rewrite arrays etc. at the same time as definition [tips]
[Python] How to get the first and last days of the month
A function that measures the processing time of a method in python
[Python3] Define a decorator to measure the execution time of a function
Get and set the value of the dropdown menu using Python and Selenium
Browse .loc and .iloc at the same time in pandas DataFrame
Get the title and delivery date of Yahoo! News in Python
Count the number of times two values appear in a Python 3 iterator type element at the same time
How to get the date and time difference in seconds with python
[Python] Case where the execution time differs between the built-in list and deque
Get and convert the current time in the system local timezone with python
Get and estimate the shape of the head using Dlib and OpenCV with python
Python: I want to measure the processing time of a function neatly
Plot multiple maps and data at the same time with Python's matplotlib
[Python] How to open two or more files at the same time
[Python3] Rewrite the code object of the function
Call the python debugger at any time
Python Basic Course (at the end of 15)
[Python] Get the character code of the file
Python open and io.open are the same
Let's play with Python Receive and save / display the text of the input form
I want to make a music player and file music at the same time
Make sure to align the pre-processing at the time of forecast model creation and forecast
Type conversion of multiple columns of pandas DataFrame with astype at the same time
Turn multiple lists with a for statement at the same time in Python
I want to get the file name, line number, and function name in Python 3.4
Get the stock price of a Japanese company with Python and make a graph
Get the last element of the array by splitting the string in Python and PHP
How to get a list of files in the same directory with python
Get the contents of git diff from python
Summary of the differences between PHP and Python
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
The answer of "1/2" is different between python2 and 3
Specifying the range of ruby and python arrays
Compare the speed of Python append and map
Send Gmail at the end of the process [Python]
[Python] Get the last updated date of the website
Loop variables at the same time in the template
Remove specific strings at the end of python
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
[Python] Get the day of the week (English & Japanese)
Get the update date of the Python memo file.
[Machine learning] "Abnormality detection and change detection" Let's draw the figure of Chapter 1 in Python.
In Python3.8 and later, the inverse mod can be calculated with the built-in function pow.
Check the processing time and the number of calls for each process in python (cProfile)