Python 内建类型 - 数字(值)类型

Manual

There are three distinct numeric types:integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of integers. Integers have unlimited precision. Floating point numbers are usually implemented usingdouble in C; information about the precision and internal representation of floating point numbers for the machine on which your program is running is available insys.float_info. Complex numbers have a real and imaginary part, which are each a floating point number. To extract these parts from a complex numberz, use z.real and z.imag. (The standard library includes additional numeric types, fractions that hold rationals, anddecimal that hold floating-point numbers with user-definable precision.)
Numbers are created by numeric literals or as the result of built-in functions and operators. Unadorned integer literals (including hex, octal and binary numbers) yield integers. Numeric literals containing a decimal point or an exponent sign yield floating point numbers. Appending ‘j’ or ‘J’ to a numeric literal yields an imaginary number (a complex number with a zero real part) which you can add to an integer or float to get a complex number with real and imaginary parts.
Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. Comparisons between numbers of mixed type use the same rule. The constructorsint(), float(), and complex() can be used to produce numbers of a specific type.

直译

共有三种不同的数值类型:整数、浮点数和复数。此外,布尔值为整数的子类型。

  • 整数具有无限精度。
  • 浮点数通常利用C语言中的double实现,关于你的电脑运行程序的浮点数的精度和内置表征信息,可通过sys.float_info来获取。
  • 复数含有实部和虚部,其各为浮点数。从复数‘z’中提取这些部分时,可以使用z.real和z.imag(标准库中包含额外数值类型,分数(fractions)支持有理数,小数(decimal)支持自定义精度浮点数)。

数字通过数值文字创建,或作为内建函数和运算符的结果。纯整数文字(包括十六进制、八进制和二进制数)产出整数。数值文字包括一个小数点或指数符号产出浮点数。将’j’或’J’追加到数值文字来产生一个虚数(实数部为0的复数),虚数可以添加一个整数或浮点数来获得一个包含实部和虚部的复数。

Python支持全部混合算法:当一个二进制算术运算符有不同数值类型的运算对象时,更狭窄类型的运算对象被其他对象所放宽,那就是整数比浮点数狭窄,浮点数比复数狭小。比较使用相同规则的混合型数字,构造函数int()、float()和complex()可以被用来生产制定类型的数字。

Note

这部分关于数值类型的内容比较晦涩,需要反复推敲,但其实想表达的意思还是非常简单的。

拓展阅读

sys.float_info
fractions
decimal
int()
float()
complex()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值