2.2 数据类型2: Numeric & String
1. Python数据类型
1.1 总体:numerics, sequences, mappings, classes, instances, and exceptions
1.2 Numeric Types: int (包含boolean), float, complex
1.3 int: unlimited length; float: 实现用double in C, 可查看 sys.float_info;
complex: real(实部) & imaginary(虚部),用z.real 和 z.imag来取两部分
1.4 具体运算以及法则参见:https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
Python数字
他们是不可改变的数据类型,这意味着改变数字数据类型会分配一个新的对象。
del var1[,var2[,var3[....,varN]]]]
int | long | float | complex |
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEl | 32.3e+18 | .876j |
-0490 | 535633629843L | -90. | -.6545+0J |
-0x260 | -052318172735L | -32.54e100 | 3e+26J |
0x69 | -4721885298529L | 70.2E-12 | 4.53e-7j |
- 长整型也可以使用小写"L",但是还是建议您使用大写"L",避免与数字"1"混淆。Python使用"L"来显示长整型。
- Python还支持复数,复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型
import sys
a = 3
b = 4
c = 5.66
d = 8.0
f = complex(float(a), float(b))
print ("a is type" , type(a))
print ("b is type" , type(b))
print ("c is type" , type(c))
print ("d is type" , type(d))
print ("e is type" , type(e))
print ("f is type" , type(f))
print(a + b)
print(d / c)
print (b / a)
print (b // a)
print (e)
print (e + f)
print ("e's real part is: " , e.real)
print ("e's imaginary part is: " , e.imag)
print (sys.float_info)