Python personal notes, easy to query, unrelated to infringement.
Definition of function: def test (): print "hello world!" Test () #调用上面的test函数 Define a function with the parameter name: Def mingzi (name): print "Hello %s,how old are you?" %namemingzi (' Darren ') #调用定义的函数定义两个参数的函数: Def info ( Name,age): print "%s,%s"% (name,age) info (' Darren ', ' total ') #调用两个参数的函数给函数添加一个帮助文档: Def mingzi (name): ' this is a help doc! ' print "Hello %s,how old are you?" %namen= "Darren" Mingzi (n) #也可以先赋值再调用函数 >>>import tab>>> Import three>>>help (Three.mingzi) #查看自己编写的帮助文档 Local variables and global variables: Local variables: function execution ends when it loses its function. def info (name):     AGE=22    PRINT  ' your name is %s,you old is %s '% (name,age) info (' Darren ') Global variables: Outside the function, the global is in effect. age=26def info (name): age=22 print ' your name is %s,you old is %s '% (name,age) info (' Darren ') declarations change global variables to local variables (not recommended): Age= 26def info (name): global age #声明 age=22 print ' Your name is %s,you old is %s '% (name,age) info (' Darren ') print ' Age: ',age #此处第一个age: is to show Age: not a variable, but a variable, which is the use of a print to generate two pieces of content. Default parameters for function: def users (username,group= ' iphone '): #group = ' iphone ' is also an assignment variable, but if it is not assigned, it is the iphone by default. list={} #定义一个字典 list[username]=group #定义username为key, Group is value, meaning that the value of the change key is Variable group return listprint users (' Wang ') print Users (' Wangjia ', ' Dongge ') Multiple default variable:def info (name,age,internation= ' Zhongguo ', provice= ' Shandong '): age=22 print ' Your name is %s,you old is %s '% (name,age) print internation,proviceinfo (' Darren ', ' age ', provice= ' Beijing ', internation= ' Meiguo ') #默认赋值的变量可以没有顺序, but variables with no default values must precede the default worth of variables. Key parameters of the function: Def fun (a,b=5,c=10): print a,b,cfun (3,7) Fun (25,c=24) fun (c=50,a=100) #重点: The function adds a special parameter that can be arbitrarily assigned to multiple values. Def test (*.args): print argstest (' Darren ', ' Wang ', ') ' #把一个字典赋值给函数, The value corresponding to the dictionary key is the variable. def testa (**kargs): print kargsname_list={' name ' = ' Wang ', ' age ' = ' 22 ', ' IphonE ' = ' Pingguo '}testa (name= ' Darren ', age= ' + ', iphone= ' sanxing ') #有时候我们希望能把函数执行的结果保存下来, this time the return parameter is required: def users (username,group= ' iphone '): list={} list[username]=group return listyonghu= Users (' Wang ', ' group= ' Linux ') print yonghu lambda anonymous functions:>>> a=lambda x:x+2>> > a (2) 4-------A=range map (lambda x:x**2,a)--------def f (x): return x**2print f (4) The normal function is the same as the following lambda function g=lambda x:x**2print g (4) --- -------
This article from "Small East elder brother" blog, declined reprint!
Section eighth: Python functions