Small turtle Python tenth post-lecture exercise -- 010 list

0. What can a list store?

Can store any data type

 

1. What are the ways to add elements to a list?

append() - add an element at the end

extend()--Add multiple elements at the end, but require the format of the list to add [x,x,x,x]

insert()---Add an element after specifying the position

 

2. The append() method and the extend() method both add elements to the end of the list. What is the difference between them?

The append() method adds the argument as an element to the end of the list.
The extend() method takes the argument as a list to extend the end of the list.

 

3. Does member.append(['Bamboo Forest Stream', 'Crazy Obsession']) achieve the same effect as member.extend(['Bamboo Forest Stream', 'Crazy Obsession'])? 

Not the same, append will add ['bamboo stream', 'Crazy obsession'] as an element, including the brackets. extend will add two elements 'Bamboo Forest Stream' and 'Crazy Infatuation'.

 

4. 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?

name.insert(2,'s')

Hands-on:

Method 1: Use the insert() and append() methods to modify the list

member.insert(1, 88)  

member.insert(3, 90)

member.insert(5, 85)  

member.insert(7, 90)  

member.append(88)  

 

Method 2: Recreate a list overlay with the same name.

member = ['Little turtle', 88, 'Dark night', 90, 'Lost', 85, 'Yijing', 90, 'Autumn dance and setting sun', 88]

 

Method three:

temp = [88,90,85,90,88]
for i in range(1,10,2):
num = (i-1)//2
member.insert(i,temp[num])

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325687422&siteId=291194637