Function
Def sayhi ():
print ' Hi '
Def Cheng (A, B):
Return a*b
Print Cheng (3,4)
Local Variables : function internal definition, release after function call ends
Global Variables : both inside and outside the function can be called, global
>>> def hanshu2 ():
... The global x #x为Global Variables , and X adds 2 to the inside of the function, and the globally variable x also changes the
... x=x+2
... print X
Assignment parameters
def (x=1,y) #语法错误, an assignment parameter cannot be followed by an unassigned parameter, that is, the assignment argument is placed behind
#练习
>>> def compare (a,b,c): #找出最大值函数
... if a>=b:
... if A>=c:return a
... Else:return c
.. else:
... if B>=c:return b
... Else:return c
...
Document string
>>> def Wendang ():
... "Test doc function" #文档字符串
... print ' Hi '
...
>>> Wendang ()
Hi
>>> wendang.__doc__
' Test doc function '
>>>
# Encoding:utf-8 def He (x,*y): #* Used to pass any of the non-named parameters, these parameters will be a Tuple in the form of Access to the "' The exercise of the flight " ' = 0for in y: = Total + i*x return Totalprint He (3,4)print He (3,4,5)
#Encoding:utf-8defCanshu (** y): # * * is used to process the passing of any parameter with a name, which is accessed using dict PrintU'the length of the input dictionary is:%s'%(len (y))PrintyPrintY.keys ()Printy.values () Canshu (name ='VP', age='m', sex='f' )
5-11 function assignment parameters, variable parameters