Directory:
First, create a list
Second, add elements to the list
Iii. 10 lessons After class exercises and answers
***************
First, create a list
***************
Create a list the same way you create a normal variable, enclose a stack of data in brackets, separating the data with commas.
>>> number = [1,2,3,4,5]>>> number[1, 2, 3, 4, 5]>>> Len ( Number)5
You can create a list of the most promiscuous:
>>> mix = [1," small turtle ", 3.14,[1,2,3]]>>> mix[' Little Turtle ', 3.14, [1, 2, 3]]
You can create an empty list:
>>> empty = []>>> empty[]
***********************
Second, add elements to the list
***********************
Append () Extend () insert ()
To add an element to the list, you can use the Append () method: (Note: append () is not a bif, it belongs to a method of the list object)
>>> number = [1,2,3,4,5]>>> number.append (6)>>> number[1, 2, 3, 4, 5, 6]
We added the numbers 7 and 8, but found that we could not add multiple elements with append () at the same time: we can use the extend () method to add multiple elements to the end of the list:
>>> number.append (7,8) Traceback (most recent call last): File " <PYSHELL#3> ", line 1, in <module> Number.append ( 7,8) Typeerror:append () takes exactly one argument ( 2 given) >>> number.extend (7,8 <pyshell#4> , Line 1, in < Module> Number.extend ( 7,8 2 given)
Why all the error?! The Extend () method actually uses a list to extend another list, so its parameters should be a list:
>>> number.extend ([7,8])>>> number[1, 2, 3, 4, 5, 6, 7, 8]
If you want to insert an element anywhere in the list, use the Insert () method. The Insert () method has two parameters: the first parameter represents the position in the list, and the second parameter inserts an element at that position. Try it and let the number 0 appear in the front of the list numbers: (Note: The first index value of the list is 0)
>>> Number.insert (0,0)>>>1, 2, 3, 4, 5, 6, 7, 8]
*******************************
Iii. 10 lessons After class exercises and answers
*******************************
Test questions:
0. What can be stored in the list?
1. What are the ways to add elements to a list?
2. Both the Append () method and the Extend () method add elements to the end of the list, what is the difference between them?
3. Member.append ([' Bamboo Grove Creek ', ' crazy Infatuation ']) and member.extend ([' Bamboo Grove Creek ', ' crazy infatuation ']) achieve the same effect?
4. There is a list of name = [' F ', ' I ', ' h ', ' C '], what method should be used to insert if the turtle wants to insert the element ' s ' between the elements ' I ' and ' H '?
Moving hands:
0. Try it yourself and analyze in this case, which method should be used to add data to the list?
Suppose given the following list:
member = [' Little Turtle ', ' Dark Night ', ' lost ', ' Yi Jing ', ' Autumn Dance ']
Request to modify the list to:
member = [' Little Turtle ', 88, ' Night ', 90, ' lost ', 85, ' Yi Jing ', 90, ' Autumn Dance ', 88]
Method One: Use the Insert () and append () methods to modify the list.
Method Two: Recreate a list overlay with a word with the same name.
1. Use the for loop to print each of the contents of the top member list,
Small Turtle in the night , the lost of the autumn dance Sun 88
2. The previous question printing style is not very good, can you modify the code to print a style? "Please use at least two ways to implement"
Small turtle88
Answer:
answer to test question:
0. What can be stored in the list?
We say that the Python list is an array of hormones, and if the array is likened to a container, then the Python list is a large warehouse, and Ta can store any data types we've learned.
1. What are the ways to add elements to a list?
We've taught you in this class three ways to add elements to the list: Append (), extend (), and insert (). 2. Both the Append () method and the Extend () method add elements to the end of the list, what is the difference between them?
The append () method is to set the parameteras an elementAdd to the end of the list.
The Extend () method is to set the parameteras a listGo to the end of the extension list.
>>> name = ['F','I','s','h']>>> Name.append ('C')>>>name['F','I','s','h','C']>>> Name.extend (['.','C'])>>>name['F','I','s','h','C','.','C']>>> Name.append (['o','m'])>>>name['F','I','s','h','C','.','C', ['o','m']]
3. Member.append ([' Bamboo Grove Creek ', ' crazy Infatuation ']) and member.extend ([' Bamboo Grove Creek ', ' crazy infatuation ']) achieve the same effect?
Not the same, because it is afraid that we do not have a careful look at the example of a problem, so do not understand please see a question to explain.
>>> Member = [A]>>> Member.append (['Bamboo Creek','Crazy Fetish'])>>>member[1, 2, 3, ['Bamboo Creek','Crazy Fetish']]>>> Member = [A]>>> Member.extend (['Bamboo Creek','Crazy Fetish'])>>>member[1, 2, 3,'Bamboo Creek','Crazy Fetish']
4. There is a list of name = [' F ', ' I ', ' h ', ' C '], what method should be used to insert if the turtle wants to insert the element ' s ' between the elements ' I ' and ' H '?
Name.insert (2, ' s ')
hands-on answers:
0. Try it yourself and analyze in this case, which method should be used to add data to the list?
Suppose given the following list:
member = [' Little Turtle ', ' Dark Night ', ' lost ', ' Yi Jing ', ' Autumn Dance ']
Request to modify the list to:
member = [' Little Turtle ', 88, ' Night ', 90, ' lost ', 85, ' Yi Jing ', 90, ' Autumn Dance ', 88]
Method One: Use the Insert () and append () methods to modify the list.
Method Two: Recreate a list overlay with a word with the same name. Answer:
Method One:
Member.insert (1,member.insert)Member.insert (3, 5)Member.insert (7, $) Member.append (88)
Method Two:
member = [' Little Turtle ' ' dark night ' astray ' Pleasant calm ' Autumn dance ', 88]
In this case, it is obvious that the second method looks better.
However, for large lists, the first method may be more appropriate, so we say never the best, only the most appropriate.
1. use loop print top member
member = [' Little Turtle ' ' dark night ' astray ' Yi Jing " Autumn dance Sun ", the " member: print(each)
>>> member = ['Little Turtle', 88,'Night', 90,'Stray', 85,'Yi Jing', 90,'Autumn dance setting sun', 88]>>> foreachinchMember:Print(each) small turtle88Night90Stray85Yi Jing90Autumn dance setting sun88
2. the previous question printing style is not very good, can you modify the code to print a style? "Please use at least two ways to implement"
= len (member) while count < length: print(Member[count], member [Count+1]) + = 2 method Two :for in Range (len (member)) :if each%2 = = 0: Print (Member[each], member[each+1])
Small Turtle 88
Night 90
Stray 85
Yee Jing 90
Autumn Dance 88
0.0 Note that this question is a bit different from the one in the last lesson, please go on the computer experiment or the answer.
>>> old = [1, 2, 3, 4, 5]>>> new = old >>> old = [6]print(NE W
What do you think will be printed if you do not operate on the computer?
>>> old = [1, 2, 3, 4, 5]>>> new = old >>> old = [6]print
(new) [1, 2, 3, 4, 5]
Lesson 10: List: An array of hormones played 1