当所用数值超过计算机自身的计算能力时,Python 会自动转用高精度计算(大数计算)。
请看下面的代码:#将 78 赋值给变量 n n = 78 print(n) print( type(n) ) #给x赋值一个很大的整数 x = 8888888888888888888888 print(x) print( type(x) ) #给y赋值一个很小的整数 y = -7777777777777777777777 print(y) print( type(y) )运行结果:
78
<class 'int'>
8888888888888888888888
<class 'int'>
-7777777777777777777777
<class 'int'>
78
<type 'int'>
8888888888888888888888
<type 'long'>
-7777777777777777777777
<type 'long'>
0b或0B开头。例如,101 对应十进制数是 5。
0o或0O开头。注意,第一个符号是数字 0,第二个符号是大写或小写的字母 O。0(数字零)开头。
0x或0X开头,
#十六进制
hex1 = 0x45
hex2 = 0x4Af
print("hex1Value: ", hex1)
print("hex2Value: ", hex2)
#二进制
bin1 = 0b101
print('bin1Value: ', bin1)
bin2 = 0B110
print('bin2Value: ', bin2)
#八进制
oct1 = 0o26
print('oct1Value: ', oct1)
oct2 = 0O41
print('oct2Value: ', oct2)
运行结果:
hex1Value: 69
hex2Value: 1199
bin1Value: 5
bin2Value: 6
oct1Value: 22
oct2Value: 33
_作为数字(包括整数和小数)的分隔符。通常每隔三个数字添加一个下划线,类似于英文数字中的逗号。下划线不会影响数字本身的值。
click = 1_301_547
distance = 384_000_000
print("Python教程阅读量:", click)
print("地球和月球的距离:", distance)
运行结果:
Python教程阅读量:1301547
地球和月球的距离:384000000
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有