Zero-based learning Python (3) of the list

List

Today's focus is the list (like mentioned only list). A list of very commonly used in data storage, must be very skilled ! ! !
And a list of variables is not the same, it can store a set of data. Let's look at the format:

list = ['1','2','3','4','5']

This format is: list name = [Data 1, Data 2, Data 3]
The number and type of data is not limited to the middle with the comma separated, dried brackets enclose, with the name assigned to a list of assignments.
How to modify the list it? Usually several functions with the following:

list.append('6')
list.insert(0,'0')
list.pop(0)
list.remove('1')

These functions are used: append, insert, delete, remove.
I finished subscript detail them again.

Subscript

The subscript can represent each element of the list (i.e., data separated by a comma). Index starting from 0, such as: index of the first element is 0, the second 1 ...... In short, the position of the index element = element (an element in the first few) -1.
I want to use the index must: List the name [index name]. For chestnut:

list = ['1','2','3','4','5']
n = list[0]
print(n)

This output code string "a."
Next comes the function to modify the list.

append function

append function requires you to pass a parameter: Do you want to add data. After you pass the function will add the incoming data to the end of the list, which is called added.
Format: List name .append (data)

insert function

insert mean is inserted, so it can be inserted into your data to a certain position in the list. Therefore it requires you to pass two parameters: the first is the insertion position, the second is the data to be inserted. That how it represents the insertion position? I believe you have guessed, yes, that is subscript . The second I do not say, mentioned at the time mention append function. The format is: list name .insert (index data)
to see a chestnut:

list = ['1','2','3','4','5']
list.insert(0,'0')

This allows the next header inserting a string of "0" local zero.

pop function

pop mean to delete, then of course the role of this function is to remove an element of myself! You only need to pass a parameter that you want to delete index data that position. This is not for me to say, just write:

list = ['1','2','3','4','5']
list.pop(1)

Will be able to string "2" Delete it!
Maybe you have a doubt: do not pass what will happen? Good question! Does not pass, then delete the default value end of the list, doubts vanished!

remove function

As this function and pop functions role is deleted, but not the same thing incoming ~ ~ ~
it to incoming data content, yes, word for word! If no error is reported. But it also has a new role, but do not forget it is called to remove it!
You like the apple from the tree "removed", so you will get this apple. It also returns the value you are removed! If it receives a variable, like this:

list = ['1','2','3','4','5']
r = list.remove('1')

Then variable r is kept inside the string "1" friends ~

Focus point Zanga

Writing is not easy, I hope you can give me a support! After all, the fans support the author's power to write something Well! Also mind you, the content of this blog is very important, if you really want to learn Python, I suggest you read several times, and to remember the format in which he mentions. Believe me, it will be useful! Look forward to everyone's support, and also congratulate you all soon become the world ~ ~ ~ Koi programming see the next issue! ! !

Released four original articles · won praise 32 · views 1331

Guess you like

Origin blog.csdn.net/BearProgramming/article/details/105125191