In front of the content learned, make a small game children play very greasy, boring boring ...
It seems to speed up and go straight to the list and tuples:
A list is an ordered collection of elements that can be freely added and removed:
>>> animals = [' dog ', ' cat ', ' pig ',]>>> print animals[' dog ', ' cat ', ' pig ']
If you want to access a single animal in the list, how do you play it, just to say that the list is orderly, right? Yes, starting from 0, the back is 1, 2, 3 ..., look down and you'll understand.
>>> print animals[0] ' dog ' >>> print animals[2] ' pig '
>>> Print Animals[4]traceback (most recent call last): File "<stdin>", line 1, in <module>indexerror : List index out of range
Out of range! How many elements are there?
>>> len (animals) 3>>> print animals[len (animals) -1]pig>>> print animals[-1]pig>> > Print Animals[-2]cat
Animals a large number, how to add? In fact, I already know, the key is that the word will not be written ..., these words are still very children to learn, but also think of a word, plus to
>>> animals.append (' fox ') >>> print animals[' dog ', ' cat ', ' pig ', ' Fox ']>>> animals.insert (0 , ' sheep ']>>> print animals[' sheep ', ' dog ', ' cat ', ' pig ', ' Fox ']>>> Animals.pop () ' Fox ' >>> Print animals[' sheep ', ' dog ', ' cat ', ' pig ']>>> animals.pop (1) ' Dog ' >>> print animals[' sheep ', ' cat ', ' Pig ']>>> animals[0] = ' Fox ' >>> print animals[' fox ', ' cat ', ' pig '
Try a lot of usage, remember!!!
The list supports nesting, no longer an example, on tuples
Tuples use parentheses, the list above is in brackets, which is a difference
In addition, tuples (tuple) cannot be modified, so it is more secure
Here is the dictionary, compared to the list of its operation speed, and the data is irrelevant, its key is not the object can be changed (location hash)
>>> animals = [' dog ', ' cat ', ' pig ',]>>> years = [' 2 ', ' 2 ', ' 1 ',]
In fact, this is the information of the time I keep this little animal, the expression of this information in dictionary (dict) will appear very NB:
>>> Dict1 = {' Dog ': 2, ' cat ': 2, ' pig ': 1,}>>> print dict1[' pig ']1>>> dirct1[' dog '] = 3>& Gt;> ' sheep ' in dict1false>>> dict1.get (' sheep ' [, None]) # can specify a string that does not exist when the result is not displayed because it does not exist. >>> dict1.pop (' Cat ') 2
Similar to the dictionary there is a set set, but no value, the key does not repeat, create set to enter the list
>>> S1 = set ([' A ', ' B ', ' C ',]) >>> print s1set ([' A ', ' B ', ' C ']) >>> s1.add (' d ') >>> S1.R Emove (' a ') >>> s2 = set ([' A ', ' C ', ' d ']) >>> S1 & S2set ([' A ', ' C ']) >>> S1 | S2set ([' A ', ' B ', ' C ', ' d '])
Python self-study takeoff--004