開始使用免費開始

擴展串列

既然你可以修改串列中的元素,那麼自然也會希望能在串列中新增元素,對吧?使用 + 運算子就能完成:

x = ["a", "b", "c", "d"]
y = x + ["e", "f"]

你中了樂透,太棒了!你決定蓋一個泳池小屋 (poolhouse) 和一個車庫 (garage)。你能把這些資訊新增到 areas 串列中嗎?

本練習屬於課程

Python 入門

檢視課程

練習說明

  • 使用 + 運算子將串列 ["poolhouse", 24.5] 拼接到 areas 串列末尾。將結果串列儲存到 areas_1 中。
  • 繼續擴展 areas_1,新增關於車庫的資料。新增字串 "garage" 和浮點數 15.45。將結果串列命名為 areas_2

動手互動練習

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

# Create the areas list and make some changes
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0,
         "bedroom", 10.75, "bathroom", 10.50]

# Add poolhouse data to areas, new list is areas_1
areas_1 = ____

# Add garage data to areas_1, new list is areas_2
areas_2 = ____
編輯並執行程式碼