使用 %timeit:正式名称还是字面量语法
Python 允许您使用「正式名称」或「字面量语法」两种方式来创建数据结构。本练习将带您比较使用字面量语法创建数据结构是否能缩短运行时间。
| data structure | formal name | literal syntax |
|---|---|---|
| list | list() |
[] |
| dictionary | dict() |
{} |
| tuple | tuple() |
() |
本练习是课程的一部分
高效编写 Python 代码
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a list using the formal name
formal_list = ____
print(formal_list)
# Create a list using the literal syntax
literal_list = ____
print(literal_list)