python3中的列表

列表

    数据序列,能存储多个数据的连续存储空间。

列表的创建

    a_list=['this','is','a','list',1,2,3,4]

    b_list=list('hello')    #双引号也可以

    b_list=list(range(1,20,2))

列表的访问

    下标从0开始

    a_list[1]

    a_list[8]  : IndexError: list index out of range

    下标为负数:a_list[-1]   : 为4

    切片:a_list[1:3] ,a_list[1:]

列表的遍历

for x in a_list:
       print(x)
for i in range(len(a_list)):
       print(a_list[i])

更新列表

list = ['Google', 'Runoob', 1997, 2000]
 
print ("第三个元素为 : ", list[2])
list[2] = 2001
print ("更新后的第三个元素为 : ", list[2])
            输出结果
第三个元素为 :  1997
更新后的第三个元素为 :  2001

删除列表元素

list = ['Google', 'Runoob', 1997, 2000]
 
print (list)
del list[2]
print ("删除第三个元素 : ", list)

              输出结果

删除第三个元素 : ['Google', 'Runoob', 2000] 



猜你喜欢

转载自blog.csdn.net/qq_38534627/article/details/80205913