更新时间:2023-07-17 来源:黑马程序员 浏览量:

在Python中,divmod()函数是一个内置函数,用于执行整数除法并返回商和余数。它接受两个参数,被除数和除数,并返回一个包含商和余数的元组。divmod()函数可以方便地在执行除法时同时获取商和余数。
以下是divmod()函数的用法示例:
# 示例 1
result = divmod(10, 3)
print(result) # 输出 (3, 1)
# 示例 2
quotient, remainder = divmod(15, 4)
print(quotient) # 输出 3
print(remainder) # 输出 3
# 示例 3
numerator = 23
denominator = 6
quotient, remainder = divmod(numerator, denominator)
print(f"The quotient is {quotient} and the remainder is {remainder}") # 输出 "The quotient is 3 and the remainder is 5"在示例1中,divmod(10, 3)执行整数除法10 // 3并返回商3和余数1的元组。
在示例2中,divmod(15, 4)执行整数除法15 // 4并将商和余数分别赋值给quotient和remainder两个变量。
在示例3中,divmod(numerator, denominator)执行了变量numerator和denominator的整数除法,并将商和余数分别赋值给quotient和remainder两个变量。最后,使用格式化字符串输出商和余数的值。
总结:divmod()函数的作用是执行整数除法并返回商和余数的元组,它可以用于各种需要同时获取商和余数的场景。
1024首播|39岁程序员逆袭记:不被年龄定义,AI浪潮里再迎春天
2025-10-241024程序员节丨10年同行,致敬用代码改变世界的你
2025-10-24【AI设计】北京143期毕业仅36天,全员拿下高薪offer!黑马AI设计连续6期100%高薪就业
2025-09-19【跨境电商运营】深圳跨境电商运营毕业22个工作日,就业率91%+,最高薪资达13500元
2025-09-19【AI运维】郑州运维1期就业班,毕业14个工作日,班级93%同学已拿到Offer, 一线均薪资 1W+
2025-09-19【AI鸿蒙开发】上海校区AI鸿蒙开发4期5期,距离毕业21天,就业率91%,平均薪资14046元
2025-09-19