“Python/docs/3.9/tutorial/index”的版本间差异
来自菜鸟教程
Python/docs/3.9/tutorial/index
(autoload) |
小 (Page commit) |
||
第1行: | 第1行: | ||
+ | {{DISPLAYTITLE:Python 教程 — Python 文档}} | ||
<div id="the-python-tutorial" class="section"> | <div id="the-python-tutorial" class="section"> | ||
<span id="tutorial-index"></span> | <span id="tutorial-index"></span> | ||
− | = | + | = Python 教程 = |
− | Python | + | Python 是一种易于学习、功能强大的编程语言。 它具有高效的高级数据结构和简单但有效的面向对象编程方法。 Python 优雅的语法和动态类型,加上它的解释性质,使其成为大多数平台上许多领域的脚本编写和快速应用程序开发的理想语言。 |
− | |||
− | |||
− | |||
− | |||
− | + | Python 解释器和广泛的标准库可以从 Python 网站 https://www.python.org/ 以源代码或二进制形式免费提供给所有主要平台,并且可以免费分发。 该站点还包含许多免费的第三方 Python 模块、程序和工具以及其他文档的分发和指针。 | |
− | |||
− | https://www.python.org/ | ||
− | |||
− | |||
− | + | Python 解释器很容易使用 C 或 C++(或其他可从 C 调用的语言)实现的新函数和数据类型进行扩展。 Python 也适合作为可定制应用程序的扩展语言。 | |
− | |||
− | |||
− | + | 本教程非正式地向读者介绍了 Python 语言和系统的基本概念和特性。 有一个 Python 解释器有助于实践经验,但所有示例都是独立的,因此本教程也可以离线阅读。 | |
− | |||
− | |||
− | |||
− | + | 有关标准对象和模块的说明,请参阅 [[../../library/index#library-index|Python 标准库]] 。 [[../../reference/index#reference-index|Python 语言参考]] 给出了更正式的语言定义。 要使用 C 或 C++ 编写扩展,请阅读 [[../../extending/index#extending-index|Extending and Embedding the Python Interpreter]] 和 [[../../c-api/index#c-api-index|Python/C API Reference Manual]]。 还有几本书深入介绍了 Python。 | |
− | [[../../reference/index#reference-index| | ||
− | |||
− | [[../../c-api/index#c-api-index| | ||
− | + | 本教程并不试图全面覆盖每一个功能,甚至每个常用功能。 相反,它介绍了 Python 的许多最值得注意的特性,并且会让您对这门语言的风格和风格有一个很好的了解。 阅读后,您将能够读写 Python 模块和程序,并且您将准备好进一步了解 [[../../library/index#library-index|Python 标准库]] 中描述的各种 Python 库模块。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | [[../../glossary#glossary|词汇表]]也值得一读。 | |
<div class="toctree-wrapper compound"> | <div class="toctree-wrapper compound"> | ||
− | * [[../appetite|1. | + | * [[../appetite|1. 开胃]] |
− | * [[../interpreter|2. | + | * [[../interpreter|2. 使用 Python 解释器]] |
− | ** [[../interpreter#invoking-the-interpreter|2.1. | + | ** [[../interpreter#invoking-the-interpreter|2.1. 调用解释器]] |
− | *** [[../interpreter#argument-passing|2.1.1. | + | *** [[../interpreter#argument-passing|2.1.1. 参数传递]] |
− | *** [[../interpreter#interactive-mode|2.1.2. | + | *** [[../interpreter#interactive-mode|2.1.2. 交互模式]] |
− | ** [[../interpreter#the-interpreter-and-its-environment|2.2. | + | ** [[../interpreter#the-interpreter-and-its-environment|2.2. 口译员及其环境]] |
− | *** [[../interpreter#source-code-encoding|2.2.1. | + | *** [[../interpreter#source-code-encoding|2.2.1. 源代码编码]] |
− | * [[../introduction|3. | + | * [[../introduction|3. Python 的非正式介绍]] |
− | ** [[../introduction#using-python-as-a-calculator|3.1. | + | ** [[../introduction#using-python-as-a-calculator|3.1. 使用 Python 作为计算器]] |
− | *** [[../introduction#numbers|3.1.1. | + | *** [[../introduction#numbers|3.1.1. 数字]] |
− | *** [[../introduction#strings|3.1.2. | + | *** [[../introduction#strings|3.1.2. 字符串]] |
− | *** [[../introduction#lists|3.1.3. | + | *** [[../introduction#lists|3.1.3. 列表]] |
− | ** [[../introduction#first-steps-towards-programming|3.2. | + | ** [[../introduction#first-steps-towards-programming|3.2. 编程的第一步]] |
− | * [[../controlflow|4. | + | * [[../controlflow|4. 更多控制流工具]] |
− | ** [[../controlflow#if-statements|4.1. <code>if</code> | + | ** [[../controlflow#if-statements|4.1. <code>if</code> 声明]] |
− | ** [[../controlflow#for-statements|4.2. <code>for</code> | + | ** [[../controlflow#for-statements|4.2. <code>for</code> 声明]] |
− | ** [[../controlflow#the-range-function|4.3. | + | ** [[../controlflow#the-range-function|4.3. <code>range()</code> 功能]] |
− | ** [[../controlflow#break-and-continue-statements-and-else-clauses-on-loops|4.4. <code>break</code> | + | ** [[../controlflow#break-and-continue-statements-and-else-clauses-on-loops|4.4. <code>break</code> 和 <code>continue</code> 语句,以及 <code>else</code> 循环子句]] |
− | ** [[../controlflow#pass-statements|4.5. <code>pass</code> | + | ** [[../controlflow#pass-statements|4.5. <code>pass</code> 声明]] |
− | ** [[../controlflow#defining-functions|4.6. | + | ** [[../controlflow#defining-functions|4.6. 定义函数]] |
− | ** [[../controlflow#more-on-defining-functions|4.7. | + | ** [[../controlflow#more-on-defining-functions|4.7. 有关定义函数的更多信息]] |
− | *** [[../controlflow#default-argument-values|4.7.1. | + | *** [[../controlflow#default-argument-values|4.7.1. 默认参数值]] |
− | *** [[../controlflow#keyword-arguments|4.7.2. | + | *** [[../controlflow#keyword-arguments|4.7.2. 关键字参数]] |
− | *** [[../controlflow#special-parameters|4.7.3. | + | *** [[../controlflow#special-parameters|4.7.3. 特殊参数]] |
− | **** [[../controlflow#positional-or-keyword-arguments|4.7.3.1. | + | **** [[../controlflow#positional-or-keyword-arguments|4.7.3.1. 位置或关键字参数]] |
− | **** [[../controlflow#positional-only-parameters|4.7.3.2. | + | **** [[../controlflow#positional-only-parameters|4.7.3.2. 仅位置参数]] |
− | **** [[../controlflow#keyword-only-arguments|4.7.3.3. | + | **** [[../controlflow#keyword-only-arguments|4.7.3.3. 仅关键字参数]] |
− | **** [[../controlflow#function-examples|4.7.3.4. | + | **** [[../controlflow#function-examples|4.7.3.4. 函数示例]] |
− | **** [[../controlflow#recap|4.7.3.5. | + | **** [[../controlflow#recap|4.7.3.5. 回顾]] |
− | *** [[../controlflow#arbitrary-argument-lists|4.7.4. | + | *** [[../controlflow#arbitrary-argument-lists|4.7.4. 任意参数列表]] |
− | *** [[../controlflow#unpacking-argument-lists|4.7.5. | + | *** [[../controlflow#unpacking-argument-lists|4.7.5. 解包参数列表]] |
− | *** [[../controlflow#lambda-expressions|4.7.6. Lambda | + | *** [[../controlflow#lambda-expressions|4.7.6. Lambda 表达式]] |
− | *** [[../controlflow#documentation-strings|4.7.7. | + | *** [[../controlflow#documentation-strings|4.7.7. 文档字符串]] |
− | *** [[../controlflow#function-annotations|4.7.8. | + | *** [[../controlflow#function-annotations|4.7.8. 函数注解]] |
− | ** [[controlflow | + | ** [[../controlflow#intermezzo-coding-style|4.8. 间奏曲:编码风格]] |
− | * [[../datastructures|5. | + | * [[../datastructures|5. 数据结构]] |
− | ** [[../datastructures#more-on-lists|5.1. | + | ** [[../datastructures#more-on-lists|5.1. 更多关于列表]] |
− | *** [[../datastructures#using-lists-as-stacks|5.1.1. | + | *** [[../datastructures#using-lists-as-stacks|5.1.1. 使用列表作为堆栈]] |
− | *** [[../datastructures#using-lists-as-queues|5.1.2. | + | *** [[../datastructures#using-lists-as-queues|5.1.2. 使用列表作为队列]] |
− | *** [[../datastructures#list-comprehensions|5.1.3. | + | *** [[../datastructures#list-comprehensions|5.1.3. 列表推导式]] |
− | *** [[../datastructures#nested-list-comprehensions|5.1.4. | + | *** [[../datastructures#nested-list-comprehensions|5.1.4. 嵌套列表理解]] |
− | ** [[../datastructures#the-del-statement|5.2. | + | ** [[../datastructures#the-del-statement|5.2. <code>del</code> 语句]] |
− | ** [[../datastructures#tuples-and-sequences|5.3. | + | ** [[../datastructures#tuples-and-sequences|5.3. 元组和序列]] |
− | ** [[../datastructures#sets|5.4. | + | ** [[../datastructures#sets|5.4. 套]] |
− | ** [[../datastructures#dictionaries|5.5. | + | ** [[../datastructures#dictionaries|5.5. 字典]] |
− | ** [[../datastructures#looping-techniques|5.6. | + | ** [[../datastructures#looping-techniques|5.6. 循环技术]] |
− | ** [[../datastructures#more-on-conditions|5.7. | + | ** [[../datastructures#more-on-conditions|5.7. 更多关于条件]] |
− | ** [[../datastructures#comparing-sequences-and-other-types|5.8. | + | ** [[../datastructures#comparing-sequences-and-other-types|5.8. 比较序列和其他类型]] |
− | * [[../modules|6. | + | * [[../modules|6. 模块]] |
− | ** [[../modules#more-on-modules|6.1. | + | ** [[../modules#more-on-modules|6.1. 更多关于模块]] |
− | *** [[../modules#executing-modules-as-scripts|6.1.1. | + | *** [[../modules#executing-modules-as-scripts|6.1.1. 将模块作为脚本执行]] |
− | *** [[../modules#the-module-search-path|6.1.2. | + | *** [[../modules#the-module-search-path|6.1.2. 模块搜索路径]] |
− | *** [[../modules#compiled-python-files|6.1.3. | + | *** [[../modules#compiled-python-files|6.1.3. “已编译”的 Python 文件]] |
− | ** [[../modules#standard-modules|6.2. | + | ** [[../modules#standard-modules|6.2. 标准模块]] |
− | ** [[../modules#the-dir-function|6.3. | + | ** [[../modules#the-dir-function|6.3. <code>dir()</code> 功能]] |
− | ** [[../modules#packages|6.4. | + | ** [[../modules#packages|6.4. 套餐]] |
− | *** [[../modules#importing-from-a-package|6.4.1. | + | *** [[../modules#importing-from-a-package|6.4.1. 从包中导入 *]] |
− | *** [[../modules#intra-package-references|6.4.2. | + | *** [[../modules#intra-package-references|6.4.2. 包内引用]] |
− | *** [[../modules#packages-in-multiple-directories|6.4.3. | + | *** [[../modules#packages-in-multiple-directories|6.4.3. 多个目录中的包]] |
− | * [[../inputoutput|7. | + | * [[../inputoutput|7. 输入和输出]] |
− | ** [[../inputoutput#fancier-output-formatting|7.1. | + | ** [[../inputoutput#fancier-output-formatting|7.1. 更高级的输出格式]] |
− | *** [[../inputoutput#formatted-string-literals|7.1.1. | + | *** [[../inputoutput#formatted-string-literals|7.1.1. 格式化字符串文字]] |
− | *** [[../inputoutput#the-string-format-method|7.1.2. | + | *** [[../inputoutput#the-string-format-method|7.1.2. String format() 方法]] |
− | *** [[../inputoutput#manual-string-formatting|7.1.3. | + | *** [[../inputoutput#manual-string-formatting|7.1.3. 手动字符串格式化]] |
− | *** [[../inputoutput#old-string-formatting|7.1.4. | + | *** [[../inputoutput#old-string-formatting|7.1.4. 旧的字符串格式]] |
− | ** [[../inputoutput#reading-and-writing-files|7.2. | + | ** [[../inputoutput#reading-and-writing-files|7.2. 读写文件]] |
− | *** [[../inputoutput#methods-of-file-objects|7.2.1. | + | *** [[../inputoutput#methods-of-file-objects|7.2.1. 文件对象的方法]] |
− | *** [[../inputoutput#saving-structured-data-with-json|7.2.2. | + | *** [[../inputoutput#saving-structured-data-with-json|7.2.2. 使用 <code>json</code> 保存结构化数据]] |
− | * [[../errors|8. | + | * [[../errors|8. 错误和异常]] |
− | ** [[../errors#syntax-errors|8.1. | + | ** [[../errors#syntax-errors|8.1. 语法错误]] |
− | ** [[../errors#exceptions|8.2. | + | ** [[../errors#exceptions|8.2. 例外]] |
− | ** [[../errors#handling-exceptions|8.3. | + | ** [[../errors#handling-exceptions|8.3. 处理异常]] |
− | ** [[../errors#raising-exceptions|8.4. | + | ** [[../errors#raising-exceptions|8.4. 引发异常]] |
− | ** [[../errors#exception-chaining|8.5. | + | ** [[../errors#exception-chaining|8.5. 异常链]] |
− | ** [[../errors#user-defined-exceptions|8.6. | + | ** [[../errors#user-defined-exceptions|8.6. 用户定义的异常]] |
− | ** [[../errors#defining-clean-up-actions|8.7. | + | ** [[../errors#defining-clean-up-actions|8.7. 定义清理操作]] |
− | ** [[../errors#predefined-clean-up-actions|8.8. | + | ** [[../errors#predefined-clean-up-actions|8.8. 预定义的清理操作]] |
− | * [[../classes|9. | + | * [[../classes|9. 班级]] |
− | ** [[../classes#a-word-about-names-and-objects|9.1. | + | ** [[../classes#a-word-about-names-and-objects|9.1. 关于名称和对象的一句话]] |
− | ** [[../classes#python-scopes-and-namespaces|9.2. Python | + | ** [[../classes#python-scopes-and-namespaces|9.2. Python 范围和命名空间]] |
− | *** [[../classes#scopes-and-namespaces-example|9.2.1. | + | *** [[../classes#scopes-and-namespaces-example|9.2.1. 范围和命名空间示例]] |
− | ** [[../classes#a-first-look-at-classes|9.3. | + | ** [[../classes#a-first-look-at-classes|9.3. 类的第一眼]] |
− | *** [[../classes#class-definition-syntax|9.3.1. | + | *** [[../classes#class-definition-syntax|9.3.1. 类定义语法]] |
− | *** [[../classes#class-objects|9.3.2. | + | *** [[../classes#class-objects|9.3.2. 类对象]] |
− | *** [[../classes#instance-objects|9.3.3. | + | *** [[../classes#instance-objects|9.3.3. 实例对象]] |
− | *** [[../classes#method-objects|9.3.4. | + | *** [[../classes#method-objects|9.3.4. 方法对象]] |
− | *** [[../classes#class-and-instance-variables|9.3.5. | + | *** [[../classes#class-and-instance-variables|9.3.5. 类和实例变量]] |
− | ** [[../classes#random-remarks|9.4. | + | ** [[../classes#random-remarks|9.4. 随机备注]] |
− | ** [[../classes#inheritance|9.5. | + | ** [[../classes#inheritance|9.5. 遗产]] |
− | *** [[../classes#multiple-inheritance|9.5.1. | + | *** [[../classes#multiple-inheritance|9.5.1. 多重继承]] |
− | ** [[../classes#private-variables|9.6. | + | ** [[../classes#private-variables|9.6. 私有变量]] |
− | ** [[../classes#odds-and-ends|9.7. | + | ** [[../classes#odds-and-ends|9.7. 什物]] |
− | ** [[../classes#iterators|9.8. | + | ** [[../classes#iterators|9.8. 迭代器]] |
− | ** [[../classes#generators|9.9. | + | ** [[../classes#generators|9.9. 发电机]] |
− | ** [[../classes#generator-expressions|9.10. | + | ** [[../classes#generator-expressions|9.10. 生成器表达式]] |
− | * [[../stdlib|10. | + | * [[../stdlib|10. 标准库简介]] |
− | ** [[../stdlib#operating-system-interface|10.1. | + | ** [[../stdlib#operating-system-interface|10.1. 操作系统接口]] |
− | ** [[../stdlib#file-wildcards|10.2. | + | ** [[../stdlib#file-wildcards|10.2. 文件通配符]] |
− | ** [[../stdlib#command-line-arguments|10.3. | + | ** [[../stdlib#command-line-arguments|10.3. 命令行参数]] |
− | ** [[../stdlib#error-output-redirection-and-program-termination|10.4. | + | ** [[../stdlib#error-output-redirection-and-program-termination|10.4. 错误输出重定向和程序终止]] |
− | ** [[../stdlib#string-pattern-matching|10.5. | + | ** [[../stdlib#string-pattern-matching|10.5. 字符串模式匹配]] |
− | ** [[../stdlib#mathematics|10.6. | + | ** [[../stdlib#mathematics|10.6. 数学]] |
− | ** [[../stdlib#internet-access|10.7. | + | ** [[../stdlib#internet-access|10.7. 互联网]] |
− | ** [[../stdlib#dates-and-times|10.8. | + | ** [[../stdlib#dates-and-times|10.8. 日期和时间]] |
− | ** [[../stdlib#data-compression|10.9. | + | ** [[../stdlib#data-compression|10.9. 数据压缩]] |
− | ** [[../stdlib#performance-measurement|10.10. | + | ** [[../stdlib#performance-measurement|10.10. 绩效衡量]] |
− | ** [[../stdlib#quality-control|10.11. | + | ** [[../stdlib#quality-control|10.11. 质量控制]] |
− | ** [[../stdlib#batteries-included|10.12. | + | ** [[../stdlib#batteries-included|10.12. 包括电池]] |
− | * [[../stdlib2|11. | + | * [[../stdlib2|11. 标准库简介——第二部分]] |
− | ** [[../stdlib2#output-formatting|11.1. | + | ** [[../stdlib2#output-formatting|11.1. 输出格式]] |
− | ** [[../stdlib2#templating|11.2. | + | ** [[../stdlib2#templating|11.2. 模板制作]] |
− | ** [[../stdlib2#working-with-binary-data-record-layouts|11.3. | + | ** [[../stdlib2#working-with-binary-data-record-layouts|11.3. 使用二进制数据记录布局]] |
− | ** [[../stdlib2#multi-threading|11.4. | + | ** [[../stdlib2#multi-threading|11.4. 多线程]] |
− | ** [[../stdlib2#logging|11.5. | + | ** [[../stdlib2#logging|11.5. 日志记录]] |
− | ** [[../stdlib2#weak-references|11.6. | + | ** [[../stdlib2#weak-references|11.6. 弱引用]] |
− | ** [[../stdlib2#tools-for-working-with-lists|11.7. | + | ** [[../stdlib2#tools-for-working-with-lists|11.7. 处理列表的工具]] |
− | ** [[../stdlib2#decimal-floating-point-arithmetic|11.8. | + | ** [[../stdlib2#decimal-floating-point-arithmetic|11.8. 十进制浮点运算]] |
− | * [[../venv|12. | + | * [[../venv|12. 虚拟环境和包]] |
− | ** [[../venv#introduction|12.1. | + | ** [[../venv#introduction|12.1. 介绍]] |
− | ** [[../venv#creating-virtual-environments|12.2. | + | ** [[../venv#creating-virtual-environments|12.2. 创建虚拟环境]] |
− | ** [[../venv#managing-packages-with-pip|12.3. | + | ** [[../venv#managing-packages-with-pip|12.3. 使用 pip 管理包]] |
− | * [[../whatnow|13. | + | * [[../whatnow|13. 现在怎么办?]] |
− | * [[../interactive|14. | + | * [[../interactive|14. 交互式输入编辑和历史替换]] |
− | ** [[../interactive#tab-completion-and-history-editing|14.1. | + | ** [[../interactive#tab-completion-and-history-editing|14.1. 标签完成和历史编辑]] |
− | ** [[../interactive#alternatives-to-the-interactive-interpreter|14.2. | + | ** [[../interactive#alternatives-to-the-interactive-interpreter|14.2. 交互式解释器的替代品]] |
− | * [[floatingpoint | + | * [[../floatingpoint|15. 浮点运算:问题和限制]] |
− | ** [[../floatingpoint#representation-error|15.1. | + | ** [[../floatingpoint#representation-error|15.1. 表示错误]] |
− | * [[../appendix|16. | + | * [[../appendix|16. 附录]] |
− | ** [[../appendix#interactive-mode|16.1. | + | ** [[../appendix#interactive-mode|16.1. 交互模式]] |
− | *** [[../appendix#error-handling|16.1.1. | + | *** [[../appendix#error-handling|16.1.1. 错误处理]] |
− | *** [[../appendix#executable-python-scripts|16.1.2. | + | *** [[../appendix#executable-python-scripts|16.1.2. 可执行的 Python 脚本]] |
− | *** [[../appendix#the-interactive-startup-file|16.1.3. | + | *** [[../appendix#the-interactive-startup-file|16.1.3. 交互式启动文件]] |
− | *** [[../appendix#the-customization-modules|16.1.4. | + | *** [[../appendix#the-customization-modules|16.1.4. 自定义模块]] |
第179行: | 第159行: | ||
</div> | </div> | ||
+ | <div class="clearer"> | ||
− | [[Category:Python 3.9 | + | |
+ | |||
+ | </div> | ||
+ | |||
+ | [[Category:Python 3.9 文档]] |
2021年10月31日 (日) 04:54的最新版本
Python 教程
Python 是一种易于学习、功能强大的编程语言。 它具有高效的高级数据结构和简单但有效的面向对象编程方法。 Python 优雅的语法和动态类型,加上它的解释性质,使其成为大多数平台上许多领域的脚本编写和快速应用程序开发的理想语言。
Python 解释器和广泛的标准库可以从 Python 网站 https://www.python.org/ 以源代码或二进制形式免费提供给所有主要平台,并且可以免费分发。 该站点还包含许多免费的第三方 Python 模块、程序和工具以及其他文档的分发和指针。
Python 解释器很容易使用 C 或 C++(或其他可从 C 调用的语言)实现的新函数和数据类型进行扩展。 Python 也适合作为可定制应用程序的扩展语言。
本教程非正式地向读者介绍了 Python 语言和系统的基本概念和特性。 有一个 Python 解释器有助于实践经验,但所有示例都是独立的,因此本教程也可以离线阅读。
有关标准对象和模块的说明,请参阅 Python 标准库 。 Python 语言参考 给出了更正式的语言定义。 要使用 C 或 C++ 编写扩展,请阅读 Extending and Embedding the Python Interpreter 和 Python/C API Reference Manual。 还有几本书深入介绍了 Python。
本教程并不试图全面覆盖每一个功能,甚至每个常用功能。 相反,它介绍了 Python 的许多最值得注意的特性,并且会让您对这门语言的风格和风格有一个很好的了解。 阅读后,您将能够读写 Python 模块和程序,并且您将准备好进一步了解 Python 标准库 中描述的各种 Python 库模块。
词汇表也值得一读。
- 1. 开胃
- 2. 使用 Python 解释器
- 3. Python 的非正式介绍
- 4. 更多控制流工具
- 5. 数据结构
- 6. 模块
- 7. 输入和输出
- 8. 错误和异常
- 9. 班级
- 10. 标准库简介
- 11. 标准库简介——第二部分
- 12. 虚拟环境和包
- 13. 现在怎么办?
- 14. 交互式输入编辑和历史替换
- 15. 浮点运算:问题和限制
- 16. 附录