(A) Definition and function calls
Code:
DEF max (x, y): # define a variable z, is equal to the variable x, y values greater # Z = X IF X> Y the else Y if(x>y): return(x) else: return(y) A = 478 B = 345 657 # calls my_max () function, the function returns a result value is assigned to the variable result = max (A, B) # ① Print ( " result: " , result)
result:
result: 345657
Code:
DEF A (Love): Print ( ' ------ This is an amazing language ------- ' ) return Love + ' Python ' a1 = A ( ' I like ' ) Print ( ' I said: ' , A1)
result:
This is an amazing ------ ------- language I said: I like python
(B) the reference transmission value is transmitted and
The different types of actual parameters, transmission mode function parameters can be divided into two kinds, respectively, and the reference value passed (address) is transmitted:
Value is passed: argument type is suitable for immutable type (string, number, component);
Reference (address) is transmitted: the argument is applied to the variable type (list, dictionary);
value of the difference between transmission and reference transmission is a function of the parameter values passed, if the parameter value is changed, the argument does not affect value; and continued after the function parameters passed by reference, changing the value of the parameter, the value of the argument will change together .
Code:
def demo(obj): obj + = obj Print ( " parameter values: " , obj) Print ( " ----------------- pass ---------------- Value " ) # value is passed Application Type: string, number , etc. tuple argument immutable a = " C language Chinese network " Print ( " a value: " , a) # If the shape parameter value is changed, does not affect the value of the argument, the parameter stack, the same argument Demo (A) Print ( " argument is: " , A) Print ( " ----------------- --------------- passing by reference " ) # applies to the argument type variable type ( list, dictionary) A = [ ' 0 ' , ' . 1 ' , ' 2 ' ] Demo (A) # argument with the change parameter is changed Print ( " argument is: " , A)
result:
----------------- ---------------- value transfer a value of: C language Chinese network Parameter values: C language Chinese language Chinese network web C Argument is: C language Chinese network ----------------- --------------- passed by reference Parameter values: [ '0', '1', '2', '0', '1', '2'] Argument values: [ '0', '1', '2', '0', '1', '2']
(C) parameters
(1) position parameter
In calling the function, the actual number of specified parameters, the same number of parameters must be in the form of (multi-Chuan Chuan less will not work).
( 2) keyword arguments
Keyword argument refers to the use of the formal parameter name to determine the input parameter values.
Code:
DEF man (Sex, name): # Print ( "sex", Sex) # Print ( "name", name) # define a function return ' I ' + Sex + ' \ t ' ' interested in women ' + ' \ t ' + ' names are: ' + name; Print (man ( " boys " , ' John Doe ' )) # keyword arguments passed to the parameter Print (man (Sex = ' boys ' , name = ' Joe Smith ')) # Exchangeable position when using keyword arguments Print (man (Sex = ' male ' , name = " John Doe " )) # part keyword parameters, part parameters use position # positional parameters must be placed prior to the keyword parameter Print (man ( ' male ' , name = ' Zhang ' )) # Print (man (' male ', sex =' John Doe ')) given
result:
I am a male hobby female name: Joe Smith I am a male hobby female name: Joe Smith I am a male hobby female name: Joe Smith I am a male hobby female name: Joe Smith
(3) the default parameters
python allows to set the default parameter values, i.e., by defining a function, directly to the formal parameters specify a default value .
Code:
# Specified as two parameters Default DEF man (name = ' Tagore ' , book = ' Python ' ): return name + book; # All default parameters A = man () Print (A) # only book default value b = man ( ' Cangyangjiacuo ' ) Print (B) # neither parameter default value C = man ( ' Nalanrongruo ' , ' Bowl ' ) Print (C) # only parameter name default value D = man (Book = ' birds ') print(d)
result:
Tagore python Cangyangjiacuo python Nalanrongruo Bowl Tagore birds set
(Iv) Reverse transmission parameters
The so-called reverse collected parameters, referring to the parameters in the list has been the premise of the program, tuples, dictionaries and other objects, the elements to their "open" passed to the function. Reverse parameters collected need to add an asterisk before the incoming list, tuple parameters, add two asterisks before the dictionary parameter.
Code:
DEF man (name, Book): Print ( ' name is: ' + name) Print ( ' works is: ' + Book) A = [ ' Tagore ' , ' Birds ' ] man(*a) B = man ( ' Tagore ' , ' Gardener ' )
result:
Name: Tagore Works: Birds Name: Tagore Works: The Gardener
(E) a plurality of function return value
Code:
def sum_and_avg(list): sum = 0; count = 0; for i in list: if isinstance(i,int) or isinstance(i,float): sum += i COUNT + =. 1 Print (SUM) Print (SUM / COUNT) # obtain a plurality of values sum_and_avg function returns, the return value is packaged into a plurality of tuples list1 = [2,4,6,7,3,5,6, ' G ' ] sum_and_avg(list1)
result:
33 4.714285714285714
Code:
def sum_and_avg(list): sum = 0; count = 0; for i in list: if isinstance(i,int) or isinstance(i,float): sum += i count +=1 return sum,sum/count list1 = [2,4,6,7,3,5,6,'g'] A, B = sum_and_avg (List1) # using sequence unpacked to obtain a plurality of return values Print (A) Print (B)
result:
33 4.714285714285714