My Python introductory notes (8)

Five chapters, tuples


Readability counts.——The Zen of Python


  I. Overview of tuples

  Tuple (tuple) is another important sequences in Python structure, it is similar to the list, they are arranged in a series of elements in a particular order, but it is immutable sequence. Tuple is also known as immutable list. Formally, all elements of the tuple are placed one pair of "()" in use between two adjacent elements comma "," split. The content of any type of integer, real, string, lists, etc. can be tuples into the tuple and a tuple in the same, may be different types of elements. Typically, the tuple for program content in unmodifiable. Definition and from the list of tuples of view, these two structures is quite similar, the main difference between them is tuples are immutable sequence, i.e. tuple element can be individually modified. The list is a variable sequence, the list element can be modified.

  Second, the creation and deletion of tuples

  1. Use the assignment operators to create a tuple

  Like other types of variable Python, when creating a tuple, assignment operator can be used "=" a tuple directly assigned to the variable, syntax is as follows:

  tuplename=(element1,element2,element3,...,enement n)

  tuplename: name of the tuple

  element1, element n: element tuple, the tuple is no limit to the number of elements.

1 1 NUM = (220,284,1210,1184 )
 2 2 str1 = ( ' element 1 ' , ' elements of two ' , ' rain rainbow students ' )
 3 3 Unit = ( ' Python ' , 1210, ( ' Life is short, I using the Python ' ))
 . 4 . 4 Print (type (NUM), type (str1), type (Unit))
 . 5 . 5 outputs:
 . 6 . 6 < class  ' tuple ' > < class  ' tuple ' > < class  'tuple'>

  In Python, though tuple is a pair of parentheses to enclose all the elements, but in fact, the parentheses is not necessary, simply a set of values ​​separated by a comma, Python can think he's yuan group.

. 1 the In [. 1]: = tuple 1210,1184, ' this is the test text ' , [220,284 ]
 2  
. 3 the In [2 ]: type (tuple)
 . 4 Out [2]: tuple

  If you need to create a tuple contains only one element, you need to add an element in the back "," otherwise it will output a string. Examples are as follows:

. 1 NUM = (220 ,)
 2 num2 = ( ' What is it ' )
 . 3  Print (type (NUM), type (num2))
 . 4  Output:
 . 5 < class  ' tuple ' > < class  ' STR ' >

  2. Create an empty tuple

  In Python, you can create a null set, for example:

  = emptytuple ()    emptytuple: namespace tuple

  A null set may return a null value or a null value for the transfer function. For example, define a function must pass a tuple type of value, but we also do not want to pass a set of data for it, then you can create an empty tuple is passed to it.

  3. Create value tuples

  In Python, you may be used tuple () function directly range function () results converted into a numerical rotated out tuple. The syntax is as follows:

  tuple (Data)   Data: can be converted to data tuples, which may be a range type objects, strings, or other iteration tuple types of data, for example: 

1 num = tuple(range(10,20,3))
2 print(type(num),num)
3 输出:
4 <class 'tuple'> (10, 13, 16, 19)

  Use tuple () function not only through the range object creation tuples, tuples can also create other objects.

  4. Remove the tuple

  For tuples already created, when not in use, you can use the del statement to delete syntax is as follows:

  typlename del   tuplename: To delete the name of the tuple

  del statement at the time of the actual development, not commonly used, because Python comes with garbage collection mechanism is automatically destroyed tuple unused.

  Third, access tuple element

  May be used in Python print () statements directly outputting content tuple in tuple output, comprising the left and right sides parentheses, if not all of the output element, the element can also be obtained by the specified index tuple. When the output of a single element, not including parentheses, if the string, not including left quotation marks, in addition tuple further sections may employ or specified element. Examples are as follows: 

. 1 NUM = (10, ' test text ' , 3, ' life is short, I use the Python ' )
 2 num2 = NUM [. 1 ]
 3 num3 = NUM [: 3 ]
 . 4  Print (type (num2), num2, type ( num3), num3)
 . 5  outputs:
 . 6 < class  ' STR ' > testing text < class  ' tuple ' > (10, ' test text ' , 3)

  Fourth, the modified tuple element

  Tuple immutable sequence, the individual elements can not modify it, but can be reassigned tuples. Examples are as follows:

. 1 NUM = (10, ' test text ' , 3, ' life is short, I use the Python ' )
 2 NUM = (103, ' life is short, I use the Python ' )
 3  Print (NUM)
 . 4  Output:
 5 (103 , ' life is short, I use Python ' )

  Further, the composition may also be connected to the tuple, for example:

. 1 NUM = (10, ' test text ' , 3, ' life is short, I use the Python ' )
 2  Print ( " Initial Ganso: ' , NUM)
 3 num2 = NUM + ( ' rain Ni students is Shadiao ' ,)
 . 4  Print ( ' combination of tuples: ' , num2)
 . 5  output:
 6 initial tuples: (10, ' test text ' , 3, ' life is short, I use the Python ' )
 . 7 composition tuple: (10, ' test text ' , 3 'Life is short, I used Python ', ' Rain rainbow is a sand sculpture students ' )

  During tuple connectivity, content must be connected to all tuples, tuples and strings or lists can not be connected, in addition tuple connection is only one element, the element must not forget the back, otherwise it will error .

  V. derivation tuple

  Tuple derivation can quickly generate a tuple, its manifestations similar list derived type, except that the list in parentheses comprehension "[]" with parentheses "()." For example, containing 10 to generate a random number of tuples, for example:

. 1  Import random    # introducing random standard library 
2 randomnum = (the random.randint (10,100) for I in Range (10 )) 
 . 3 randomnum2 = tuple (randomnum)
 . 4  Print ( " generated tuple: " , randomnum)   # is converted to tuple 
. 5  Print ( " converted tuple: " , randomnum2)
 . 6  output:
 7 generated tuple: <Generator Object <genexpr> AT 0x000002051973E228>
 . 8 converted tuple: (48, 92, 91, 67 , 56, 17, 66, 74, 82, 37)

  Tuple derivation results generated or not a list of tuples, but an object is generated, and this list is different derivation type is required to generate the object which can be converted to a list of tuples or converted to list use tuple () function, into a list using the list () function.

  The difference between six and a list of tuples

  Tuples and lists are all sequences, and they can store a set of elements in a particular order, is not limited types, the distinction between them mainly in the following points:

  1. The variable sequence part of the list, its elements can be modified or deleted at any time; the tuples belonging immutable sequence, wherein the elements can not be modified unless the overall replacement.

  2. lists can append (), extend (), insert (), remove () and pop () method implementation like add and edit the list of elements, but not the list of these methods, because they can not add to and modify elements tuple, The same can not delete list elements.

  3. List the elements can be used to access and modify sections in the list; although tuple also supports slices, but it only supports slicing through the access list elements can not be modified.

  4. tuple and processing speed faster than the access list, so if only the elements which are accessible, without modification, have suggested using tuples.

  The list can not keys of a dictionary, and can tuple.

Guess you like

Origin www.cnblogs.com/1210x1184/p/11074470.html