_ Author _ = 'bill tsui' ''' Reading Notes for Python basic tutorial Version 2 (chapter 2) ''' # Python 3.3.4 # general sequence operation print ([0,] [0]) # index subscript start position 0 print, 4, 5, 6, 7, 8, 9] [-1]) #'9',-1 is the left element of the element whose subscript is 0, and-2 is the second to last, accordingly, print ([0, 1, 2, 4, 5, 6, 7, 8, 9] [0: 5]) # [0, 1, 2, 3, 4] slices [a: B], split from index a and retain the print ([,] [: 5]) elements after index B # [0, 1, 2, 3, 4] print ([0,] [-3:]) # [7, 8, 9] access to the last three elements. The ending position print ([,]) can be omitted only when the slice contains the last element. # [0, 2, 4, 6, 8] set the step size to print ([,] [10: 0:-2]) # [9, 7, 5, 3, 1] When start is greater than end, the negative step size ranges from right to left. Otherwise, it is [] print, 2, 3] + [, 6]) # print ([, 2, 3] * 5) # generate a new print ([None] * 10) sequence that repeats N times by serial multiplication # print (5 in [,]) an empty sequence with a length of 10) # print (len ([,]) for element membership check # print sequence length (max ([,]) # sequence maximum value print (min ([,]) # sequence minimum value # list read/write sequence print (list ('hello') # create a list from a string, it is equivalent to the list class constructor. in fact, list () applies to creating a list from all sequences newList = [0, 1, 2, 3, 4, 5]; newList [0] = 1; # change the value of the List del newList [0] # Delete the element newList [: 3] = [10, 11, 12] # assign values to slices in the list. assign values to newList [: 3] = [99,100,101,102,103] # number = []; number [] = [2, 3, 4] # A new value number is inserted when values are assigned to an empty slice [: 3] = [] # assign an empty list to the slice to delete the slice del number [: 3] # You can also delete the slice del number [:: 2] # Delete slices with Step 2 [0, 1, 2, 3]. append (4) # Add the element [,] to the original list. count (0) # count the number of times an element appears in the list [0, 1, 2, 3]. extend ([, 6]) # extend the list based on the original list. different from +, extend modifies the value of the original list [0, 1, 2, 3, 4, 5, 6, 7, 8]. index (5) # Return the index location of the first matched item in the list. If no index is found, an exception [,] is thrown. insert () # insert a new value [,] at the specified index. pop (2) # Remove the element at the index and return its value. By default, the last element [, 3] is removed. remove (0) # remove the first matched element [0, 1, 2, 3, 4, 5, 6, 8, 9]. reverse () # store the elements in the list in reverse direction [,]. sort () # sort the list using a fixed algorithm ["add", "addd", "dd"]. sort (key = len, reverse = True) # sort the list using the key function # tuples read-only sequence 1, 2, 3 # Use commas to separate the tuples (1, 2, 3) created by some values) # In fact, you can use () to create a tuples () # Empty tuples (1,) # single-element tuple ([, 3]) # create tuples from sequences, similar to the constructor of the tuple class, tuple () is applicable to the creation of tuples from all sequences # The meaning of the existence of tuples: 1. Used as the key in the ing; 2. Used as the internal construction method and function return value