Learn Python The Hard Way learning (38)-list operations

Source: Internet
Author: User

We have already learned list. When learning while, we add elements to the list and print them. In the extra score exercise, let's also find some other list operations. If you don't know what I'm talking about, go back and review what we 've learned.

If we only know how to use the list append method, we do not actually know the usage of this function. Let's take a look at its usage.

When we use mystuff. append ('hello'), a series of things occur. Let's see what happened:
Python will first check the mystuff variable to see if it is a function parameter or a global variable. Find mystuff first.
When mystuff uses the. Number, it first looks at what the variable mystuff is. If it is a list, there will be many of its methods available.
When using append, you will find whether mystuff has the 'append' name. If yes, you can use it directly.
If the append is followed by parentheses, you will know that it is a function. Just execute this function as usual and there will be additional parameters.
This extra parameter is mystuff. It's strange, but python does this. The final function is like this: append (mystuff, 'Hello ').
In most cases, you don't have to know what is going on, but it helps when you encounter the following error:
Www.2cto.com :~ # Python
Python 2.6.5 (r265: 79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Class Thing (object ):
... Def test (hi ):
... Print "hi"
...
>>> A = Thing ()
>>> A. test ("hello ")
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: test () takes exactly 1 argument (2 given)
>>>

What are the above? You have never learned the class, but we will learn it later. Now let's take a look at the error reported by python. If we change it to test (a, "hello"), or not, the person who writes the class is not correct.

These may be difficult to digest, but there will be a lot of exercises to strengthen it later. The following exercises are to mix strings and lists to see if we can find something interesting.
[Python]
Ten_things = "Apples Oranges Crows Telephone Light Sugar"
 
 
Print "Wait there's not 10 things in that list, let's fix that ."
 
 
Stuff = ten_things.split ("")
More_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
 
 
While len (stuff )! = 10:
Next_one = more_stuff.pop ()
Print "Adding:", next_one
Stuff. append (next_one)
Print "Thing'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])


Running result
Www.2cto.com # python ex38.py
Wait there's not 10 things in that list, let's fix that.
Adding: Boy
Thing's 7 items now.
Adding: Girl
Thing's 8 items now.
Adding: Banana
Thing's 9 items now.
Adding: Corn
Thing's 10 items now.
There we go: ['append', 'oranges', 'crows', 'telphone', 'light ', 'sugal', 'Boy', 'girl ', 'bana', 'corn']
Let's do some things with stuff.
Oranges
Corn
Corn
Apples Oranges Crows Telephone Light Sugar Boy Girl Banana
Telephone # Light
Www.2cto.com #

Extra score exercise
1. Translate the function into the execution format of python. For example, ''. join (stuff) is join ('', stuff)

2. translate two function call methods in language, such ''. join (stuff) is to use ''To connect stuff, join ('', stuff) is to use ''and stuff to call the join method

3. check some object-oriented programming information on the Internet. I used to do the same. Don't worry. I will understand it later.

4. Check the usage of class in python. Do not look at the usage of class in other languages. This will only confuse you.

5. What is the relationship between dir (something) and something class.

6. If you don't understand what I said, it doesn't matter. programmers have invented Object-Oriented Programming (OOP), which is often used. If you think this is difficult, then we will learn function programming.
Author: lixiang0522

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.