Python divmod函数

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门


一.divmod函数介绍

       divmod 函数也是 Python 的内置函数,它是把除数和余数运算结果结合起来,返回一个包含商和余数的元组 tuple (a // b, a % b)

'''
参数: x,y都是数字,可以是整形也可以是浮点数,x 是分子,y 是分母;

返回值:返回一个元组(x//y,x%y);
'''
divmod(x, y)

二.divmod函数使用

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python divmod函数.py
@Time:2021/04/04 11:00
@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 函数只能接受整数 int 或浮点数类型 float 参数,不能使用字符串 string , 否则会报错!


三.猜你喜欢

  1. Python 字符串/列表/元组/字典之间的相互转换
  2. Python 局部变量和全局变量
  3. Python type 函数和 isinstance 函数区别
  4. Python is 和 == 区别
  5. Python 可变数据类型和不可变数据类型
  6. Python 浅拷贝和深拷贝
  7. Python 递归函数
  8. Python sys模块
  9. Python 列表list
  10. Python 元组tuple
  11. Python 字典 dict
  12. Python 条件推导式
  13. Python 列表推导式
  14. Python 字典推导式
  15. Python 函数声明和调用
  16. Python 不定长参数 *argc/**kargcs

ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞2 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容