Python learning notes (13) Python functions (2), learning notes python Functions
Parameters and variables
1 >>> def foo (a, B): # function is an object 2 return a + b3 4 >>> p = foo # object assignment statement. Assign the foo function to the variable p. 5 >>> foo () 6 97 >>> p () Variable p points to the function foo 8 99 >>>
PASS Parameters by reference
Assign values in order. The variable a in the function points to x, and x is the first real parameter. The parameter a points to the object referenced by x, instead of copying the number 3 to a function, this method of calling the object is called passing by reference.
1 >>> x, y = 3,4 # x, and y respectively point to 3 and 4 2 >>> foo (x, y) # The function has two parameters: a and B 3 7 4> def bar (x): 5 x = 1 8 >>> m = 9 9 >>> bar (m) 10 >>> m11 912 >>>
All variables of a program are not accessible anywhere. The access permission determines where the variable is assigned.
The scope of a variable determines which specific variable name you can access in the program. The scopes of the two most basic variables are as follows:
Global and local variables
Variables defined in a function have a local scope and global scopes defined outside the function.
Local variables can only be accessed within the declared function, while global variables can be accessed within the entire program. When a function is called, all variable names declared in the function will be added to the scope.
1 >>> x = 2 # global variable 2 >>> def foo (): 3 x = 9 # local variable 4 print "this x is the fun :", x 5 6 7 >>> foo () 8 this x is the fun: 9 9 >>> x10 2
The x in the function is different from the x outside the function. We apply an x like x = 9 to a function within a certain range, which is called a local variable. 11 >>> def bar (): 12 global x # declare the global variable 13 x = 914 print "this x is the fun :", x15 16 17 >>> x18 219 >>> bar () 20 this x is the fun: 921 >>> x22 923 >>>
Namespace: a namespace is a special abstraction of the scope.
Parameter collection and value passing
Collection Method 1: * args
* Args receives parameters in the form of tuples.
1 >>> def foo (* arg): 2 print arg 3 4 5 >>> foo (1, 2, 3) 6 (1, 2, 3) # receiving parameters in the form of tuples 7 >>> foo ("baidu", "ali", "qq", "weixin") 8 ('baidu', 'ali ', 'qq', 'weixin') 9> foo ("web", [1, 2, 3, "pythom"]) 10 ('web', [1, 2, 3, 'pythom']) 11 >>> def foo (x, * arg): 12 print "x:", x13 print "arg :", arg14 15 16 >>> foo (118, 3) 17 x: 721 arg: (2, 3) 19 >>> foo (7) 20 x: arg :() 22 >>>
Collection Method 2: ** kargs receives parameters in dictionary form
1 >>> def foo(**karg): 2 print karg 3 4 5 >>> foo(a=1,b=2,c=3) 6 {'a': 1, 'c': 3, 'b': 2} 7 >>> def foo(x,*arg,**karg): 8 print x 9 print arg10 print karg11 12 13 >>> foo(1)14 115 ()16 {}17 >>> foo(1,2)18 119 (2,)20 {}21 >>> foo(1,2,3)22 123 (2, 3)24 {}25 >>> foo(1,2,3,name="python")26 127 (2, 3)28 {'name': 'python'}29 >>>
>>> def book(author,name): print "{0} has a book :{1}".format(author,name) >>> bars={"name":"learn python with cc","author":"cc"}>>> book(**bars)cc has a book :learn python with cc>>>
Special Functions
Zip () Supplement
1 >>> colors =["red","green","blue"] 2 >>> values=[234,12,89,65] 3 >>> zip(colors,values) 4 [('red', 234), ('green', 12), ('blue', 89)] 5 >>> dots=[(1,2),(3,4),(5,6)] 6 >>> x,y=zip(*dots) 7 >>> x 8 (1, 3, 5) 9 >>> y10 (2, 4, 6)11 >>> seq =range(1,10)12 >>> zip(*[iter(seq)]*3)13 [(1, 2, 3), (4, 5, 6), (7, 8, 9)]14 >>> x =iter(range(1,10))15 >>> x16 <listiterator object at 0x0000000003E8F860>17 >>> list(x)18 [1, 2, 3, 4, 5, 6, 7, 8, 9]19 >>> zip(x,x,x)20 []21 >>> x=iter(range(1,10))22 >>> zip(x,x,x)23 [(1, 2, 3), (4, 5, 6), (7, 8, 9)]24 >>>
Lambda x: x + y lambda variable: Expression
Map, reduce, filter
1 >>> def foo(x): 2 x+-3 3 return x 4 5 >>> foo(4) 6 4 7 >>> n =range(10) 8 >>> n 9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]10 >>> [i+3 for i in n ]11 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]12 >>> lam =lambda x:x+313 >>> n2=[]14 >>> for i in n15 SyntaxError: invalid syntax16 >>> for i in n:17 n2.append(lam(i))18 19 20 >>> n221 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]22 >>> g =lambda x,y:x+y23 >>> g(3,4)24 725 >>> lambda x:x+426 <function <lambda> at 0x0000000003E91438>27 >>> n28 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]29 >>> map(foo,n)30 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]31 >>> map(lambda x:x+3,n)32 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]33 >>> lst1 =[1,2,3,4,5]34 >>> lst2=[6,7,8,9,0]35 >>> map(lambda x,y:x+y,lst1,lst2)36 [7, 9, 11, 13, 5]37 >>> reduce(lambda x,y:x+y,lst1)38 1539 >>> n =range(-5,5)40 >>> n41 [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]42 >>> filter(lambda x:x>0,n)43 [1, 2, 3, 4]44 >>> [x for x in n if x>0]45 [1, 2, 3, 4]46 >>>