Http://docs.python.org/3/library/functions.html or rookie Chinese information
1-Using functions
ABS ( -20)# Find the absolute # to find the maximum value # turn to intfloat ("12.34 " # turn into float # turn into a string # turn into BOOL type bool (' '
2-Custom functions
def my_abs (x): if not isinstance (x, (int, float)): return 222 if x >= 0: return x Else : return -X
X,y= (111,222); The x value is the 111,y value is 222. function can return a tuple function directly
3-Parameters of the function
3.1 Default parameters, define default parameters Remember one thing: the default parameter must point to the immutable object!
defSUM (x, n=2): returnx+Nsum (5)#equivalent to calling Power (5, 2):defEnroll (name, Gender, age=6, city='Beijing'): Print('City :', City) Enroll ('Adam','M', city='Tianjin')#you can pass only the specified parameters
3.2 Variable Parameters
def # * indicates variable sum = 0 for in numbers: = sum + N * n return Sumcalc (# parameter called nums=[1,2,3]calc (# the second way
3.3 Keyword Parameters
defPerson (name, age, * *kw):Print('Name:', Name,'Age :', Age,'Other :', kw) extra= {' City':'Beijing','Job':'Engineer'}person ('Jack', **extra)
3.4 Named keyword Parameters
defPerson (name, age, *args, city='Beijing', Job):Print(name, age, args, city, job) person ('Xiaofeng', 12,city='Shenzhin', job='Myjob') Extra= {' City':'Beijing','Job':'Engineer'}person ('Xiaofeng', 12,**extra)
3.5 parameter Combinations
defF1 (A, B, c=0, *args, * *kw):Print('A ='A'B ='B'C ='C'args =', args,'kw =', kw) args= (1, 2, 3, 4) kw= {'D': 99,'x':'#'}f1 (*args, **kw)#A = 1 b = 2 c = 3 args = (4,) kw = {' d ': ' X ': ' # '}
python-2 function