All test statements based on Python 2.7.3, Ubuntu 12.04
Self-study Python, if it is inappropriate, please correct me. Thank you.
Examples of most of the Python tutorials from Blog Park Vamei
1. Looping objects
# !/usr/bin/env python # Coding=utf-8 # contains a next () method each time a loop calls next () # until the stopiteration exception occurs, stop calling the next () method f = open ('test.txt')for in F: Print Line
# Builder customizes a looping object # # Span style= "color: #008000;" The return value of one yield is used per loop def Gen (): a = Ten yield a = a**2 yield a = a+10 yield a yield 525# This generator has 4 yield, when used as a circulator, 4 loops for a in Gen (): print a
Generator generator
# for the shape below is the generator can be abbreviated to G = (x for x in range (m)) # named generator expression (Generator expressions) def Gen (): for in range (4): yield for in range (4)) for in G: Print A
Table derivation
# Table Derivation list comprehension quick Generate table # The mechanism of table derivation is actually taking advantage of the cyclic object L = [] for in range: l.append (i) Print L # use brackets in shorthand form for in range ()print L
Test exercise:
# Test zip () aggregate function # after aggregation [(1,7), (3,12), (5,13)] # set y>10 so X takes 3 and 5. L=[6,10]a = [1,3,5= [7,12,13for inif y >] Print L
2. Function objects
function is also an object, passing the function name as a parameter
def func (A, b): return A + bdef Test (F, A, B): print'test' print1, 2)
The first argument f of the test function is a function object. The ability to pass Func to F () in F,test has the function of func ().
Map () function
def func (A, b): The function of return A + b# map () is to function objects sequentially on each element of the table # Each action result is stored in the returned table re # map () takes one element from each of the two tables each time, bringing in the defined function. re = map (func, [1, 2, 3, 4], [2, 3, 4, 5])print re
The filter () function filters the data by reading the function in the filter
def func (a): if a >: return True Else: return FalsePrint filter (func, [10, 99, 101, 500])
Returns the result as [101,500]
In Python 3.X, filter returns not a table, but a loop object.
The reduce () function requires that the function itself can receive two parameters. Reduce can progressively function on individual parameters.
def func (A, b): return A *= reduce (func, [1, 2, 3, 4])print re
The reduce () function is not directly used in 3.0, it is defined in the Functools package and needs to be introduced into the package
Exception exception handling differs from other high-level languages it's just that the keyword has changed. Another branch of an else
Try : ... except exception1: ... except exception2: ... except : ... Else : ... finally : ...
The process is as follows
try-> Abnormal->except->finally
Try-> No Abnormal->else->finally
In the Try program segment, we put in a section that is prone to mistakes. We can keep up with except to illustrate what the program should do if the statement in the Try section occurs stopiteration. If no exception occurs, the except section is skipped. The program will then continue to run, instead of being completely interrupted. If an exception occurs in a try, the attribution of the exception is performed, and the except is executed. Anomaly layer comparison, see if it is Exception1, exception2 ... until it finds its attribution, executes the statement in the corresponding except. If there are no parameters behind the except, then all exception are handed over to this program for processing. If the exception cannot be given to the appropriate object, the exception will continue to be thrown to the upper layer until it is captured or causes an error in the main program.
Basic syntax for Python's first experience (iv)