学习Python的操场和小抄
这是按以下方式拆分的Python脚本的集合topics并包含代码示例,其中包含解释、不同的用例和指向更多阅读资料的链接
阅读这篇文章Português
这是一个操场因为您可以更改或添加代码以查看它是如何工作的,并且test it out使用断言。它还允许您lint the code您已经编写并检查它是否符合Python代码样式指南。总之,它可能会使您的学习过程更具交互性,并且可能会帮助您从一开始就保持相当高的代码质量
这是一个小抄因为一旦您想要重述的语法,您可能会返回到这些代码示例standard Python statements and constructions另外,因为代码充满了断言,所以您将能够立即看到预期的函数/语句输出,而无需启动它们
您可能还会对以下内容感兴趣
🤖 Interactive Machine Learning Experiments
如何使用此存储库
此存储库中的每个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.
因此,通常您可能需要执行以下操作:
- Find the topic你想学习还是复习
- 阅读每个脚本的文档字符串中链接的注释和/或文档(如上例所示)
- 查看代码示例和断言以查看用法示例和预期输出
- 更改代码或添加新断言以查看工作原理
- Run tests和lint the code以查看它是否正常工作并且写入是否正确
目录
- 快速入门
- 操作员
- Arithmetic Operators(+,-,*,/,//,%,**)
- Bitwise Operators(&,|,^,>>,<<,~)
- Assignment Operators(=,+=,-=,/=,//=等)
- Comparison Operator(==,!=,>,<,>=,<=)
- Logical Operators(and,or,not)
- Identity Operators(is,is not)
- Membership Operators(in,not in)
 
- Arithmetic Operators(
- 数据类型
- Numbers(包括布尔值)
- Strings以及他们的方法
- Lists及其方法(包括列表理解)
- Tuples
- Sets以及他们的方法
- Dictionaries
- Type Casting
 
- 控制流
- 功能
- Function Definition(def和return声明)
- Scopes of Variables Inside Functions(global和nonlocal声明)
- Default Argument Values
- Keyword Arguments
- Arbitrary Argument Lists
- Unpacking Argument Lists(*和**声明)
- Lambda Expressions(lambda声明)
- Documentation Strings
- Function Annotations
- Function Decorators
 
- Function Definition(
- 班级
- 模块
- 错误和异常
- Handling Exceptions(try声明)
- Raising Exceptions(raise声明)
 
- Handling Exceptions(
- 文件
- 添加内容
- The passstatement
- Generators(yield声明)
 
- The 
- 标准图书馆简介
- Serialization(json库)
- File Wildcards(glob库)
- String Pattern Matching(re库)
- Mathematics(math,random,statistics图书馆)
- Dates and Times(datetime库)
- Data Compression(zlib库)
 
- Serialization(
必备条件
安装Python
确保你有Python3 installed在您的机器上
您可能想要使用venv创建虚拟环境的标准Python库,并从本地项目目录安装和提供Python、pip和所有从属软件包,以避免扰乱系统范围的软件包及其版本
根据您的安装,您可能可以通过运行以下命令来访问Python3解释器python或python3对于pip包管理器也是如此-可以通过运行以下命令来访问它pip或pip3
您可以通过运行以下命令检查您的Python版本:
python --version
请注意,在此存储库中,每当您看到python假设它是Python3个
安装依赖项
通过运行以下命令安装项目所需的所有依赖项:
pip install -r requirements.txt
测试代码
测试使用以下工具进行pytest框架
通过使用添加文件和函数,您可以为自己添加新的测试test_前缀(即test_topic.py使用def test_sub_topic()内部函数)
要运行所有测试,请从项目根文件夹执行以下命令:
pytest
要运行特定测试,请执行:
pytest ./path/to/the/test_file.py
链接代码
PyLint
检查代码是否针对PEP 8样式指南请运行:
pylint ./src/
在Linter将检测到错误的情况下(即missing-docstring)您可能希望通过运行以下命令来阅读有关特定错误的更多信息:
pylint --help-msg=missing-docstring
Flake8
检查代码是否针对PEP 8样式指南请运行:
flake8 ./src
或者,如果您希望获得更详细的输出,可以运行以下命令:
flake8 ./src --statistics --show-source --count

