I believe there was a foundation python Students with job interview experience , Must have been asked about the list 、 Knowledge of dictionaries and tuples . The most frequently asked questions are those commonly used in these three data structures API, Or give you some questions on the spot , Through these data structures coding Realization , There are many network resources for these contents , I won't introduce them here . This article mainly explains the main differences between the three and the nine key points of mastering them !
L = [1,'a',1.234,[1,2,3],(4,5,6)]
print(L)
Output
[1, 'a', 1.234, [1, 2, 3], (4, 5, 6)]
L = [1,'a',1.234,[1,2,3],(4,5,6)]
print(L[3])
Output
[1, 2, 3]
Changing the values of a list is easy
L1=[1,2,3]
L1[1]=10
print(L1)
Output
[1, 10, 3]
When we try to change the value in a tuple
L2=('a','b','c')
L2[1]='x'
print(L2)
Output error prompt
L2[1]='x'
TypeError: 'tuple' object does not support item assignment
dic={'name':'kevin','age':40}
print(dic)
Output the whole dictionary {'name':'kevin','age':40}
print(dic['name'])
Output key by name Value : 'kevin'
print(dic.values())
Output all values of the dictionary ['kevin',40]
print(dic.keys())
Output all the keys of the dictionary ['name','age']