1. If
There can be zero or moreElifParts, andElsePart is optional. The keyword'Elif'Is short for 'else if'
2.
>>> 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 combineRange ()AndLen ()As follows
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']>>> for i in range(len(a)):... print(i, a[i])
In each ways the object returnedRange ()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 isIterable, That is, suitable as a target for functions and constructs that could CT something from which they can obtain successive items until the supply is exhausted. We have seen thatForStatement is suchIterator. The functionList ()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 haveElseClause; it is executed when the loop terminates through exhaustion of the List (For) Or when the condition becomes false (While), But not when the loop is terminated byBreakStatement.
5. Pass
ThePassStatement 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 placePassCan 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.PassIs 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 inDefiningScope, 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 CALS:
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 cals, you can write the function like this instead:
def f(a, L=None): if L is None: L = [] L.append(a) return L
7. Constructor
The instantiation operation ("calling" A Class Object) creates an empty object. Classes 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_ Init __()Method, class instantiation automatically invokes_ Init __()For the newly-created class instance.