Python数据类型

本文介绍了Python作为动态语言,如何自动识别和处理不同类型的数据,如整型、浮点型和复数。示例中展示了变量a、b、c、d的赋值和类型检查,并演示了不同类型间的运算,包括加法、除法、取整以及复数的构造和属性访问。通过`sys.float_info`获取浮点数的取值范围。
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(由于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)   #打印复数的虚部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值