Python内置数据类型
python的数据类型有:
- 数值类型
- int 整形
- float 浮点型
- complex 复数
- bool 布尔值
- 序列对象
- str 字符串
- list 列表
- tuple 元组
- 键值对
- set 集合
- dict 字典
数值型
常用的处理函数
- 【match.round()】 四舍六入五取偶
- 【match.ceil()】 向上取整
- 【match.floor()】 向下取整,和【//】运算结果类似
- 【int()】只取整数部分
import math
print(f"floor函数:{math.floor(2.5)}\t ceil函数:{math.ceil(2.5)}")
print(f"floor函数:{math.floor(-2.5)}\t ceil函数:{math.ceil(-2.5)}")
类型的排断
【isinstance(‘abc’,str)】等价于【type(‘abc’) == str】
但是 【isinstance(False,int)】的值为True,而【type(False) == int】的值为 False.因为isinstance可以判断子类,而bool类型是整形的子类。
【isinstance(‘abc’,(int,float,str))】判断‘abc’的类型是否在元组(int,float,str)中
元组tuple
简单例子:
例子2:
- 元组的访问
注意:元组是只读的,所以增、改、删方法都没有
命名元组
简单示例:
注意:
namedtuple(“Point”,“x y”)等价于:namedtuply(“Point”,“x,y”)也等价于namedtuply(“Point”,[‘x’,‘y’])
因为,在nametuply源码中“x,y”与“x y”都将逗号【,】转换成空格后根据空格切割成列表。
关键原码如下: