function

Definition of a function

Function: A programming method of structuring program logic

** def function name () :

code

Return What to return **

The use of function keyword arguments

Print contains five parameters that need to be filled in order. If the last four parameters are used separately, use the form of parameter = “.

As is shown in

The functions are assigned separately

` def func(a,b,c):

print(‘a=%s’ %a)

print(‘b=%s’ %b)

print(‘c=%s’ %c)

` func (1, 2, 3)

A =1 b=2 c=3

The variable scope of a function

var1=123 def func(): var1=233 print(var1) func() print(var1)

Var =123 var=233

When a value is defined inside a function, it overwrites the value defined outside the function

Values defined inside and outside the function are called

Var1 =233

Instead of

global var1

var1=233

The result shown is var1=233

Iterators and generators

Iterator: iter

List1 it = = {1, 2, 3} iter (list1) print (next) (it)Copy the code

Results: 1

Features: Values are set in sequence

Generator: yield

Def frange(start,stop,step): def frange(start,stop,step): Yield x+=step for I in frange(10,20,0.5): print(I)Copy the code

Results shown 10,10.5, 11….

Lambda expressions

1.

lambda x:x<=(month,day)
||
def func1(x):
    return x<=(month,day)
Copy the code

2.

lambda item:item[1]
||
def func2(item):
    return item[1]
Copy the code

Built-in functions

① Filter (function/empty, iterator) to fetch the number of iterations that satisfy the function

A =,2,3,4,5,6,7 [1] print (list (filter (lambda x: x > 2, a)))Copy the code

Results: [3,4,5,6,7]

Using filter and lambda requires mandatory list conversions

②map(function,, pass multiple parameters) processes multiple parameters in turn

A = b =,3,5 [2] [1, 2, 3] print (list (map (lambda x, y, x + y, a, b)))Copy the code

Results: [3,5,8]

③ Reduce (function, sequence, initial value) will be sequential and initial value of the operation as a function

Reduce (lambda x, y, x + y, [4] 2, 1)Copy the code

The result shown is (1+2)+3+4=10

③ Switch key and value of zip(multiple elements)

X =zip((1,2,3),(4,5,6)) for I in x: print(I)Copy the code

Results :(1,4)(2,5)(3,6)

The iterating function has iter form, which can be evaluated using the for in form


x={'a':'aa','b':'bb'}
y=zip(x.values(),x.keys())
print(dict(y))
Copy the code

{‘aa’: ‘a’, ‘bb’: ‘b’}

closure

Closure: An outer function is referenced by an inner function. Similar to function nesting

Def sum(a): def add(b): Num1 =func() num2=sum(2) #num2 =sum(2 print(num2(4))Copy the code

Results: 6

Closures implement operations

Def arg_y(a,b): Line =a_line(3,5) # print(line(10)) #xCopy the code

Results :35

A decorator

Add additional functionality without requiring additional code

The time base

Sleep method: Pause the operation time for a few seconds

Time method: time statistics of seconds on January 1, 1970

Import time print(time.time()) time.sleep(3Copy the code

The running time of the function

import time def i_can_sleep(): Time.sleep (3) start=time.time() i_can_sleep() stop=time.time() print(' function run %s seconds '%(stop-start))Copy the code

The result: the function ran for 3.000880718231201 seconds

@ XXX a decorator

def timer(func): def wrapper(): Start =time.time() func() time.sleep(3) stop=time.time() print(' function run %s seconds '%(stop-start)) return wrapper @timer # decorator def I_can_sleep ():#i_can_sleep is decorated with the function time.sleep(3) i_can_sleep() #timer calls the function i_can_sleep passed to timer to receive a func variable func passed to the internal function wrapperCopy the code

Decorator with parameters

Def new_tips(argv):#argv receives the new_tips decorator add, passing the del argument def tips(func): Def nei (a, b) : # a, b are passed to the internal function print (' start % s % s' % (argv, func. __name__)) # func. __name__ function name. Func (a,b) print('stop %s %s'%(argv,func.__name__)) return nei # return tips # new_tips Def add(a,b): print(a+b) @new_tips('del') def sub(a,b): Print (a-b) print(add(5,4)) print(sub(5,4)) ** start add add 9 stop add add None start del sub 1 stop del sub NoneCopy the code

Context manager

with

For line in f: print(line) finally: f=open('name.txt') try: With open('name.txt') as f: for line in f: print(line)Copy the code

The module

define

Popular is an existing code segment that needs to be imported

Writing:

Import module name

Form module name import method name

Import OS OS. All methods import matplotlib as m # rename module to m.xx # do not use module Self-created file nameCopy the code

Grammar specification

www.python.org/dev/peps/pe…

Move right overall and press Tab