If s represents a string, then int(s) produces the integral value
that string represents. For example, the expression int('137')
produces the integer value 137. If an invalid string is given as
a parameter, as in int('hello'), a ValueError is raised (see
Section 1.7 for discussion of Python’s exceptions). By default,
the string must use base 10. If conversion from a different base
is desired, that base can be indicated as a second, optional,
parameter. For example, the expression int('7f ',16) evaluates
to the integer 127.
*整数构造函数 int()默认返回值 0。但是,此构造函数可
用于根据另一类型的现有值构造整数值。例如,如果 f 表
示浮点值,那么命令 int(f)会返回 f 的整数部分截断值。例
如,int(3.14)和 int(3.99)都产生值 3,而 int(-3.9)则产生值-
3。int()函数也可用于解释表示整数值的字符串(例如由
用户输入的字符串)。如果 s 表示一个字符串,则 int(s)
将产生字符串表示的整数值。例如,表达式 int('137')产生
整数值 137.如果将无效字符串作为参数给出,如 int('hello')
中,则会引发 ValueError(有关 Python 的异常的讨论,请
参见第 1.7 节)。默认情况下,字符串所表达的整数必须
使用十进制。如果需要从不同的进位制进行转换,则该
进位制应当被指示为第二个可选参数。例如,表达式
int('7f',16)的计算结果为 127。
The float Class
*浮点类
The float class is the sole floating-point type in Python, using
a fixed-precision representation. Its precision is more akin to a
double in Java or C++, rather than those languages’ float type.
We have already discussed a typical literal form, 98.6. We note
that the floating-point equivalent of an integral number can be
expressed directly as 2.0. Technically, the trailing zero is
optional, so some programmers might use the expression 2. to
designate this floating-point literal. One other form of literal
for floating-point values uses scientific notation. For example,
the literal 6.022e23 represents the mathematical value 6.022 ×
10
23
.
*float 类是 Python 中唯一的采用固定精度的浮点类型。
它的精度更像 Java 或 C++中的 double 类型。我们已经讨
论了一个典型的字面形式,98.6。我们注意到,与整数相
等的浮点数可以用 2.0 这种样式来表示。从计算机内部原
理上讲,后缀 0 是可去的,所以一些程序员可能使用表达
式 2.来指定这个浮点数字。另外一种形式的浮点样式借
鉴 了 科 学 计 数 法 。 例 如 , 6.022e23 表 示 数 学 值
6.022×1023。
The constructor form of float() returns 0.0. When given a
parameter, the constructor attempts to return the equivalent
floating-point value. For example, the call float(2) returns the
floating-point value 2.0. If the parameter to the constructor is
a string, as with float('3.14'), it attempts to parse that string as
a floating-point value, raising a ValueError as an exception.
*函数 float()默认返回 0.0。当给定一个参数时,构造函
数尝试返回与之等值的浮点值。例如,调用 float(2)返回
浮点型 2.0。如果函数 float()的参数是一个字符串,比如
float('3.14'),这时候函数会尝试将该字符串解析为浮点值,
如果不匹配就会抛出 ValueError 异常。
Sequence Types: The list, tuple, and str Classes
*序列类:列表类、元组类、字符类
The list, tuple, and str classes are sequence types in Python,
representing a collection of values in which the order is
significant. The list class is the most general, representing a
sequence of arbitrary objects (akin to an “array” in other
languages). The tuple class is an immutable version of the list
class, benefiting from a streamlined internal representation.
The str class is specially designed for representing an
immutable sequence of text characters. We note that Python
does not have a separate class for characters; they are just
strings with length one.
*列表,元组和字符串类是 Python 中的序列类型,代表
一个或多个有序的集合。列表类是最通用的,表示任意
对象的序列(类似于其他语言中的“数组”)。元组类是
列表类的不可变版本,因为内部表示很简单,所以有不
少好处。字符串类专门用于表示不可变的文本字符序列。
我们注意到,Python 中没有单字符类;事实上单个字符
只是长度为 1 的特殊字符串。
The list Class
*列表类
A list instance stores a sequence of objects. A list is a
referential structure, as it technically stores a sequence of
references to its elements (see Figure 1.4). Elements of a list
may be arbitrary objects (including the None object). Lists are
array-based sequences and are zero-indexed, thus a list of
length n has elements indexed from 0 to n − 1 inclusive. Lists
are perhaps the most used container type in Python and they
will be extremely central to our study of data structures and
algorithms. They have many valuable behaviors, including the
ability to dynamically expand and contract their capacities as
needed. In this chapter, we will discuss only the most basic
properties of lists. We revisit the inner working of all of
Python’s sequence types as the focus of Chapter 5.
*列表可以储存一个对象的实例序列。列表储存的是对
象的地址(参见图 1.4)。列表的元素可以是任意对象
(包括 None 对象)。列表是基于数组的序列,并且是以
0 为下标开头的,因此长度为 n 的列表具有下标从 0 到 n-1
的元素。列表可能是 Python 中最常用的容器类型,它对
我们对数据结构和算法的学习至关重要。列表有许多有
价值的行为,包括动态变化。在本章中,我们将仅讨论
列表的最基本属性。在第 5 章,我们将要重新审视 Python
所有序列类型的内部机制,而这也将是第五章的重点。
Python uses the characters [] as delimiters for a list literal,
with[] itself being an empty list. As another example, ['red',
'green', 'blue'] is a list containing three string instances. The