1. if
There can be zero or more elif parts, and the else part is optional. The keyword ‘elif‘ is short for ‘else if’
2. for
>>> for x in a:... print(x, len(x))>>> for x in a[:]: # make a slice copy of the entire list... if len(x) > 6: a.insert(0, x)
3. range()
range(5, 10) 5 through 9range(0, 10, 3) 0, 3, 6, 9range(-10, -100, -30) -10, -40, -70
To iterate over the indices of a sequence, you can combine range() and len() as follows
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']>>> for i in range(len(a)):... print(i, a[i])
In many ways the object returned by range() behaves as if it is a list, but in fact it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space.
We say such an object is iterable, that is, suitable as a target for functions and constructs that expect something from which they can obtain successive items until the supply is exhausted. We have seen that the for statement is such an iterator. The function list() is another; it creates lists from iterables
>>> print(range(10))range(0, 10)>>> list(range(5))[0, 1, 2, 3, 4]
4. loop
Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.
5. pass
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example:
>>> while True:... pass # Busy-wait for keyboard interrupt (Ctrl+C)...
This is commonly used for creating minimal classes:
>>> class MyEmptyClass:... pass...
Another place pass can be used is as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored:
>>> def initlog(*args):... pass # Remember to implement this!
6. defining functions
The first statement of the function body can optionally be a string literal; this string literal is the function’s documentation string, or docstring. (More about docstrings can be found in the section Documentation Strings.)
In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is called None (it’s a built-in name).
The default values are evaluated at the point of function definition in the defining scope, so that:
code:
i = 5def f(arg=i): print(arg)i = 6f()
result:
5
Important warning: The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes. For example, the following function accumulates the arguments passed to it on subsequent calls:
def f(a, L=[]): L.append(a) return Lprint(f(1))print(f(2))print(f(3))
This will print
[1][1, 2][1, 2, 3]
If you don’t want the default to be shared between subsequent calls, you can write the function like this instead:
def f(a, L=None): if L is None: L = [] L.append(a) return L
7 建構函式
The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__(), like this:
def __init__(self): self.data = []
When a class defines an __init__() method, class instantiation automatically invokes __init__() for the newly-created class instance.