#lists: Lists can be nested. word=['a','b','C']a=WORD[2]#accessing the list by indexPrint "A is:"+AA is: Cword=['a','b','C','D','e','F','g']b=word[1:3]#take elements starting from 1 to 3Printb['b','C']b=word[1:2]Printb['b']PrintWord[1:4]#take elements starting from 1 to 4['b','C','D']PrintWord[2:3]word[-1]#-1 Table CountdownAA=A[-4:-1]#countdown Intercept, the result is a rowDelete the first value of a listdelsample_list[0] Insert a value in the list sample_list[0:0]= ['Sample Value'] Get the length of the list list_length=len (sample_list) List traversal forElementinchsample_list:Printmethod for list (element) L.append (Var)#Append ElementL.insert (Index,var) L.pop (Var)#returns the last element and removes it from the listL.remove (Var)#Delete the first occurrence of the elementL.count (Var)#The number of occurrences of the element in the listL.index (Var)#the position of the element, without throwing an exceptionL.extend (list)#Append list, that is, merge list to LL.sort ()#SortL.reverse ()#ReverseList operator:, +,*, keyword dela[1:]#fragment operator for extraction of sub-list[1,2]+[3,4]#to [1,2,3,4]. With Extend ()[2]*4#to [2,2,2,2]delL[1]#Delete the element that specifies the subscriptdelL[1:3]#Delete the element that specifies the subscript rangecopy of List L1= L#L1 is the alias of L, in C is the same pointer address, the L1 operation is the operation of L. This is how the function arguments are passed.L1 = l[:]#L1 is a clone of L, another copy. tuples: Tuples are immutable, tuples can be nested atuple= (A.'Hello') Atuple (1, 2, 3,'Hello') atuple[0]1atuple[-1] 'Hello'atuple[2:] (3,'Hello') atuple[:2] (1, 2) Atuple[0:3] (1, 2, 3) atuple[0]=5#Error forElementinchatuple:Printelement#Dictionary: There is no order for key/value pairs in the dictionaryx={'a':'AAA','b':'BBB','C': 12}Printx['a']aaaPrintx['C']12from Python2.2version from Fdict= Dict ((['x', 1], ['y', 2]) can be a number, a tuple is a key, but not a list, the dictionary is a key, the key must be immutable element a={1:11}b={2:22}x[a]=11111#Errorx[b]=22222Dictionary added: x['D']='DDD'#Dictionary Modificationx['D']='dddd'#Dictionary Delete#del x[' a ']#x.clear ()B=x.pop ('b') forKeyinchx:Print("%s=%s"%(Key,x[key]))#----------------Set-----------------------Setset is like pulling the key out of the dict, similar to a list, but the content cannot be duplicated, creating s by calling the set () method= Set (['A','B','C']) S.add ('Hello') S.add ('SS') S.add ('Hello') S.remove ('Hello') S.update ('ZW')#is to split the element to be passed in as an individual into the collection forKeyinchS:PrintKey#---------------Convert each other----------------------#1. DictionariesDict = {'name':'Zara',' Age': 7,'class':' First'}#dictionary converted to string, return: <type ' str ' > {' age ': 7, ' name ': ' Zara ', ' class ' : ' First '}Printtype (str (dict)), str (dict)#dictionaries can be converted to tuples, returning: (' age ', ' name ', ' class ')Printtuple (dict)#dictionaries can be converted to tuples, returning: (7, ' Zara ', ' first ')Printtuple (Dict.values ())#dictionary to list, return: [' age ', ' name ', ' class ']Printlist (dict)#Dictionary to listPrintdict.values ()#2, meta-grouptup= (1, 2, 3, 4, 5)#tuple to String, return: (1, 2, 3, 4, 5)PrintTup.__str__()#tuple to list, return: [1, 2, 3, 4, 5]Printlist (tup)#tuples cannot be converted to dictionaries#3. ListNums=[1, 3, 5, 7, 8, 13, 20];#list to String, return: [1, 3, 5, 7, 8, , +]PrintStr (nums)#list to tuple, return: (1, 3, 5, 7, 8, million)Printtuple (nums)#list cannot be converted to dictionary#4. String#string to tuple, return: (1, 2, 3)PrintTuple (eval ("(a)"))#string to list, return: [1, 2, 3]PrintList (eval ("(a)"))#string converted to dictionary, return: <type ' dict ' >PrintType (eval ("{' name ': ' Ljq ', ' age ':"))
Python notes (2)