{"id":24807,"date":"2021-11-02T09:48:02","date_gmt":"2021-11-02T04:18:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=24807"},"modified":"2021-11-05T20:36:45","modified_gmt":"2021-11-05T15:06:45","slug":"python-divmod-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-divmod-method-with-examples\/","title":{"rendered":"Python divmod() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python int() Method with Examples<\/a> When argument1 (dividend) is divided by argument2, the divmod() function returns a tuple containing the quotient and the remainder (divisor).<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:<\/strong><\/p>\n dividend:<\/strong> It is a number. The number by which you wish to divide<\/p>\n divisor:<\/strong> It is a number. The number you wish to divide by.<\/p>\n Return Value:<\/strong><\/p>\n The divmod() Function returns<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similarly, Check it out for other numbers<\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n In the previous article, we have discussed Python int() Method with Examples divmod() Method in Python: When argument1 (dividend) is divided by argument2, the divmod() function returns a tuple containing the quotient and the remainder (divisor). Syntax: divmod(dividend, divisor) Parameter Values: dividend: It is a number. The number by which you wish to divide divisor: …<\/p>\n
\ndivmod() Method in Python:<\/strong><\/p>\ndivmod(dividend, divisor)<\/pre>\n
\n
Given first number(dividend)= 3\r\nGiven second number(divisor) = 5<\/pre>\n
A Tuple containing the quotient and the remainder = (0, 3)<\/pre>\n
Given first number(dividend) = 12.5\r\nGiven second number(divisor) = 3.5<\/pre>\n
A Tuple containing the quotient and the remainder = (3.0, 2.0)<\/pre>\n
divmod() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first number as static input and store it in a variable.\r\ngvn_numb1 = 3\r\n# Give the second number as static input and store it in another variable.\r\ngvn_numb2 = 5\r\n# Pass the given first and second numbers as arguments to the divmod() function\r\n# that returns a tuple containing the quotient and the remainder (divisor).\r\nrslt = divmod(gvn_numb1, gvn_numb2)\r\n# Print the above result i.e, a tuple containing the quotient and the remainder\r\n# (divisor).\r\nprint(\"A Tuple containing the quotient and the remainder =\", rslt)\r\n<\/pre>\n
A Tuple containing the quotient and the remainder = (0, 3)<\/pre>\n
gvn_numb1 = 12.5\r\ngvn_numb2 = 3.5\r\nrslt = divmod(gvn_numb1, gvn_numb2)\r\nprint(\"A Tuple containing the quotient and the remainder =\", rslt)\r\n<\/pre>\n
A Tuple containing the quotient and the remainder = (3.0, 2.0)<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the first number as user input using the float(input()) function \r\n# and store it in a variable.\r\ngvn_numb1 = float(input(\"Enter some random number = \"))\r\n# Give the second number as user input using the float(input()) function \r\n# and store it in another variable.\r\ngvn_numb2 = float(input(\"Enter some random number = \"))\r\n# Pass the given first and second numbers as arguments to the divmod() function\r\n# that returns a tuple containing the quotient and the remainder (divisor).\r\nrslt = divmod(gvn_numb1, gvn_numb2)\r\n# Print the above result i.e, a tuple containing the quotient and the remainder\r\n# (divisor).\r\nprint(\"A Tuple containing the quotient and the remainder =\", rslt)\r\n<\/pre>\n
Enter some random number = 45.5\r\nEnter some random number = 5\r\nA Tuple containing the quotient and the remainder = (9.0, 0.5)<\/pre>\n
\n