開始使用免費開始

存取字典

只要字典的鍵設計得好,存取字典中的值就會很直覺。舉例來說,想從 europe 取得法國的首都,可以這樣寫:

europe['france']

這裡,'france' 是鍵,會回傳其對應的值 'paris'

本練習屬於課程

Python 中級

檢視課程

練習說明

  • 叫用 europekeys() 方法,看看 europe 中有哪些鍵,並把結果印出來。
  • 印出鍵 'norway' 對應的值。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Definition of dictionary
europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }

# Print out the keys in europe


# Print out value that belongs to key 'norway'
編輯並執行程式碼