Hướng dẫn int division in python

In Python, you can calculate the quotient with // and the remainder with %.

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged python modulo integer-division or ask your own question.
  • How do you write quotient and remainder in Python?
  • How do you divide a remainder in Python?
  • What is quotient in Python?
  • Which operator is used to find quotient in Python?

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged python modulo integer-division or ask your own question.
  • How do you write quotient and remainder in Python?
  • How do you divide a remainder in Python?
  • What is quotient in Python?
  • Which operator is used to find quotient in Python?
q = 10 // 3
mod = 10 % 3
print(q, mod)
# 3 1

The built-in function divmod() is useful when you want both the quotient and remainder.

  • Built-in Functions - divmod() — Python 3.7.4 documentation

divmod(a, b) returns a tuple (a // b, a % b).

You can unpack and assign to each variable.

  • Unpack a tuple and list in Python
q, mod = divmod(10, 3)
print(q, mod)
# 3 1

Of course, you can receive it as a tuple.

answer = divmod(10, 3)
print(answer)
print(answer[0], answer[1])
# (3, 1)
# 3 1

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two numbers n and m. The task is to find the quotient and remainder of two numbers by dividing n by m.

    Examples:

    Input:
    n = 10
    m = 3
    Output:
    Quotient:  3
    Remainder 1
    
    Input
    n = 99
    m = 5
    Output:
    Quotient:  19
    Remainder 4

    Method 1: Naive approach

    The naive approach is to find the quotient using the double division (//) operator and remainder using the modulus (%) operator.

    Example:

    Python3

    def find(n, m):

        q = n//m

        print("Quotient: ", q)

        r = n%m

        print("Remainder", r)

    find(10, 3)

    find(99, 5)

    Output:

    Quotient:  3
    Remainder 1
    Quotient:  19
    Remainder 4

    Time Complexity: O(1)

    Auxiliary Space: O(1)

    Method 2: Using divmod() method

    Divmod() method takes two numbers as parameters and returns the tuple containing both quotient and remainder.

    Example:

    Python3

    q, r = divmod(10, 3)

    print("Quotient: ", q)

    print("Remainder: ", r)

    q, r = divmod(99, 5)

    print("Quotient: ", q)

    print("Remainder: ", r)

    Output:

    Quotient:  3
    Remainder 1
    Quotient:  19
    Remainder 4

    Time Complexity: O(1)

    Auxiliary Space: O(1)


    How could I go about finding the division remainder of a number in Python?

    For example:
    If the number is 26 and divided number is 7, then the division remainder is 5.
    (since 7+7+7=21 and 26-21=5.)

    Hướng dẫn int division in python

    asked Apr 7, 2011 at 16:44

    1

    you are looking for the modulo operator:

    a % b
    

    for example:

    >>> 26 % 7
    5
    

    Of course, maybe they wanted you to implement it yourself, which wouldn't be too difficult either.

    wjandrea

    24.4k8 gold badges52 silver badges73 bronze badges

    answered Apr 7, 2011 at 16:45

    Uku LoskitUku Loskit

    39.7k9 gold badges87 silver badges91 bronze badges

    2

    The remainder of a division can be discovered using the operator %:

    >>> 26%7
    5
    

    In case you need both the quotient and the modulo, there's the builtin divmod function:

    >>> seconds= 137
    >>> minutes, seconds= divmod(seconds, 60)
    

    answered May 1, 2011 at 11:49

    tzottzot

    88.9k29 gold badges135 silver badges200 bronze badges

    0

    26 % 7 (you will get remainder)

    26 / 7 (you will get divisor, can be float value)

    26 // 7 (you will get divisor, only integer value)

    wjandrea

    24.4k8 gold badges52 silver badges73 bronze badges

    answered Mar 17, 2016 at 22:14

    1

    If you want to get quotient and remainder in one line of code (more general usecase), use:

    quotient, remainder = divmod(dividend, divisor)
    #or
    divmod(26, 7)
    

    answered Feb 21, 2019 at 4:44

    Alok NayakAlok Nayak

    2,24220 silver badges28 bronze badges

    1

    From Python 3.7, there is a new math.remainder() function:

    from math import remainder
    print(remainder(26,7))
    

    Output:

    -2.0  # not 5
    

    Note, as above, it's not the same as %.

    Quoting the documentation:

    math.remainder(x, y)

    Return the IEEE 754-style remainder of x with respect to y. For finite x and finite nonzero y, this is the difference x - n*y, where n is the closest integer to the exact value of the quotient x / y. If x / y is exactly halfway between two consecutive integers, the nearest even integer is used for n. The remainder r = remainder(x, y) thus always satisfies abs(r) <= 0.5 * abs(y).

    Special cases follow IEEE 754: in particular, remainder(x, math.inf) is x for any finite x, and remainder(x, 0) and remainder(math.inf, x) raise ValueError for any non-NaN x. If the result of the remainder operation is zero, that zero will have the same sign as x.

    On platforms using IEEE 754 binary floating-point, the result of this operation is always exactly representable: no rounding error is introduced.

    Issue29962 describes the rationale for creating the new function.

    answered Jan 11, 2018 at 14:28

    Chris_RandsChris_Rands

    36.2k12 gold badges78 silver badges110 bronze badges

    1

    If you want to avoid modulo, you can also use a combination of the four basic operations :)

    26 - (26 // 7 * 7) = 5
    

    answered Jul 14, 2013 at 3:07

    alysalys

    3213 silver badges10 bronze badges

    Use the % instead of the / when you divide. This will return the remainder for you. So in your case

    26 % 7 = 5
    

    answered Apr 7, 2011 at 16:47

    codewariocodewario

    17.7k19 gold badges84 silver badges148 bronze badges

    We can solve this by using modulus operator (%)

    26 % 7 = 5;

    but 26 / 7 = 3 because it will give quotient but % operator will give remainder.

    answered Feb 10, 2018 at 19:20

    1

    Modulo would be the correct answer, but if you're doing it manually this should work.

    num = input("Enter a number: ")
    div = input("Enter a divisor: ")
    
    while num >= div:
        num -= div
    print num
    

    answered Apr 7, 2011 at 17:25

    CooperCooper

    6693 silver badges9 bronze badges

    1

    You can find remainder using modulo operator Example

    a=14
    b=10
    print(a%b)
    

    It will print 4

    mischva11

    2,7093 gold badges16 silver badges32 bronze badges

    answered Apr 15, 2020 at 7:50

    1

    If you want the remainder of your division problem, just use the actual remainder rules, just like in mathematics. Granted this won't give you a decimal output.

    valone = 8
    valtwo = 3
    x = valone / valtwo
    r = valone - (valtwo * x)
    print "Answer: %s with a remainder of %s" % (x, r)
    

    If you want to make this in a calculator format, just substitute valone = 8 with valone = int(input("Value One")). Do the same with valtwo = 3, but different vairables obviously.

    answered Oct 19, 2017 at 0:33

    1

    Here's an integer version of remainder in Python, which should give the same results as C's "%" operator:

    def remainder(n, d):
        return (-1 if n < 0 else 1) * (abs(n) % abs(d))
    

    Expected results:

    remainder(123, 10)   ==  3
    remainder(123, -10)  ==  3
    remainder(-123, 10)  == -3
    remainder(-123, -10) == -3
    

    answered Mar 19, 2021 at 2:18

    you can define a function and call it remainder with 2 values like rem(number1,number2) that returns number1%number2 then create a while and set it to true then print out two inputs for your function holding number 1 and 2 then print(rem(number1,number2)

    answered Jun 25, 2020 at 11:52

    1

    Not the answer you're looking for? Browse other questions tagged python modulo integer-division or ask your own question.

    How do you write quotient and remainder in Python?

    In Python, you can calculate the quotient with // and the remainder with % ..

    q = 10 // 3 mod = 10 % 3 print(q, mod) # 3 1. source: divmod-test.py..

    q, mod = divmod(10, 3) print(q, mod) # 3 1. source: divmod-test.py..

    answer = divmod(10, 3) print(answer) print(answer[0], answer[1]) # (3, 1) # 3 1. source: divmod-test.py..

    How do you divide a remainder in Python?

    The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem. The modulo operator is considered an arithmetic operation, along with + , - , / , * , ** , // .

    What is quotient in Python?

    In Division The number which we divide is called the dividend. The number by which we divide is called the divisor. The result obtained is called the quotient. The number left over is called the remainder.

    Which operator is used to find quotient in Python?

    To find the quotient, the '//' operator is used. To find the remainder, the '%' operator is used. The operations output is assigned to two variables respectively.