一 序列
这些类型是由一些成员共同组成的一个序列整体,所以统称为序列。
元组 ,列表,字符串。
1 .标准的操作符 都能应用序列。
2.序列类的操作符
①成员关系操作符(in not in)
成员关系符就是判断一个字符是否属于这个字符串,再就是这个字符串是否属于这个元组,或者列表。返回值也是布尔值(True,Flase)。
②连接操作符(“+”)
序列 + 序列
可以把2个序列组合到一个新的序列中去。
>>> ["a",2,5,"hou"] + ["zai", " cun"] ['a', 2, 5, 'hou', 'zai', ' cun']
③重复操作符(“*”)
序列 * 整数
>>> ["a",2,5,"hou"] * 3 ['a', 2, 5, 'hou', 'a', 2, 5, 'hou', 'a', 2, 5, 'hou']
④切片操作符([][:][::])
序列[索引]
倒索引 | -5 | -4 | -3 | -2 | -1 |
---|---|---|---|---|---|
数值 | 5 | 6 | 7 | 8 | 10 |
正索引 | 0 | 1 | 2 | 3 | 4 |
>>> list=["hou",2,"22",'2222'] >>> list[1] 正向 0开始 2 >>> list[-1] -1 结束 '2222' >>> list[-4] 'hou' >>> list[::-4] 反向数值的反 查 2222,22,2,hou -1 为hou ['2222'] >>> list[1:4:2] 从 索引 1到4 每次 跳2个 默认不输入为1 [2, '2222']
3 序列的内建 函数
list( ) 把一个元组 ,或者字符串转换为一个列表
tuple() 把一个列表。。。相反。
max()
min() 最大最小 没啥可说
len () 统计长度的
注意 :len(list) - 1 == list [-1](正向最后)那个索引
倒索引 | -5 | -4 | -3 | -2 | -1 |
---|---|---|---|---|---|
len( )=5 | 5 | 6 | 7 | 8 | 10 |
正索引 | 0 | 1 | 2 | 3 | 4 |
reversed()
sum()
zip()
二 字符串
字符串是python 最常见的类型。可以用引号 来创建 python的双引号和单引号一样的 ,shell 是 转义的只能在双引号里面表示。
收集常用的Python内置的各种字符串处理 函数的使用方法
str='python String function'
三 列表
列表和字符串一样是序列的数据类型,可以通过下标和切片访问某一个或者某一连续的元素。与其不通,字符串只能由字符组成且不能改变,然而列表是一个容器,能保留任何python对象。既然是一个容器当然 他就可以排序,以及对单独元素 插入 或者删除。
1.创建列表
列表由[ ] 来定义 以,分割元素。
a= 567
alist = [123,456,"hou",[ 'hou','zai','cun'], a,('uu','jj'')]
blist = [ ]
列表的元素可以是任何元素 上面是例子:数字 变量 子元组 字符串 子列表 。
2访问列表
和字符串一样 索引的方式访问也可以 内置函数 pop() 最后一个元素
>>>alist [0]
123
>>>alist [ :3]
[123,456,"hou"]
3更新列表
alist [0] =1
>>> alist = [1,456,"hou",[ 'hou','zai','cun'], a,('uu','jj'')]
append() 方法也可以在 列表最后索引加如
alist.append( 'ca')
>>> alist = [1,456,"hou",[ 'hou','zai','cun'], a,('uu','jj'),'ca']
4列表的操作符
和字符串基本一样(前面都有例子这不说了)
< > =
[ ] [ : ]
in not in
连接 + 重复 *
5列表解析
alist= [2,5,11]
>>> [i * 2 for i in alist ]
[4,10,22]
>>>[ i for i in range(8) if i %3 ==0]
[0,3,6]
6。列表内建函数
cmp() 列表的比较 ,一次对对2个列表的 的元素。 len() >>>alist=[1,1,22,222] >>>len(alist) 4 sorted() 排序 reversed() 倒排 enumerate() for i in range(len(alist)): print i, alist[i] 但利用enumerate,可以这样: for i, x in enumerate(alist): print i, x zip() >>> a = [1,2,3] >>> b = [4,5,6 ] >>> c = [4,5,6,7,8] >>> zipped = zip(a,b) [(1, 4), (2, 5), (3, 6)] a=[1,2,3] >>>sum(a) 6 list() tup() 互换函数 |
---|
7 列表的方法
dir(list) or dir([ ])
三。元组
元组和列表 基本差不多 唯一区别 元组的元素不可更改,
操作符 * + in not in > < = 等都可通用