開始使用免費開始

操作 Dictionary(1)

如果你會存取 dictionary,也就能為它指定新值。要在 europe 中新增一組鍵值對,可以像這樣寫:

europe['iceland'] = 'reykjavik'

本練習屬於課程

Python 中級

檢視課程

練習說明

  • europe 中加入鍵 'italy',其值為 'rome'
  • 為了確認 'italy' 現在已是 europe 的鍵,印出 'italy' in europe
  • 再為 europe 新增一組鍵值對:'poland' 為鍵,'warsaw' 為對應值。
  • 印出 europe

動手互動練習

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

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

# Add italy to europe


# Print out italy in europe


# Add poland to europe


# Print europe
編輯並執行程式碼