Original articles, welcome to reprint. Reprint Please specify: Reproduced from Cheung's blog
Original link: http://blog.csdn.net/humanking7/article/details/45276769 python common combination data type 1. Sequence type 1.1. List: Use [] to create, can be changed (change the content after the ID unchanged), support the modification, different from the string and integral type. such as:
>>list1 = [' This ', ' is ', ' a ', ' pig ', '. ']
>>list1[0]
>> ' This '
>>list1[0][0]
>> ' T '
1.2. Tuples: Use () to create, can not be changed, in addition to the list is no different. such as:
>>t1 = ("This", "is", "a", "Pig", ".")
>>t1[0]
>> ' This '
>>t1[0][0]
>> ' T '
1.3. String: Also belongs to the sequence type, and can be sliced operation.
>>name = ' Jerry '
>>name[0]
' j '
>>name[0:1] #后面位置不显示
' j '
>>name[ 0:2]
' JE '
>>name[:2] #从0开始, if you do not write
' je '
>>name[2:] #到结尾, if you do not write
' Rry '
>>name[0:4]
' Jerr '
>>name[0:4:2] #第3个数字表示了步进长度为2
' JR '
Note: The slice itself creates a new memory object, because the string itself is not variable, so the slice itself is the new object. The original film itself has not changed anything. 2. Collection type:
set: A piece of data is put together in a haphazard, no concept of order, and it also removes duplicate data . 3. Mapping Type:
Dictionary: A bit like a key-value pair, K-v set
>>dic1 = {' x ': #, ' y ': 27.49, ' z ':
>>dic1[' x ']
>>dic2 = {1:32, 2:27.49, 3:65}
>>dic2[2]
27.49
>>print "% (x) -10d% (y) 0.3g"%dic1 #print用法见后面 27.5
Note that the dictionary is a mutable object and a tuple is an immutable sequence. The combination data type is also an object, so it can be nested, such as: [' Hello ', ' world ', [1,2,3]] in essence, lists and tuples do not actually store data, but rather hold object reference tuples, lists, and string data types that are "size", i.e., Its length can be used in a built-in function len () to measure the Append () method in a specific "method" list that a Python object can have its invocation can be used to supplement the new element