×

### Python divmod() function in Linux The divmod() function is used to divide and return the result as an integer and the remainder of the two entered numbers. What this function does is to take two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder. This is a common math operation in python to help solve math problems.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Python function queries.

In this context, we shall look into how to use the divmod() function in Python.

#### More about divmod() function and it's syntax

The divmod() function returns 2 values: integer and remainder.

Basically, divmod() returns:

`(q, r) - a pair of numbers (a tuple) consisting of quotient q and remainder r`

If x and y are integers, the return value from divmod() is same as (a // b, x % y).

If either x or y is a float, the result is (q, x%y). Here, q is the whole part of the quotient.

It's syntax is given below:

`divmod(x, y)`

It's Parameter Values is given below:

• x: dividend also known as a non-complex number (numerator).
• y: divisor also regarded as a non-complex number (denominator).

#### Examples of using Python divmod() function

1. If you have a fuctions such as:

`x = divmod(7, 2)print(x)`

The output will be:

`(3, 1)`

2. Take a look at another divmod() fuction:

`x = divmod(9, 4)y = divmod(7, 2)print("x = ", x)print("y = ", y)`

The output will be:

`x = (2, 1)y = (3, 1)`

3. Here is another one:

`divmod() combines input() functionprint("Enter x number:")x = int(input())print("Enter y number:")y = int(input())z = divmod(x, y)print("Result: ",z)`

The Output will be:

`Enter x number: 5Enter y number: 2Result: (2, 1)`

4. A complex divmod() fuction is given below:

`print('divmod(8, 3) = ', divmod(8, 3))print('divmod(3, 8) = ', divmod(3, 8))print('divmod(5, 5) = ', divmod(5, 5))# divmod() with Floatsprint('divmod(8.0, 3) = ', divmod(8.0, 3))print('divmod(3, 8.0) = ', divmod(3, 8.0))print('divmod(7.5, 2.5) = ', divmod(7.5, 2.5))print('divmod(2.6, 0.5) = ', divmod(2.6, 0.5))`

It's Output would be:

`divmod(8, 3) =  (2, 2)divmod(3, 8) =  (0, 3)divmod(5, 5) =  (1, 0)divmod(8.0, 3) =  (2.0, 2.0)divmod(3, 8.0) =  (0.0, 3.0)divmod(7.5, 2.5) =  (3.0, 0.0)divmod(2.6, 0.5) =  (5.0, 0.10000000000000009)`

#### Conclusion

This article covers how to use the divmod() function in Python. In fact, Python divmod() function is employed to return a tuple that contains the value of the quotient and therefore the remainder when dividend is divided by the divisor. It takes two parameters where the first one is the dividend and the second one is the divisor.

Python divmod() function Parameter Values:

• divident - This parameter contains the number you want to divide.
• divisor - This parameter contains the number you want to divide with.

#### Search by typing a one letter Word Term Here

• #### Install VidCutter on Linux Mint 21 "Vanessa" - Step by step guide

• #### Install TeXworks on Ubuntu 22.10 (Kinetic Kudu) - Step by step process

• #### DU Command in Linux - More about it ?

• #### Create a Network Bridge on CentOS 8 - Step by step guide ?

• #### What is CHOWN Command in Linux ?

• #### Check Ports in Use on Ubuntu 22.04 (Jammy Jellyfish)

• #### Install ZSH on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### How to use Ansible Loops ?

• #### Ansible 'When' Conditionals - More about it ?

• #### AWK Features Explained with Examples

• #### Bash "If String Equals" Comparison - Complete guide ?

• #### 6 Best Text Editors for Linux

• #### Disable the Network Interface on Boot in CentOS 8 - Step by step guide ?

• #### DMIDECODE Command in Linux - How to use it ?

• #### Compare Numbers in a Bash Script - How to do it ?

• #### Linux LSOF Command - Explained with Examples

• #### LVEXTEND Command in Linux - Explained with Examples ?

• #### PASSWD Command in Linux - Explained with Examples

• #### Replace a String in a File Using Ansible

• #### Rsync Command Options - Explained with Examples

• #### Execute Remote Command with SSH

• #### Linux sed Command Options - Explained with examples ?

• #### How Tags in Ansible Playbook works running Ubuntu 22.04 (Jammy Jellyfish) ?

• #### TAR Command Options in Linux - Explained with Examples

• #### Ansible "Debug" Module - Explained with Examples with Ubuntu

• #### ulimit Linux Command - Explained with Examples

• #### Working with Variables in Ansible running Ubuntu 22.04 (Jammy Jellyfish)

• #### Fix Inactive UFW Status on Ubuntu 22.04 (Jammy Jellyfish)

• #### Run Background Process in Ubuntu 22.04 (Jammy Jellyfish) Linux system

• #### UFW Allow Port on Ubuntu 22.04 (Jammy Jellyfish)

• #### UFW Remove Rule in Ubuntu 22.04 (Jammy Jellyfish) Linux system

• #### Python Compare Strings - How it works ?

• #### Which Flavour of Ubuntu Should You Choose ?

• #### Install pageedit editor on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### Install Webfs Server on Jammy Jellyfish Ubuntu 22.04 system

• #### Install kakoune Editor on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### Install Luakit browser on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### Install Gnome Edit on Ubuntu Jammy Jellyfish ( 22.04 ) - Step by step guide ?

• #### Install Pluma on Debian 11.4 (Bullseye) System - Step by step guide ?

• 