張佑維Python字典Dictionaries

w3scholls截圖

w3scholls練習程式碼

#字典 keys:values, 張佑維
#w3schools原來 字串:字串,改成 整數:字串
a =	{   #市場價值最大的五家公司
  2330: "台積電",
  2317: "鴻海",
  2454: "聯發科",
  2412: "中華電",
  6505: "台塑化"
  }
print(a)
print(a[6505])
print(a.get(2330))#功能同 a[2330]
print(a.keys())   #keys()方法列出key搜尋鍵
print(a.values()) #keys()方法列出values值
b = a.copy()
print("列出b " + str(b))
print(b[2317])
print(b.clear())
a.update({2308: "台達電"})
print(a.values())
print("用迴圈列出字典a的所有值")
for t in a:
   print(a[t])

字典方法Dictionary Methods

Python has a set of built-in methods that you can use on dictionaries.
MethodDescription
clear()Removes all the elements from the dictionary
copy()Returns a copy of the dictionary
fromkeys()Returns a dictionary with the specified keys and value
get()Returns the value of the specified key
items()Returns a list containing a tuple for each key value pair
keys()Returns a list containing the dictionary's keys
pop()Removes the element with the specified key
popitem()Removes the last inserted key-value pair
setdefault()Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update()Updates the dictionary with the specified key-value pairs
values()Returns a list of all the values in the dictionary

教學影片216

教學影片217

教學影片218

留言

這個網誌中的熱門文章

張佑維python,print,input,字串方法[::-1]

張佑維Entry輸入文字變數textvariable字型font