One, tuple (tuple)
1. Feature: A data sequence that cannot be changed. "Understanding: Once a tuple is created, the tuple cannot be modified, that is, the tuple cannot be updated, incremented, deleted ,"
2. Create: A pair of parentheses "()" and its contained elements (or an empty tuple if there are no elements).
Create generic tuples: That is, one-dimensional tuples. such as: Temptuple = ("One", "one", "one", "three", 4,5,6).
Create nested tuples: Tuples can also contain tuples, that is, nested tuples or two-dimensional (multidimensional) tuples. such as: Multuple = (("You", "is", "a", "dog"), "You", "too").
Note: If you want to create a tuple that contains an element, you must add a comma "," after the element, or create a string instead of a tuple.
such as: Atuple = ("One",), astring = ("one"). Type (atuple), "tuple", type (astring), "string".
3. Access: tuple name [Index],index is the index of the element in the tuple, indexed as an integer, starting from 0. Note: Index cannot cross the border, otherwise it will error.
Access General tuples: Temptuple[0], "One", Temptuple[3], 4.
Access nested tuples: multuple[0][1], "is".
Note: You can use a negative number as an index to access the tuple. -1 for the last element, 2 for the penultimate, and so on. such as: Temptuple[-1], "too".
You can use a for loop to traverse a tuple. such as: for element in Temptuple:print (elment).
4. Length: Len (tuple name). such as Len (Temptuple)->6. (The length is also the number of elements in the tuple).
5. Printing: print (tuple name), which prints all the elements in the tuple.
6. Type: "Tuple", type (tuple name).
7. Delete: Del tuple name, delete tuple, when accessing the deleted tuple error, prompt: tuple is undefined. (Other types can also use this method)
II. lists (list)
1. Features: A data series that can be changed. (distinct from tuples, can be dynamically added, deleted, updated)
2. Create: a square bracket "[]" and its contained elements, a single element can be without commas, as in the same tuple, you can create nested lists. such as: Templist = ["One", "one", "three"].
3. Basic operation and Method:
(1) Access, traversal, length, printing, type and other operations with tuples
(2) Update: Assigns a value to the element in the list, and cannot assign a value to an element that does not exist in the list. such as: templist[2] = 3->templist = ["One", "one", "3],templist[3" = "four" and Error!!!)
(3) Delete: After the element is deleted (not the last one), the element on the right moves to the left.
1) del: deletes the specified element. such as: Del templist[0]->templist = ["", "" three "]
2) Pop: Deletes the element at the specified position and returns the deleted element, removing the last one by default. such as: element = Templist.pop (1)->element = Two,templist = ["One", "three"].
(4) Increase (insert):
1) Append: Add an element to the end of the list, the parameter cannot be empty, or an error. such as: Templist.append ("four")->templist = ["One", "one", "the" "Three", "four"].
2) Extend: Add multiple elements to the end of the list, parameters cannot be empty, cannot be of type int, can be a string (treated as a list, each character is an element), a tuple, a list. Such as:
The argument is a string: Templist.extend ("str")->templist = ["One", "one", "one", "three", "s", "T", "R"].
The parameters are tuples or lists: Templist.extend (("Four", "five")->templist = ["One", "" "" "", "" three "," four "," five "].
3) List[index:index] = [value]: The position specified by the list index inserts an element. such as: templist[1:1] = ["Add"], templist = ["One", "Add", "one", "three"].
Note: More general form: listname[start:end] = tuple/list/string, multiple elements can be inserted.
4. Advanced operation and Method:
(1) A numeric increment list is generated:
1) pList = Range (N), resulting in a list of element values of 0~n-1. such as: PList = Range (Ten)->plist = [0,1,2,3,..., 9].
2) PList = Range (Sn,en), which produces a list of element values of sn~en-1. such as: PList = range (1,5)->plist = [1,2,3,4].
3) PList = Range (sn,en,inc), which produces a list of element values that are incremented by Inc. Example: PList = Range (1,10,2)->plist = [1,3,5,7,9]
(2) Fixed value initialization: PList = [value for index in range (n)], produces a list of length N, element values are value.
such as: value = "x", n=10, then plsit = ["X", "X",..., "X"]
Simpler form: PList = [Value]*n.
(3) Operator:
1) "+": two lists added, will be combined into a list. such as PL1 = [1,2],PL2 = [3,4],pl1+pl2->[1,2,3,4]
2) "*": Form: [Value]*n. such as value= "a", n=4, then get the list ["a", "a", "a", "a"].
3) Del:del Pl[index]: Deletes the element at the specified position. Del Pl[sindex:eindex]: Removes the element from the sindex~eindex-1 position.
(4) List copy:
1) pL1 = Pl:pl1 is the alias of PL, that is, pL1 and PL are in the same list, modify one of the lists, and the other list changes.
such as: PL = [1,2,3],PL1 = pl,pl1[2] = 33,->PL = PL1 = [1,2,33]
2) pL2 = Pl[:]:pl2 is a clone (or copy) of PL, that is, pL2 and PL are different lists, modify one of the lists, and the other will not be affected.
(5) Common methods:
1) l.append (value): Inserts an element into the end of the list
2) L.insert (Index,value): Inserts an element value to the index position of the list.
3) L.pop (index): Removes the element at the specified position from the list and returns the element value, removing the last element by default.
4) L.remove (value): Deletes the first occurrence of the element in the list value. such as: PList = [1,2,3,2],plist.remove (2)->plist = [1,3,2].
5) L.count (value): Returns the number of occurrences of the element value in the list.
6) L.index (value): The position of the first occurrence of the element, without throwing an exception.
7) L.extend (list/tuple/string): Inserts multiple elements at the end of the list.
8) L.sort (): Sort
9) L.reverse (): Reverse
Three, Dictionary (dictionary)
1. Attributes: grouped data indexed by name. Tuples and lists are indexed numerically, while dictionaries can be indexed in numbers, letters, strings, symbols, and so on.
In the dictionary, the index is called the key, that is key, and the corresponding value is called the value.
2. Create: dic = {key1:value1,key2:value2}, you can first create an empty dictionary, DiC ={}, and then initialize it, such as dic["one" = "firstvalue".
Note: The key is unique, and the dictionary only recognize the last assigned key value. For example: dic = {1:1,2:2,1:3}->dic = {1:3,2:2}, the last element (1:3) is "discarded".
3. Access: Dic[key], the value corresponding to the key, if the key does not exist, then an error.
4. Common methods and operations:
1) d.get (key, Defualtvalue): Gets the value corresponding to the key, if the key does not exist, the default value of Defualtvalue is returned, and none is returned if no default value is set.
2) D.has_key (key): Check if there is a key in the dictionary, the key returns True, otherwise false.
3) D.keys (): Returns a list of all the keys in the dictionary.
4) d.values (): Returns a list of all the values in the dictionary.
5) D.items (): Returns a list of all key-value pairs in the dictionary, i.e. [(key1,value1), (key2,value2), (KEY3,VALUE3),...].
6) D.update (DIC2): Merges elements from dictionary dic2 into dictionary D.
7) D.popitem (): Randomly deletes a key-value pair (one item) in the dictionary and returns the value. Throws an exception if the dictionary is empty.
8) D.clear (): Clears the element in the dictionary and returns none
9) D.pop (key): Deletes the item corresponding to the specified key and returns the value corresponding to the key. Key cannot be empty, nor is it the default to delete the last element, because the dictionary is unordered, note and list the difference!!!
D.copy (): Copy dictionary, two dictionaries are not the same dictionary. such as D = {1:1,2:2},d1 = d.copy ()->d1 = {1:1,2:2}.
CMP (DICT1,DICT2): Comparison dictionary, (priority is the number of elements, key size, key value size), the first large return 1, the first small return 1, as large as the return 0.
Four, strings (string)
1. Feature: A sequence of characters that cannot be modified. In addition to being unable to modify, the string can be treated as a list.
2. Access: Accessing a character in a string descriptor the same as an element in a tuple or list, the term "shard (slicing)". For example: str = "astring", str[0] = "A", str[1] = "S".
This is not an in-depth discussion of strings, and putting strings here is mainly to illustrate some of the characteristics of strings that have lists.
V. Collection (SET)
1. Features: Similar to a dictionary, but contains only keys, and no corresponding value, contains the data is not duplicated.
2. Create: s = set (list or tuple or string). Duplicate values exist only one in the collection. Such as:
List list:s = set ([1,2,3,3])->s = set ([])
Tuple tuple:s = set ((+))->s = set ([])
String string:s = Set ("abc")->s = set (["A", "B", "C"])
Transferred from: http://www.cnblogs.com/heyongqi/p/5497775.html
Python Basic types: tuples, lists, dictionaries, strings, set grooming summary