Home > Mobile >  How to divide a number by 10 only in Python?
How to divide a number by 10 only in Python?

Time:09-23

I just need to divide some number by only 10. For instance, if the number is 127, the result should only be 12 and remained to be 7. Is there a way I can get Python to give me these BOTH results? Because I need to divide that remainder by some number as well for instance 2. So overall it should be like this: 127 = 12 (10) and 3 (2) and 1(remainder).

Hope that makes sense

CodePudding user response:

def divide_and_get_remainder(num, divisor):
    quotient = num // divisor
    remainder = num % divisor
    return quotient, remainder

if __name__ == '__main__':
    num = 127
    divisor = 10
    q, r = divide_and_get_remainder(num,divisor)
    print(f'{num} / {divisor} = {q} with remainder = {r}')

CodePudding user response:

You can try using double division and modulus division -

quotient = 127 // 10

remainder = 127 % 10


print(quotient) # 12
print(remainder) # 7

Or use divmod -

num = 127
d = 10 # Divisor

quotient,remainder = divmod(num, d) # It returns tuple 

print(quotient) # 12
print(remainder) # 7
  • Related