list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d"]
print("1.访问列表中的值")
print("list1[0]: ", list1[0]) # 输出 list1[0]: physics
print("list2[1:5]: ", list2[1:5]) # 输出 list2[1:5]: [2, 3, 4, 5]
print("2.更新列表")
list = [] # 空列表
list.append('Google') # 使用 append() 添加元素
list.append('Runoob')
print(list) # 输出 ['Google', 'Runoob']
print("3.删除列表元素")
print(list1)
del list1[2]
print("After deleting value at index 2 : ")
print(list1) # 输出 ['physics', 'chemistry', 2000]
print("4.Python列表脚本操作符")
print(len(list3)) # 输出 4
print(list2+list3) # 输出 [1, 2, 3, 4, 5, 'a', 'b', 'c', 'd']
print(list2*3) # 输出 [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(3 in list2) # 输出 True
for x in list2: print (x)
"""
输出:
1
2
3
4
5
"""
print("5.Python列表截取")
L = ['Google', 'Runoob', 'Taobao']
print(L[2]) #输出 Taobao
print(L[-2]) #输出 Runoob
print(L[1:]) #输出 ['Runoob', 'Taobao']
print("6.方法")
# 对原列表进行排序
aList = ['123', 'Google', 'Runoob', 'Taobao', 'Facebook'];
aList.sort();
print("List : ")
print(aList)
"""
输出
List :
['123', 'Facebook', 'Google', 'Runoob', 'Taobao']
"""
# 在列表末尾添加新的对象
aList.append( 2009 );
print ("Updated List : ", aList) # 输出 Updated List : ['123', 'Facebook', 'Google', 'Runoob', 'Taobao', 2009]