程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python interview high frequency questions: 9 key points for mastering lists, dictionaries and tuples

編輯:Python

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 !

list

  • 1. Different types of objects can be stored in the list

L = [1,'a',1.234,[1,2,3],(4,5,6)]

print(L)

Output

[1, 'a', 1.234, [1, 2, 3], (4, 5, 6)]

  • 2. A list is an ordered queue , You can get a single element by position sequence number

L = [1,'a',1.234,[1,2,3],(4,5,6)]

print(L[3])

Output

[1, 2, 3]

  • 3. The list has unlimited length , That's not enough

Tuples

  • 4. Tuples can be thought of as immutable lists , Tuples use braces ( ), Use square brackets for lists [ ].

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

  • 5. So you might ask , Why design tuples ? It's simple , Tuples occupy less memory resources than lists !

Dictionaries

  • 6. A dictionary is a hash table , The objects in the dictionary are out of order .
  • 7. Dictionaries reflect a mapping relationship through pairs of key values .
  • 8. Every key in the dictionary is unique .

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']

  • 9. You may ask again , Why design a dictionary ? It's simple , When a large amount of data processing is involved , Dictionaries are much more efficient than lists !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved