a = ['a', 'b', 'c', 'd', 'e']
# 切片起止索引越界不会出问题
first_20_items = a[: 20]
print(first_20_items) # ['a', 'b', 'c', 'd', 'e']
last_20_items = a[-20:]
print(last_20_items) # ['a', 'b', 'c', 'd', 'e']
# 访问单个元素时,下标不能越界
try:
print(a[20])
except Exception as e:
print(e.__class__, e)
# < type 'exceptions.IndexError' >
# IndexError('list index out of range', )
# 对切片内容赋值,会自动扩张或收缩原列表
a[2:4] = [1, 2, 3, 4]
print(a) # ['a', 'b', 1, 2, 3, 4, 'e']
# 对原列表进行拷贝
b = a[:]
print(b == a) # True
print(b is a) # False
# 增加新的引用
a = ['a', 'b', 'c', 'd', 'e']
b = a
a[2: 4] = [1, 2, 3, 4]
print(b) # ['a', 'b', 1, 2, 3, 4, 'e']
print(b is a) # True
本系列文章和代码已经作为项目归档到github,仓库地址:jumper2014/PyCodeComplete。大家觉得有帮助就请在github上star一下,你的支持是我更新的动力。什么?你没有github账号?学习Python怎么可以没有github账号呢,快去注册一个啦!