The Python language contains 6 built-in sequences, of which there are two main types: lists and tuples.
Lists can be modified, and tuples are not allowed, and if you want to add or remove certain elements, you can only use a list, in order to restrict certain elements, you will use tuples. In general, lists can be substituted for tuples. In the list, there are indexes, shards, add, multiply and so on.
One, list (lists) with square brackets []
A list is a built-in data type of Python, and list is an ordered set of elements that can be added and removed at any time.
Gets the list of elements to be obtained with a corner mark, a corner mark can use a positive angle, or you can use a negative angle, throw indexerro when out of bounds
The data type of the elements in the list can also be different (just like in Java), or you can put another list within the list, which creates a multidimensional collection
The common methods are:
1.len () Gets the length of the list
2.x.append ("hello") adding elements to the list
3.x.pop () Popup End Element
1Zoo = ['Pig','Tiger','Dog']2 Print(Len (Zoo))3Zoo.append ("Nihao")4 Print(Zoo)5 Print(Zoo.pop ())6 Print(zoo[0])7 Print(Zoo[-1])
Second, a tuple (tuple) with parentheses ()
Ganso is also an ordered list, and the list is very similar, the difference is the tuple once defined can not be modified, in a sense this also improves the security of the code, the Query method and list, the use of a tuple can use a tuple.
Add "comma" when defining Ganso with only one element to avoid ambiguity with mathematical operations
Variable tuple
When a list is placed inside the ancestor, the value of the list in the ancestor can change, in fact the tuple does not change, the value of the internal list is changed.
1 country = ('bj', ['sh','gd' ])2print(country[1])3print(country[1 ][0])4 country[1][0] ="nihao"5Print (Country[1][0])
Third, the dictionary
Dictionary (dictionary): using {} to define
dict={' name ': ' Pei ', ' age ': ' 26 '}
Python's tuple, the difference between a list and a dictionary