• Python divmod()


    The divmod() method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder.

    The syntax of divmod() is:

    divmod(x, y)

    divmod() Parameters

    divmod() takes two parameters:

    • x - a non-complex number (numerator)
    • y - a non-complex number (denominator)

    Return Value from divmod()

    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.


    Example: How divmod() works in Python?

    print('divmod(8, 3) = ', divmod(8, 3))
    print('divmod(3, 8) = ', divmod(3, 8))
    print('divmod(5, 5) = ', divmod(5, 5))
    
    # divmod() with Floats
    print('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))

    Output

    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)
  • 相关阅读:
    redis的两种安装方法
    http头部信息
    软件测试技术知识
    github使用
    3 python之基础概要
    2 Python之编程语言介绍及变量
    1 Python入门及计算机硬件知识
    Linux常用命令
    CMDB小练习
    CMDB和运维自动化
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13631414.html
Copyright © 2020-2023  润新知