- 列表的遍历使用in与not in。in与not in还可以判断字符是否在字符串中。
list1 = ['hello', 'emm', 666, 233]
for i in list1: # i是迭代对象,会依次遍历list1的对象,并赋予自己。
print(i)
print('p' not in 'hello') # 输出True
- 列表元素的增加操作有append()函数、extend()函数、insert()函数与切片。
(1)append()函数在列表的末尾添加一个元素;
(2)extend()函数在列表的末尾添加多个元素;
(3)insert()函数在列表的任意位置添加一个元素;
(4)切片在列表的任意位置添加多个元素。
list1 = ['hello'