多语言展示
当前在线:780今日阅读:31今日分享:25

python核心编程(第二版)--学习笔记--2.6

Python中的数字
工具/原料
1

python交互解释器

2

linux shell

方法/步骤
1

有符号整型,二进制以0b为前缀,八进制以0o为前缀,十六进制以0x,0X为前缀int  例子如:0b01,0o12,0x80,-0X92十进制的例子:84,-237,-680,Python 3.4.3 (default, Nov 28 2017, 16:41:13) [GCC 4.8.4] on linuxType 'help', 'copyright', 'credits' or 'license' for more information.>>> i = 0b01>>> i1>>> type(i)>>> i = 0o12>>> i10>>> type(i)>>> i = 0x80>>> i128>>> type(i)>>> i = -0X92>>> i-146>>> type(i)>>> i = 84>>> i84>>> type(i)>>> i = -237>>> i-237>>> type(i)>>> i = 680>>> i680>>> type(i)

2

长整型,仅受限于用户计算机的虚拟内存总数,类似于java中的BigInteger类型long 例如:29979062458,-841401,0xDECADEDEADBEEFBADFEEDDEA长整型已与整型整合为一体,python中会自将超过长度的int转为长整型。Python 3.4.3 (default, Nov 28 2017, 16:41:13) [GCC 4.8.4] on linuxType 'help', 'copyright', 'credits' or 'license' for more information.>>> l = 1>>> l1>>> id(l)10088288>>> type(l)>>> l = 29979062458>>> id(l)4272>>> type(l)调用id()方法,可见地址已经变了。但是调用type()方法,仍显示为 int类型。

3

布尔型是特殊的整型,尽管布尔型常由True和False表示,如果将布尔值放到一个数值上下文环境中,True会被当成整型值1,False会被当成整型值0Python 3.4.3 (default, Nov 28 2017, 16:41:13) [GCC 4.8.4] on linuxType 'help', 'copyright', 'credits' or 'license' for more information.>>> b = True>>> bTrue>>> type(b)>>> i = 9>>> i9>>> type(i)>>> x = b + i>>> x10>>> type(x)

方法/步骤2
1

浮点数float例子如:3.1415926,4.2E-10,-90.decimal并不是python内置支持,需要先导入后才能使用。Python 3.4.3 (default, Nov 28 2017, 16:41:13) [GCC 4.8.4] on linuxType 'help', 'copyright', 'credits' or 'license' for more information.>>> f = 3.1415926>>> f3.1415926>>> type(f)>>> f = 1.1>>> f1.1>>> 1.11.1>>> f = 4.2E-10>>> f4.2e-10>>> type(f)>>> f = -90.>>> f-90.0>>> type(f)>>> import decimal>>> print (decimal.Decimal('1.1'))1.1

2

复数类型是python特有的支持类型,在其它的语言中通常不被支持。complex 例如:6.23+1.5j,-1.23-875JPython 3.4.3 (default, Nov 28 2017, 16:41:13) [GCC 4.8.4] on linuxType 'help', 'copyright', 'credits' or 'license' for more information.>>> c = 6.23+1.5j>>> c(6.23+1.5j)>>> type(c)>>> c = -1.23-875J>>> c(-1.23-875j)>>> type(c)

注意事项

decimal并不是python的内置类型,使用前需要先导入。

推荐信息