标签:
序列和元组
1. 区别: 序列能修改,元组不行
2. 列表写法:
user = [‘Fiona‘, 18]
user2 = [‘Lala‘, 19]
database = [user, user2]
print(database)
output: [‘Fiona‘, 18],[‘Lala‘, 19]
3. 数据结构:container(容器),可以包含其他对象的任意对象。序列(列表和元组)和映射(比如字典)是两种主要的容器。
4. 索引 (indexing):
- 所有元素都是有编号的,从0开始递增。
greeting = ‘hello‘ print(greeting[0]) output: h #如果print(greeting[-1]) #output:0 从后往前数
- 也可以这样
fourth = input(‘year:‘)[3] print(fourth) #input: 1982 output:2
5. 分片(slicing): 注意,这个编号是从1开始的
- 这样
tag = ‘<a herf ="http://www.python.org">python web site</a>‘ print(tag[9:31]) #output: http://www.python.org
- 或者这样
number = [1,2,3,4,5,6] print(number[3:5]) #output: [4,5]
标签:
原文地址:http://www.cnblogs.com/hualala/p/5492861.html