Python 简明教程
Python - Features
Python 是一种功能丰富、高级、解释型、交互式且面向对象的脚本语言。Python 是一种多功能且非常流行的编程语言,因为它具有可读性、简单性、广泛的库以及更多功能等特性。在本教程中,我们将了解 Python 的各种特性,这些特性使其成为一种强大且多功能的编程语言。
Python is a feature-rich, high-level, interpreted, interactive, and object-oriented scripting language. Python is a versatile and very popular programming language due to its features such as readability, simplicity, extensive libraries, and many more. In this tutorial, we will learn about the various features of Python that make it a powerful and versatile programming language.
Python 最重要的特性如下:
Python’s most important features are as follows:
Easy to Learn
这是 Python 广受欢迎的一个最重要的原因。Python 关键字集合受限。其特性比如简单的 syntax 、使用缩进来避免花括号的混乱、无需预先声明变量的动态类型都能帮助新手快速轻松地学习 Python。
This is one of the most important reasons for the popularity of Python. Python has a limited set of keywords. Its features such as simple syntax, usage of indentation to avoid clutter of curly brackets and dynamic typing that doesn’t necessitate prior declaration of variable help a beginner to learn Python quickly and easily.
Dynamically Typed
Python 是一种动态类型编程语言。在 Python 中,在变量声明时无需指定变量类型。由于其动态类型特性,将在运行时根据赋值指定类型。
Python is a dynamically typed programming language. In Python, you don’t need to specify the variable time at the time of the variable declaration. The types are specified at the runtime based on the assigned value due to its dynamically typed feature.
Interpreter Based
任何编程语言中的指令都必须转换为机器码以便处理器执行它们。编程语言要么是基于编译器的,要么是基于解释器的。
Instructions in any programming languages must be translated into machine code for the processor to execute them. Programming languages are either compiler based or interpreter based.
在编译器的情况下,将生成整个源程序的 machine language 版本。即使一个语句有误,转换也会失败。因此,对于新手来说,开发流程非常繁琐。C 家族语言(包括 C 、 C++ 、 Java 、 C# 等)是基于编译器的。
In case of a compiler, a machine language version of the entire source program is generated. The conversion fails even if there is a single erroneous statement. Hence, the development process is tedious for the beginners. The C family languages (including C, C++, Java, C# etc) are compiler based.
Python 是一种基于解释器的语言。解释器一次从源代码中获取一条指令,将其转换为机器码并执行。在第一次出现错误之前,指令将被执行。通过此功能,可以更轻松地调试程序,从而证明这对初学者水平的程序员逐渐建立信心非常有用。因此,Python 是一种对初学者友好的语言。
Python is an interpreter based language. The interpreter takes one instruction from the source code at a time, translates it into machine code and executes it. Instructions before the first occurrence of error are executed. With this feature, it is easier to debug the program and thus proves useful for the beginner level programmer to gain confidence gradually. Python therefore is a beginner-friendly language.
Interactive
Python 标准发行版带有一个基于 REPL(读入 - 评估 - 打印 - 循环)原则的交互式 Shell。该 Shell 提供了一个 Python 提示符 >>>。可以键入任何有效的 Python 表达式并按 Enter。Python 解释器会立即返回响应,提示符返回以读取下一个表达式。
Standard Python distribution comes with an interactive shell that works on the principle of REPL (Read – Evaluate – Print – Loop). The shell presents a Python prompt >>>. You can type any valid Python expression and press Enter. Python interpreter immediately returns the response and the prompt comes back to read the next expression.
>>> 2*3+1
7
>>> print ("Hello World")
Hello World
交互模式对于熟悉库并测试其功能特别有用。你可以在编写程序之前在交互模式中尝试较小的代码片段。
The interactive mode is especially useful to get familiar with a library and test out its functionality. You can try out small code snippets in interactive mode before writing a program.
Multi-paradigm
Python 是一種完全面向 object-oriented 的語言。Python 程序中的所有內容都是 object 。不過,Python 將面向對象設計封裝起來,方便作為強制或程序語言(例如 C)使用。Python 也提供了一些類似函數式編程的功能。此外,已開發出某些第三方工具來支援其他編程範例,例如面向方面和邏輯編程。
Python is a completely object-oriented language. Everything in a Python program is an object. However, Python conveniently encapsulates its object orientation to be used as an imperative or procedural language – such as C. Python also provides certain functionality that resembles functional programming. Moreover, certain third-party tools have been developed to support other programming paradigms such as aspect-oriented and logic programming.
Standard Library
尽管它只有很少的关键词(仅三十五个),但 Python 软件仍发行有一个由大量模块和包组成的标准库。因此,Python 开箱即用地支持序列化、数据压缩、互联网数据处理等编程需求。Python 因其“附带电池”的方式而闻名。
Even though it has a very few keywords (only Thirty Five), Python software is distributed with a standard library made of large number of modules and packages. Thus Python has out of box support for programming needs such as serialization, data compression, internet data handling, and many more. Python is known for its batteries included approach.
以下是 Python 幾個常用的模組:
Some of the Python’s popular modules are:
-
Tkinter
Open Source and Cross Platform
可以从 https://www.python.org/downloads/ 下载 Python 标准发行版,没有任何限制。你可以下载针对各种操作系统平台的预编译二进制文件。此外,源代码也是免费提供的,这就是它属于开源范畴的原因。
Python’s standard distribution can be downloaded from https://www.python.org/downloads/ without any restrictions. You can download pre-compiled binaries for various operating system platforms. In addition, the source code is also freely available, which is why it comes under open source category.
Python 软件(以及文档)在 Python Software Foundation License 下发行。它是一个 BSD 风格的宽容软件许可证,且与 GNU GPL(通用公共许可证)兼容。
Python software (along with the documentation) is distributed under Python Software Foundation License. It is a BSD style permissive software license and compatible to GNU GPL (General Public License).
Python 是一種跨平台語言。可在各種作業系統平台(例如 Windows 、 Linux 、Mac OS、 Android OS )上使用預先編譯的二進制檔案。Python 的參考實作稱為 CPython,並以 C 編寫。可以下載原始碼並針對作業系統平台進行編譯。
Python is a cross-platform language. Pre-compiled binaries are available for use on various operating system platforms such as Windows, Linux, Mac OS, Android OS. The reference implementation of Python is called CPython and is written in C. You can download the source code and compile it for your OS platform.
Python 程序首先编译成中间平台无关字节码。然后,解释器内的虚拟机执行字节码。此行为使 Python 成为一种跨平台语言,因此 Python 程序可以轻松地从一个操作系统平台移植到另一个平台。
A Python program is first compiled to an intermediate platform independent byte code. The virtual machine inside the interpreter then executes the byte code. This behaviour makes Python a cross-platform language, and thus a Python program can be easily ported from one OS platform to other.
GUI Applications
Python 的標準發行版有一個出色的圖形庫,稱為 TKinter。它是廣受歡迎的 GUI 工具組 TCL/Tk 的 Python 移植版本。可以使用 Python 建立美觀且友善的使用者介面應用程式。GUI 工具組通常以 C/C++ 編寫。其中許多已經移植到 Python。例如 PyQt 、 WxWidgets 、 PySimpleGUI 等。
Python’s standard distribution has an excellent graphics library called TKinter. It is a Python port for the vastly popular GUI toolkit called TCL/Tk. You can build attractive user-friendly GUI applications in Python. GUI toolkits are generally written in C/C++. Many of them have been ported to Python. Examples are PyQt, WxWidgets, PySimpleGUI etc.
Database Connectivity
幾乎任何類型的資料庫都可以用做 Python 應用程式的後端。DB-API 是資料庫驅動程式軟體的一組規格,讓 Python 可以與關聯式資料庫進行通訊。透過許多第三方程式庫,Python 也能與 NoSQL 資料庫(例如 MongoDB )搭配使用。
Almost any type of database can be used as a backend with the Python application. DB-API is a set of specifications for database driver software to let Python communicate with a relational database. With many third party libraries, Python can also work with NoSQL databases such as MongoDB.
Extensible
可延伸性的術語表示新增或修改現有功能的能力。如前所述,CPython(Python 的參考實作)是以 C 編寫。因此,可以使用 C 輕鬆撰寫模組/程式庫,並將其納入標準程式庫。還有其他 Python 實作,例如 Jython(使用 Java 編寫)和 IPython (使用 C# 編寫)。因此,可以在這些實作中撰寫和合併新功能,並分別使用 Java 和 C#。
The term extensibility implies the ability to add new features or modify existing features. As stated earlier, CPython (which is Python’s reference implementation) is written in C. Hence one can easily write modules/libraries in C and incorporate them in the standard library. There are other implementations of Python such as Jython (written in Java) and IPython (written in C#). Hence, it is possible to write and merge new functionality in these implementations with Java and C# respectively.
Active Developer Community
由於 Python 的普及和開源特性,許多 Python 開發人員經常參與線上論壇和會議。Python 軟體基金會也有大量的會員,參與了組織的任務:" Promote, Protect, and Advance the Python Programming Language "
As a result of Python’s popularity and open-source nature, a large number of Python developers often interact with online forums and conferences. Python Software Foundation also has a significant member base, involved in the organization’s mission to "Promote, Protect, and Advance the Python Programming Language"
Python 也获得了显着的机构支持。主要 IT 公司 Google、Microsoft 和 Meta 通过准备文档和其他资源做出了很大贡献。
Python also enjoys a significant institutional support. Major IT companies Google, Microsoft, and Meta contribute immensely by preparing documentation and other resources.
除了上述功能之外,Python 還有其他一大串優點,以下列舉其中幾個:
Apart from the above-mentioned features, Python has another big list of good features, few are listed below −
-
It supports functional and structured programming methods as well as OOP.
-
It can be used as a scripting language or can be compiled to byte-code for building large applications.
-
It provides very high-level dynamic data types and supports dynamic type checking.
-
It supports automatic garbage collection.
-
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.