1. A small problem:
Deleting a list element during a loop will be different from the expected result, but will not cause an error
Lis =[11,22,33,44,55] forIinchRange (len (LIS)):#When you use range () for the first time, the value is saved by default, and the value in the subsequent loop value is fixed to dead. Print(i)delLis[i]Print(LIS)delLis[1::2]Print(LIS) forIinchRange (0, 2): Print(I,type (i))delLis[i]Print(LIS)
View Code
For dictionary dict: Cannot delete dictionary key value pair during loop, error
DIC = {'K1':'v1','K2':'v2','A3':'v3'} forIinchDIC:if 'k' inchI:delDic[i]
Correct procedure 1
Dic1 = {} for in dic: if'k' is not inch I: = Dic1print(DIC1)
Correct Practice 2: Put the deletion to the list, the loop list to delete
DIC = {'K1':'v1','K2':'v2','A3':'v3'}l= [] forIinchDIC:if 'k' inchi:l.append (i) forIinchL:delDic[i]Print(DIC)
2. Tuple issues (Interview questions)
TU1 = (1= (2)print(Tu1,type (TU1)) # 1 <class ' int ' >Print (Tu2,type (TU2)) # (2,) <class ' tuple ' >= ([1= ([1],) Print(Tu1,type (TU1)) # [1] <class ' list ' >print(TU2, Type (TU2)) # ([1],) <class ' tuple ' >
Python basic three-----Basic data type Summary supplement