Basic syntax for first-time Python experience (4) and first-time python experience
All test statements are based on Python 2.7.3 and Ubuntu 12.04.
Learn Python by yourself. please correct me if it is not appropriate. Thank you.
The vast majority of examples are python tutorials from vamei in the blog Park.
1. Loop object
#! /Usr/bin/env python # coding = UTF-8 # contains a next () method that calls next () Every loop # Until the StopIteration exception occurs stop calling next () method f = open('test.txt ') for line in f: print line
# Generator customizes a loop object # definitions are similar to functions, but return is changed to yield # Each loop uses a yield return value def gen (): a = 10 yield a = a ** 2 yield a = a + 10 yield a yield 525 # The generator has four yield instances, four cycles are performed for a in gen (): print
Generator
# The Generator can be abbreviated as G = (x for x in range (m) as follows # Name: Generator Expression (Generator Expression) def gen (): for I in range (4): yield iG = (x for x in range (4) for a in G: print
Table Derivation
# Table derivation list comprehension quick table generation # The table derivation mechanism uses the circular object L = [] for I in range (10): L. append (I * 2) print L # abbreviated form using brackets L = [x * 2 for x in range (10)] print L
Test exercise:
# Test zip () Aggregate Function # [(), ()] # Set y> 10 so x takes 3 and 5. L = [6, 10] a = [1, 3, 5] B = [7, 12, 13] L = [x * 2 for (x, y) in zip (a, B) if y> 10] print L
2. function object
The 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' print f(a, b)test(func, 1, 2)
The first parameter f of the test function is a function object. Pass func to f. f () in test has the func () function.
Map () function
Def func (a, B): return a + B # map () the function is to sequentially apply function objects to each element of the table # The result of each action is stored in the returned table re # map () to retrieve one element from both tables each time, include the defined function. Re = map (func, [1, 2, 3, 4], [2, 3, 4, 5]) print re
The filter () function filters data by reading functions.
def func(a): if a > 100: return True else: return Falseprint filter(func, [10, 99, 101, 500])
The returned result is [101,500].
In Python 3. X, the filter returns a circular object instead of a table.
The reduce () function requires the function itself to receive two parameters. Reduce can repeatedly act on each parameter.
def func(a, b): return a * bre = reduce(func, [1, 2, 3, 4])print re
The reduce () function cannot be used directly in 3.0. It is defined in the functools package and needs to be introduced into the package.
Exception is not much different from Exception Handling in other advanced languages, except that the keyword is changed and an else branch is added.
try: ...except exception1: ...except exception2: ...except: ...else: ...finally: ...
The process is as follows,
Try-> exception-> wait t-> finally
Try-> no exception-> else-> finally
In the try section, we add the error-prone part. We can keep up with limit t to explain what the program should do when the try clause StopIteration occurs. If no exception occurs, the Skip T section is skipped. Then, the program continues to run, rather than being completely interrupted. If an exception occurs in a try, the execution exception will be assigned and the execution will fail. Exception layer-by-layer comparison, check whether it is prediction1, prediction2. .. until you find its ownership and execute the corresponding statement in limit T. If there is no parameter after limit T, all exceptions are handled by this program. If an exception cannot be handed over to an appropriate object, the exception will continue to be thrown to the upper layer until it is captured or the main program reports an error.