And, as imagination bodies forth The forms of things to unknown, and the poet’s pen Turns them to shapes, and gives to airy nothing A local habitation and a name. —William Shakespeare, A Midsummer-Night’s Dream
To give Python a proper introduction, we will begin with an example that uses several language features. In the next section, we will start from scratch and build up the language piece by piece. Think of this section as a sneak preview of features to come.
为了给 Python 一个适当的介绍,我们将从一个使用了几个语言特性的例子开始。在下一节中,我们将从头开始,一块一块地建立起语言。把这一节看成是对未来功能的预先介绍。
Python has built-in support for a wide range of common programming activities, such as manipulating text, displaying graphics, and communicating over the Internet. The line of Python code
Python内置了对各种常见编程活动的支持,如操作文本、显示图形和通过互联网进行通信。Python的代码行
>>> from urllib.request import urlopen
is an import statement that loads functionality for accessing data on the Internet. In particular, it makes available a function called urlopen, which can access the content at a uniform resource locator (URL), a location of something on the Internet.
是一个import语句,用于加载访问互联网上数据的功能。特别是,它提供了一个叫urlopen的函数,它可以访问统一资源定位器(URL)的内容,URL是互联网上某个东西的位置。
Statements & Expressions. Python code consists of expressions and statements. Broadly, computer programs consist of instructions to either
- Compute some value
- Carry out some action
语句和表达式。Python代码由表达式和语句组成。广义上讲,计算机程序由指令组成,要么是 1.计算一些数值 2.执行某些动作
Statements typically describe actions. When the Python interpreter executes a statement, it carries out the corresponding action. On the other hand, expressions typically describe computations. When Python evaluates an expression, it computes the value of that expression. This chapter introduces several types of statements and expressions.
语句通常描述动作。当 Python 解释器执行一个语句时,它就执行了相应的动作。另一方面,表达式通常描述计算。当 Python 评估一个表达式时,它计算出该表达式的值。本章介绍了几种类型的语句和表达式。
The assignment statement
赋值语句
>>> shakespeare = urlopen(‘http://composingprograms.com/shakespeare.txt‘)
associates the name shakespeare with the value of the expression that follows =. That expression applies the urlopen function to a URL that contains the complete text of William Shakespeare’s 37 plays, all in a single text document.
将名称为shakespeare与后面的表达式=的值相关联。该表达式将urlopen函数应用于一个URL,该URL包含威廉·莎士比亚的37部戏剧的完整文本,全部在一个文本文档中。
Functions. Functions encapsulate logic that manipulates data. urlopen is a function. A web address is a piece of data, and the text of Shakespeare’s plays is another. The process by which the former leads to the latter may be complex, but we can apply that process using only a simple expression because that complexity is tucked away within a function. Functions are the primary topic of this chapter.
函数。函数封装了操作数据的逻辑。urlopen是一个函数。一个网络地址是一个数据,而莎士比亚的戏剧文本是另一个数据。前者导致后者的过程可能很复杂,但我们可以只用一个简单的表达式来应用这个过程,因为这种复杂性被隐藏在一个函数中。函数是本章的主要话题。
Another assignment statement
另一个赋值语句
>>> words = set(shakespeare.read().decode().split())
associates the name words to the set of all unique words that appear in Shakespeare’s plays, all 33,721 of them. The chain of commands to read, decode, and split, each operate on an intermediate computational entity: we read the data from the opened URL, then decode the data into text, and finally split the text into words. All of those words are placed in a set.
将名称为words与莎士比亚戏剧中出现的所有独特的单词集合联系起来,全部33,721个。读取、解码和分割的命令链,每一个都在一个中间计算实体上操作:我们从打开的URL中读取数据,然后将数据解码成文本,最后将文本分割成单词。所有这些单词都被放在一个集合中。
Objects. A set is a type of object, one that supports set operations like computing intersections and membership. An object seamlessly bundles together data and the logic that manipulates that data, in a way that manages the complexity of both. Objects are the primary topic of Chapter 2. Finally, the expression
对象。集合是一种类型的对象,它支持集合操作,如计算交叉点和成员关系。对象无缝地将数据和操作这些数据的逻辑捆绑在一起,以一种管理两者复杂性的方式。对象是第二章的主要话题。最后,表达式
>>> {w for w in words if len(w) == 6 and w[::-1] in words} {‘redder’, ‘drawer’, ‘reward’, ‘diaper’, ‘repaid’}
is a compound expression that evaluates to the set of all Shakespearian words that are simultaneously a word spelled in reverse. The cryptic notation w[::-1] enumerates each letter in a word, but the -1 dictates to step backwards. When you enter an expression in an interactive session, Python prints its value on the following line.
是一个复合表达式,它的评估结果是所有同时是一个词的反向拼写的莎士比亚词的集合。隐秘的符号w[::-1]列举了一个词中的每个字母,但是-1决定了要向后退一步。当你在交互式会话中输入一个表达式时,Python 将其值打印在下面一行。
Interpreters. Evaluating compound expressions requires a precise procedure that interprets code in a predictable way. A program that implements such a procedure, evaluating compound expressions, is called an interpreter. The design and implementation of interpreters is the primary topic of Chapter 3.
解释器。评估复合表达式需要一个精确的程序,以一种可预测的方式解释代码。实现这种过程的程序,评估复合表达式,被称为解释器。解释器的设计和实现是第三章的主要议题。
When compared with other computer programs, interpreters for programming languages are unique in their generality. Python was not designed with Shakespeare in mind. However, its great flexibility allowed us to process a large amount of text with only a few statements and expressions.
与其他计算机程序相比,编程语言的解释器在其通用性方面是独一无二的。Python在设计时并没有考虑到莎士比亚。然而,它巨大的灵活性使我们能够只用几个语句和表达式就能处理大量的文本。
In the end, we will find that all of these core concepts are closely related: functions are objects, objects are functions, and interpreters are instances of both. However, developing a clear understanding of each of these concepts and their role in organizing code is critical to mastering the art of programming.
最后,我们会发现,所有这些核心概念都是密切相关的:函数是对象,对象是函数,而解释器是两者的实例。然而,对这些概念中的每一个及其在组织代码中的作用形成清晰的认识,对于掌握编程艺术至关重要。