列表

1. 列表的常用操作总结


(1)列表.pop(下标)不仅可以删除列表指定下标元素,还可以将删除元素作为返回值返回!

(2)列表可以通过下标索引获取值!

 

 

2. 列表的特点


 

 

3. 练习


lst = [21, 25, 21, 23, 22, 20]
lst.append(31)
lst.extend([29, 33, 30])
print(lst[0])
print(lst[-1])
print(lst.index(31))

 

 

4. 列表的遍历


分别使用whilefor循环遍历列表中的元素:

lst = [i for i in range(1, 11)]

# for
save_for = []
for each in lst:
    if each % 2 == 0:
        save_for.append(each)
print(save_for)

# while
save_while = []
index = 0
while index < len(lst):
    if lst[index] % 2 == 0:
        save_while.append(lst[index])
    index += 1
print(save_while)

 

posted @ 2023-07-28 20:05  Peg_Wu  阅读(10)  评论(0)    收藏  举报