divmod()函数也是python的内置函数,它是把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)
divmod(x, y)
参数: x,y都是数字,可以是整形也可以是浮点数,x 是分子,y 是分母;
返回值:返回一个元组(x//y,x%y);
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): https://www.codersrc.com/
@File:python_divmod.py
@Time:2019/12/07 21:25
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
print("{}".format(type(divmod(9,6))))
print("{}".format(divmod(9,6)))
print("{}".format(type(divmod(9.2,6.6))))
print("{}".format(divmod(9.2,6.6)))
输出结果:
<class 'tuple'>
(1, 3)
<class 'tuple'>
(1.0, 2.5999999999999996)
注意:divmod()函数只能接受整数或浮点数类型的参数不能使用字符串,否则会报错!
转载请注明:猿说Python » python divmod函数