操作 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