python divmod

Python divmod() function is used to perform division on two input numbers. The numbers should be non-complex and can be written in any format such as decimal, binary, hexadecimal etc.

Python divmod()函数用于对两个输入数字进行除法。 数字应该是非复杂的,并且可以以任何格式编写,例如十进制,二进制,十六进制等。

Python divmod() (Python divmod())

Python divmod() syntax is:

Python divmod()语法为:

divmod(a, b)

The output is a tuple consisting of their quotient and remainder when using integer division.

使用整数除法时,输出是一个由它们的商和余数组成的元组 。

For integer arguments, the result is the same as (a // b, a % b).

对于整数参数,结果与(a // b,a%b)相同。

For floating point numbers the result is (q, a % b), where q is usually (math.floor(a / b), a % b). In any case q * b + a % b is very close to a.

对于浮点数,结果为(q,a%b),其中q通常为(math.floor(a / b),a%b)。 无论如何,q * b + a%b非常接近a。

If a % b is non-zero it has the same sign as b, and 0 <= abs(a % b) < abs(b).

如果%b不为零,则它的符号与b相同,并且0 <= abs(a%b)<abs(b)。

带有整数的Python divmod()示例 (Python divmod() example with integers)

# simple example, returns (a // b, a % b) for integers
dm = divmod(10, 3)
print(dm)x, y = divmod(10, 3)
print(x)
print(y)dm = divmod(0xF, 0xF)  # hexadecimal
print(dm)

Output:

输出:

(3, 1)
3
1
(1, 0)

带有浮点数的Python divmod()示例 (Python divmod() example with floats)

# floats, returns usually (math.floor(a / b), a % b)
dm = divmod(10.3, 3)
print(dm)dm = divmod(11.51, 3)
print(dm)dm = divmod(-11.51, 3)
print(dm)

Output:

输出:

(3.0, 1.3000000000000007)
(3.0, 2.51)
(-4.0, 0.4900000000000002)

Notice that in case of floating points, the q * b + a % b is very close to a in some cases.

注意,在浮点情况下, q * b + a % b在某些情况下非常接近a

带有复数的divmod() (divmod() with complex numbers)

If we pass complex numbers as argument, we will get TypeError.

如果我们将复数作为参数传递,则会得到TypeError

dm = divmod(3 + 2J, 3)

Output: TypeError: can't take floor or mod of complex number.

输出: TypeError: can't take floor or mod of complex number.

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22813/python-divmod

python divmod

python divmod_Python divmod()相关推荐

  1. Python中的“ @”(@)符号有什么作用?

    我正在看一些使用@符号的Python代码,但我不知道它的作用. 我也不知道要搜索什么,因为搜索Python文档时会出现,或者当包含@符号时Google不会返回相关结果. #1楼 此代码段: def d ...

  2. 如何使用Python numpy.where()方法

    In Python, we can use the numpy.where() function to select elements from a numpy array, based on a c ...

  3. Python字符串title()

    Python String title() Python字符串title() Python String title() function returns a title cased version ...

  4. Python字符串isdecimal()

    Python String isdecimal() function returns True if all the characters in the string are decimal char ...

  5. Python字符串isalnum()

    Python string isalnum() function returns True if it's made of alphanumeric characters only. A charac ...

  6. Python字符串count()

    Python String count() function returns the number of occurrences of a substring in the given string. ...

  7. Python字符串splitlines()

    Python String splitlines() Python字符串splitlines() Python String splitlines() function returns the lis ...

  8. Python字符串join()方法

    Python string join() method creates a string from an iterable. It joins all the iterable elements wi ...

  9. Python字符串index()

    Python String index() function returns the lowest index where the specified substring is found. If t ...

最新文章

  1. c语言 静态链表插入排序,数据结构C语言版 表插入排序
  2. 嵌入式系统笔记之自动登录
  3. Lucene 4.X 倒排索引原理与实现: (3) Term Dictionary和Index文件 (FST详细解析)——直接看例子就明白了!!!...
  4. java 报500该怎么解决_关于servlet500错误 应该怎么解决
  5. 解决svn错误:post-commit hook failed (exit code 1) with output
  6. Multidex实现简要分析
  7. Authentication和Authorization的区别
  8. 什么是JAX-RS注释?
  9. 深入理解DOM事件机制系列第四篇——事件模拟
  10. python协同过滤调用包_简单的python协同过滤程序实例代码
  11. Java 能抵挡住 JavaScript 的进攻吗?
  12. launchpad乐器_PreSonus 发布 ATOM 打击垫控制器(视频)
  13. JavaScript TypeScript 学习总结
  14. Simulink仿真计算中保留特定位数的小数
  15. 物联网模块ESP8266-01S实现PC端和Android APP同时连接通信
  16. Ubuntu断点续存之wget
  17. 微信小程序网络请求异常怎么办_微信小程序网络超时的处理
  18. mipi传输距离3米_蓝牙网关有效传输距离是多少?
  19. Python到底是干啥的?
  20. python爬取谷歌学术参考文献的BibTex格式——基于selenium

热门文章

  1. 计算机网络 洪泛算法,泛洪
  2. 八款苹果M1生产力工具
  3. python画爱心(2)
  4. 程序员如何邂逅真爱之伪攻略篇
  5. 微信公众号配置 Token 认证以及消息推送功能
  6. 互联网技术研发管理之激励提升
  7. excel中纯数字字符串递增和数字显示时添加自定义字符串
  8. html5 小车动画_9款非常有意思的HTML5动画推荐
  9. 修改CentOS锁屏时间
  10. 2022 年精选架构合集:Redis+Nginx+ 设计模式 +Spring 全家桶 +Dubbo