Python provides a variety of data types to hold data item collections, mainly including sequences (list and tuple tuple), mappings (such as dictionary dict), collections (set), which are described in one of these ways:
A sequence
1. List of lists
A list is an ordered collection of elements that are variable in relation to tuples and strings, and can be added and removed at any time.
(1) Create List
Test on the command line as follows:
>>> L1 = [1,2,3]>>> L1 [1, 2, 3]>>> L2 = [' abc ']>>> L2 [' abc ']>>> L3 = ["A", " B "," C "]>>> L3 [' A ', ' B ', ' C ']
Note that the string must be quoted.
It is very useful to create a list of strings through the list, such as:
>>> L = List ("Python") >>> L [' P ', ' y ', ' t ', ' h ', ' o ', ' n ']
(2) Visit list
Depending on the index, note that you cannot cross the line, which is particularly similar to arrays:
>>> l[0] ' P ' >>> l[-1] ' n '
(3) Adding new elements
Append the new element to the end of the list using the Append () method, and insert () to add a new element to a specific location.
(4) Deleting an element
The delete element can take the pop () method, execute L.pop () to delete the last element of the list, if it is a specific position can take the pop (2), 2 represents the position.
(5) Replacement
The substitution is simple, and the direct index is fine.
(6) Printing
>>> L = [' A ', ' B ', ' C ']>>> for I in L:print (i) a B C
Reprint to: (Strange _ Yang Source: http://www.cnblogs.com/ybjourney/p/4767726.html)
Summary of common data types in Python (i)