#列表元素的增加操作
#append()-->在列表的末尾添加一个元素
#extend()-->在列表的末尾至少添加一个元素
#insert(0-->在列表的任意位置添加一个元素
#切片-->在列表的任意位置至少添加一个元素
###########################################
lst=[10,20,30]
print('添加元素前',lst,id(lst))
lst.append(100)
print('添加元素后',lst,id(lst))#[10, 20, 30, 100]
lst2=['hello','world']
#lst.append(lst2)#将lst2作为一个元素添加到列表的末尾
#print(lst)#[10, 20, 30, 100, ['hello', 'world']]
lst.extend(lst2)#在列表的末尾至少添加一个元素
print(lst)#[10, 20, 30, 100, 'hello', 'world']
lst.insert(1,90)#在索引为1的位置上添加元素90
print(lst)#[10, 90, 20, 30, 100, 'hello', 'world']
lst3=[True,False,'hello']
lst[1::]=lst3
print(lst)#把1后切掉,把lst3放入
Python入门--列表元素的增加操作,append(),extend(),insert(),切片
于 2022-02-10 16:20:02 首次发布