Target:
Understand the real meaning of the list method.
Operation:
List_1.append (Element) ==> append (list_1, Element)
mystuff.append (' hello ') code like this, you have actually triggered a chain reaction inside Python. Here's how it works:
1. Find MyStuff this variable first
2. Find the MyStuff and then process the period . (period) This operator starts to look at some of the variables inside the mystuff. Since MyStuff is a list, Python knows that MyStuff supports some functions.
3. the next turn is to deal with the append . Python will compare the names of all functions supported by "append" and MyStuff, and if there is one function called Append, then Python will use this function.
4. next Python sees the parentheses (parenthesis) and realizes , "Oh, this is supposed to be a function," and here it's normal to call this function, But there is one more parameter to the function here.
5. This extra parameter is actually mystuff! I know, it's weird, isn't it? But that's how Python works, so keep this in mind, just when it's OK. What really happens is, actually, Append (mystuff, ' hello ') , but all you see is mystuff.append (' Hello ') .
Code:
Ten_things ='Apples oranges Crows Telephone light Sugar'Print("Wait There ' s not things on that list, let's fix that.") Stuff= Ten_things.split (" ") More_stuff=[' Day','Night','Song','Frisbee','Corn','Banana','Girl',' Boy'] whileLen (stuff)! = 10: Next_one=More_stuff.pop ()Print('Adding:', Next_one) stuff.append (next_one)Print("there ' s%d items now."%len (stuff))Print("there we go:", Stuff)Print("Let's do some things with stuff.")Print(stuff[1])Print(stuff[-1])Print(Stuff.pop ())Print(' '. Join (stuff))Print("#". Join (Stuff[3:5]))
Results:
Wait there ' s not things on that list, let's fix that. Adding: boythere ' s 7 items now. Adding: girlthere ' s 8 items now. Adding: bananathere ' s 9 items now. Adding: cornthere ' s ten items now. There we go: [' Apples ', ' oranges ', ' Crows ', ' telephone ', ' light ', ' Sugar ', ' Boy ', ' Girl ', ' Banana ', ' Corn ']let ' s do s ome things with stuff. Orangescorncornapples Oranges Crows Telephone light Sugar boy Girl bananatelephone#light
Comments:
- Each called function is translated into the actual action performed by Python in the above way.
For example: ". Join (things) is actually a join (', things).
- Translate these two methods into natural languages.
For example, ". Join (things) can be translated to" join with 1 spaces "things, while join (', things) means" call the Join function for a space (') and things ". This is actually the same thing.
Learn from the stupid method of Python
Python3 Exercises Series (07)--List operation principle