1. List
How to declare: List = [1,2.3, ' x ', ' Hello '], with method:
- List.append (x) Add an item at the end of the list (append)
- List.extend (L) to extend the current list (extended) with the given list
- List.insert (I,X) Inserts an item at a given position
- List.remove (x) Remove the first item in the list with the value X, Note that X is not an index
- List.pop ([i]) deletes the item at the given position and returns
- List.index (x) returns the index value of the first value of x in the list, with no match resulting in an error
- List.count (x) returns the number of occurrences of x in the list
- List.sort () sort
- List.reserve () Reverse
To iterate through an instance:
numbers = [0,1,2,3,4,5,6,7,8,9]
For I in range (len (numbers)):
Print (Numbers[i])
2, tuple tuple
The Declaration method is very special, tuple = Item1,item2,item3,item4. For example:
tuple = 12,323,4,0,57, ' Hello '
For I in range (len (tuple)):
Print (Tuple[i])
3. Set Set
How to declare: Set = {ITEM1,ITEM2,ITEM3,ITEM4}, for example:
Basket = {' A ', ' B ', ' C ', ' d ', ' e '}
The collection is an unordered set of elements, and the result of the example declaration is:
{' E ', ' C ', ' d ', ' B ', ' A '}
The traversal method is:
For I basket:
Print (i)
4. Dictionary Dict
Declaration Example: tel = {' Jack ': 23432, ' scape ': 234}
The following methods can be used to assign values: tel[' chunyu '] = 19910805
The result is: {' Chunyu ': 19910805, ' Jack ': 23432, ' scape ': 234}
You can use the items () method to get the keys and corresponding values, for example:
For k,v in Tel.items ():
Print (K,V)
The traversal method is:
Tel = {' Chunyu ': 19910805, ' Jack ': 23432, ' scape ': 234}
For key in Tel:
Print (Key, ': ', Tel[key])
Python data Structure-List, Tuple, Set, Dict