学习 Python 的新游乐场和速查表

2025-06-07

学习 Python 的新游乐场和速查表

我正在学习 Python,我决定创建一个存储库,在其中放置带有标准 Python 语法、结构和语句示例的 Python 脚本示例,以便能够快速回顾如何使用该语言的这个或那个功能。

这是我所得到的

这是按主题划分的 Python 脚本集合,包含 带有解释的代码示例、不同的用例和进一步阅读的链接。

它是一个游乐场,因为你可以修改或添加代码来了解其工作原理,并使用断言进行测试。它还允许你对编写的代码进行 lint 测试,并检查其是否符合 Python 代码风格指南。总而言之,它可以让你的学习过程更具互动性,并帮助你从一开始就保持较高的代码质量。

它之所以被称为速查表,是因为当你想回顾标准 Python 语句和结构的语法时,可能会回头看看这些代码示例。此外,由于代码中充满了断言,你无需启动函数/语句就能立即看到预期的输出。

如何使用 Repo

存储库中的每个 Python 脚本都具有以下结构:

"""Lists  <--- Name of the topic here

# @see: https://www.learnpython.org/en/Lists  <-- Link to further readings goes here

Here might go more detailed explanation of the current topic (i.e. general info about Lists).
"""


def test_list_type():
    """Explanation of sub-topic goes here.

    Each file contains test functions that illustrate sub-topics (i.e. lists type, lists methods).
    """

    # Here is an example of how to build a list.  <-- Comments here explain the action
    squares = [1, 4, 9, 16, 25]

    # Lists can be indexed and sliced. 
    # Indexing returns the item.
    assert squares[0] == 1  # <-- Assertions here illustrate the result.
    # Slicing returns a new list.
    assert squares[-3:] == [9, 16, 25]  # <-- Assertions here illustrate the result.
Enter fullscreen mode Exit fullscreen mode

因此通常您可能需要执行以下操作:

  • 找到您想要学习或回顾的主题。
  • 阅读每个脚本的文档字符串中链接的注释和/或文档(如上例所示)。
  • 查看代码示例和断言以了解使用示例和预期输出。
  • 更改代码或添加新的断言来查看工作原理。
  • 运行测试检查代码以查看其是否有效且是否正确编写。

目录

  1. 入门
  2. 运算符
  3. 数据类型
  4. 控制流
  5. 功能
  6. 课程
  7. 模块
  8. 错误和异常
  9. 文件
  10. 新增内容
  11. 标准库简介

我希望您发现这个存储库很有帮助!

文章来源:https://dev.to/trekhleb/new-playground-and-cheatsheet-for-learning-python-1k8p
PREV
排列/组合算法速查表
NEXT
🤖 交互式机器学习实验