Python 简明教程

Python - Overview

Python 是高级、解释型、交互性和面向对象的脚本语言。Python 的设计具有很高的可读性。它频繁地使用英语关键字,而其他语言则使用标点符号,并且它的句法结构比其他语言少。

  1. Python is Interpreted − Python 由解释器在运行时处理。在执行程序之前,无需编译程序。这类似于 PERL 和 PHP。

  2. Python is Interactive − 您实际上可以坐在 Python 提示符下,直接与解释器进行交互,以编写程序。

  3. Python is Object-Oriented − Python 支持面向对象风格或代码封装在对象中的编程技术。

  4. Python is a Beginner’s Language − Python 是初学者程序员的出色语言,并且支持从简单文本处理到 WWW 浏览器再到游戏的广泛应用程序开发。

Python 是一种开源跨平台编程语言。它可以在所有主流操作系统平台 Linux、Windows 和 Mac OS 上使用(与 GNU 通用公共许可证兼容)。

为了促进新功能并保持可读性,开发了 Python 增强提案 (PEP) 流程。这个流程允许任何人提交一个 PEP,用于新的功能、库或其他添加项。

Python 的设计理念强调简单、可读性和明确性。Python 以其包含电池的方法而闻名,因为 Python 软件随附的标准函数和模块库非常全面。

Python 的设计理念在中得到记录。它包含十九条格言,例如 -

  1. 优美胜于丑陋

  2. 明确胜于含蓄

  3. 简单胜于复杂

  4. 复杂胜于繁琐

要获取完整的 Python Zen 文档,请在 Python Shell 中键入 import this

>>>import this

这将产生以下 19 条格言 -

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Python 支持命令式、结构化和面向对象编程方法。它还提供函数式编程的功能。

Pythonic Code Style

Python 让您自由选择以面向对象、过程化、功能性、面向方面,甚至以面向逻辑的方式编程。这些自由使 Python 成为编写干净且优美的代码的出色语言。

Python 代码样式实际上更像是一种设计理念,并且建议编写具有以下特征的代码:

  1. Clean

  2. Simple

  3. Beautiful

  4. Explicit

  5. Readable

The Zen of Python

Python 的 Zen 不仅与有效工作的代码有关,还与 Pythonic 代码有关。Pythonic 代码具有可读性、简洁性和可维护性。