1.1 总体:numerics(数字型), sequences(序列), mappings(映射), classes(类), instances(实例), and exceptions(异常处理)
1.2 Numeric Types(数字类型): int {整形,包含boolean(布尔型)}, float(浮点型), complex(复数类型)
1.3 int: unlimited length(长度不受限制); float(由于Python是用C语言编写的,因此double对应C语言中的double类型, 可通过系统命令 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是动态语言,和传统语言(C、C++、Java)不同,在声明变量时不需要声明变量的类型,Python可以自动识别出。
例:
import sys #导入sys系统包
a = 3
b = 4
c = 5.66
d = 8.0
e = complex(c, d) #在复数类型里,实部和虚部都是浮点型变量
f = complex(float(a), float(b)) #float(a)将int类型转为float型
print ("a is type" , type(a)) #用type()命令查看变量的类型
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 ( sys.float_info) #通过系统命令sys.float_info打印出该类型的取值范围,由于使用了系统包sys,所以使用之前首先要导入sys
print ("e's real part is: " , e.real) #打印复数的实部
print ("e's imaginary part is: " , e.imag) #打印复数的虚部
本文介绍了Python作为动态语言,如何自动识别和处理不同类型的数据,如整型、浮点型和复数。示例中展示了变量a、b、c、d的赋值和类型检查,并演示了不同类型间的运算,包括加法、除法、取整以及复数的构造和属性访问。通过`sys.float_info`获取浮点数的取值范围。
1384

被折叠的 条评论
为什么被折叠?



