Create list
Create a normal list
Create a mixed list
Create an empty list
Add elements to the list
append(): Only one parameter can be added
append(): The function belonging to the object is called the method, here the append() method belongs to the member object, so use "." to restrict its object
extend(): can add multiple parameters
The principle is to use one list to extend another list, so its parameter should be a list
append and extend can only be added to the end of the list
insert(): Insert list elements at random positions
There are two parameters
- The first parameter represents the position in the list
- The second parameter represents inserting an element at the position of the first parameter
- All the order starts from 0
Why start from 0?: Because the computer itself is a binary system, it processes binary data. The binary world only has 0 and 1. 0 is the first number in binary, and it also starts from 0 in decimal.
Task
-
What can be stored in the list?
Integer, string, floating point, that is, any data type -
What are the ways to add elements to the list?
append(), extend(), insert() -
The append() method and the extend() method both add elements to the end of the list. What is the difference between them?
The former can only add one at the end, the latter can add multiple, and the latter can only be added as a list -
Does member.append(['竹林小溪','Crazy迷恋']) achieve the same effect as member.extend(['竹林小溪','Crazy迷恋'])?
Not the same, the former realizes the addition of an element, namely ['Zhulin Xiaoxi','Crazy fascination']; the latter realizes the addition of two elements,'Zhulin Xiaoxi' and'Crazy fascination'. -
There is a list name = ['F','i','h','C']. If the turtle wants to insert the element's' between the elements'i' and'h', what method should be used to insert it?
insert()
5. Try it yourself and analyze in this case, which method should be used to add data to the list?
Suppose the following list is given:
member = ['Little Turtle','Dark Night','Lost','Yijing','Autumn Dance Setting Sun']
Request to modify the list to:
member = ['Little Turtle', 88,'Dark Night', 90,'Lost', 85,'Yijing', 90,'Autumn Dance Setting Sun', 88]
Method 1: Use insert() and append() methods to modify the list.
Method 2: Re-create a list coverage with the same name.
member = ['小甲鱼', '黑夜', '迷途', '怡静', '秋舞斜阳']
member.insert(1, 88)
member.insert(3, 90)
member.insert(5, 85)
member.insert(7, 90)
member.append(88)
member = ['小甲鱼', 88, '黑夜', 90, '迷途', 85, '怡静', 90, '秋舞斜阳', 88]
The second method is better. For large lists, the first method may be more appropriate
6. Use the for loop to print each content in the member list above, as shown in the figure:
member = ['小甲鱼', 88, '黑夜', 90, '迷途', 85, '怡静', 90, '秋舞斜阳', 88]
for i in member:
print(i)
- The print style of the previous question is not very good. Can you modify the code and print it into the style of the figure below? [Please use at least two methods to achieve]
Keyword must introduce counting numbers.
Method 1: Use odd and even numbers to judge
member = ['小甲鱼', 88, '黑夜', 90, '迷途', 85, '怡静', 90, '秋舞斜阳', 88]
number = range(len(member))#把列表里的元素转化为相对应的位置,即数字
for i in number:
if i % 2 ==0:
print(member[i],member[i + 1])#member[i+1]输出member列表中第i+1个
Method 2: Use string judgment
member = ['小甲鱼', 88, '黑夜', 90, '迷途', 85, '怡静', 90, '秋舞斜阳', 88]
num = 0
for i in member:
if isinstance(i,str) == True:
num += 1
print(member[num-1],member[num])
else:
num += 1
Method three:
member = ['小甲鱼', 88, '黑夜', 90, '迷途', 85, '怡静', 90, '秋舞斜阳', 88]
count = 0
length = len(member)
while count < length:
print(member[count], member[count+1])
count += 2