开始使用免费开始使用

扩展列表

如果您能修改列表中的元素,当然也希望能往列表中添加元素,对吗?您可以使用 + 运算符:

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

您刚中了彩票,太棒了!您决定建一个 poolhouse 和一个车库。能把这些信息添加到 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 = ____
编辑并运行代码