Python基础教程(第3版)

最新书摘:
  • huangh
    2011-04-20
    whether something is callable or not with the built-in function callable:>>> import math>>> x = 1>>> y = math.sqrt>>> callable(x)False>>> callable(y)True■Note The function callable no longer exists in Python 3.0. With that version, you will need to use theexpression hasattr(func, __call__). For more information about hasattr, see Chapter 7.
  • huangh
    2011-04-20
    >>> age = 10>>> assert 0 < age < 100>>> age = -1>>> assert 0 < age < 100Traceback (most recent call last):File "<stdin>", line 1, in ?AssertionError
  • huangh
    2011-04-20
    Basic Dictionary OperationsThe basic behavior of a dictionary in many ways mirrors that of a sequence:• len(d) returns the number of items (key-value pairs) in d.• d[k] returns the value associated with the key k.• d[k] = v associates the value v with the key k.• del d[k] deletes the item with key k.• k in d checks whether there is an item in d that has the key k.
  • huangh
    2011-04-19
    there are two important reasons why you need to know about tuples:• They can be used as keys in mappings (and members of sets); lists can’t be used this way. You’ll learn more mappings in Chapter 4.• They are returned by some built-in functions and methods, which means that you haveto deal with them. As long as you don’t try to change them, “dealing” with them mostoften means treating them just like lists (unless you need methods such as index andcount, which tuples don’t have).In general, lists will probably be adequate for all your sequencing needs.A Quick SummaryLet’s review some of the most important concepts covered in this chapter:Sequences: A sequence is a data structure in which the elements are numbered (startingwith zero). Examples of sequence types are lists, string...
  • huangh
    2011-04-19
    You can perform all the standard sequence operations on lists, such as indexing, slicing, concatenating, and multiplying. But the interesting thing about lists is that they can be modified. In this section, you see some of the ways you can change a list: item assignments, item deletion, slice assignments, and list methods. (Note that not all list methods actually change their list.)
  • huangh
    2011-04-19
    >>> numbers[0:10:2][1, 3, 5, 7, 9]numbers[3:6:3][4]>>> numbers[8:3:-1][9, 8, 7, 6, 5]>>> numbers[10:0:-2][10, 8, 6, 4, 2]>>> numbers[0:10:-2][]>>> numbers[::-2][10, 8, 6, 4, 2]>>> numbers[5::-2][6, 4, 2]>>> numbers[:5:-2][10, 8]
  • huangh
    2011-04-19
    Common Sequence Operations There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence, and for finding its largest and smallest elements.
  • huangh
    2011-04-19
    Sequence Overview Python has six built-in types of sequences. This chapter concentrates on two of the most common ones: lists and tuples. The other built-in sequence types are strings (which I revisit in the next chapter), Unicode strings, buffer objects, and xrange objects.
  • huangh
    2011-04-19
    A Quick SummaryThis chapter covered quite a bit of material. Let’s take a look at what you’ve learned beforemoving on.Algorithms: An algorithm is a recipe telling you exactly how to perform a task. When youprogram a computer, you are essentially describing an algorithm in a language the computercan understand, such as Python. Such a machine-friendly description is called aprogram, and it mainly consists of expressions and statements.Expressions: An expression is a part of a computer program that represents a value. Forexample, 2+2 is an expression, representing the value 4. Simple expressions are built fromliteral values (such as 2 or "Hello") by using operators (such as + or %) and functions (suchas pow). More complicated expressions can be created by combining simpler expressio...
  • huangh
    2011-04-19
    A synonym for repr(x) is `x` (here, you use backticks, not single quotes). This can be useful when you want to print out a sentence containing a number:>>> temp = 42>>> print "The temperature is " + tempTraceback (most recent call last):File "<pyshell#61>", line 1, in ?print "The temperature is " + tempTypeError: cannot add type "int" to string>>> print "The temperature is " + `temp`The temperature is 42
  • huangh
    2011-04-19
    str simply converts a value into a string in some reasonable fashion that will probably be understood by a user, for example.11 repr creates a string that is a representation of the value as a legal Python expression.
  • huangh
    2011-04-19
    Back to the __future__It has been rumored that Guido van Rossum (Python’s creator) has a time machine, because quite often when people request features in the language, the features have already been implemented. Of course, we aren’t all allowed into this time machine, but Guido has been kind enough to build a part of it into Python, in the form of the magic module __future__. From it, we can import features that will be standard in Python in the future but that aren’t part of the language yet. You saw this in the section about numbers and expressions, and you’ll be bumping into it from time to time throughout this book.
  • huangh
    2011-04-18
    >>> 1 // 20
  • huangh
    2011-04-18
    If you are familiar with other computer languages, you may be used to terminating every line with a semicolon. There is no need to do so in Python. A line is a line, more or less. You may add a semicolon if you like, but it won’t have any effect (unless more code follows on the same line), and it is not a common thing to do.
  • 孔明
    2011-04-01
    如果某项任务令人望而却步,将其分解为小一些的部分总是有用的。同时,要对解决问题所使用的工具进行评估。
  • 孔明
    2011-03-29
    换句话说,生成器是由两部分组成:生成器的函数和生成器的迭代器。生成器的函数是用def语句定义的,包含yield部分,生成器的迭代器是这个函数返回的部分。
  • 孔明
    2011-03-29
    这就在于它不是像returen那样返回值,而是每次产生多个值。每次产生一个值(使用yield),函数就会冻结:即函数停在那里等待被激活。函数被激活后就从停止的那点开始执行。