1. formal and actual parameters
From the perspective of function calls, it is divided into formal and actual parameters. Parameter is a function to create and define the process of small brackets parameters; the argument refers to the function passed in the called procedure parameters. As follows
>>> DEF MyFirstFunction (name): ' function definition process is called parameter name ' # because Ta is only one form, represents a parameter to occupy a position Print ( ' passed in ' + name + ' is called argument because Ta is a specific parameter value! ' ) >>> MyFirstFunction ( ' small turtle ' ) is passed in argument is called small turtles, since Ta is a specific parameter value!
2. The function documentation (.__ doc__)
to the function is to write the document so that others may better understand your function, so this is a good habit. Function returns document is to describe the function of the function.
DEF MyFirstFunction >>> (name):
'function definition process is called parameter name'
# Ta because only one form, a parameter showing a position occupying
print ( 'passed in' + name + 'argument is called, because Ta is a specific parameter value! ')
>>> MyFirstFunction .__ doc__ #MyFirstFunction no ()
'function definition process is called parameter name'
>>>
We found that at the beginning of the function to write the string is not printed, but it is stored as part of the function. This function is called documentation strings, it functions like a comment.
Note the difference is that a string can be a function of document special properties ._ _doc_ _ (Note: ._ _doc_ _ two sides are underlined):
Documents can also use the help () function View function
Function documentation written in a string inside, write two, print out only the first, second printing will not play, such as:
>>> DEF MyFirstFunction (name, Age): ' This is a function of document ' ' for recording using a recording function ' Print ( ' monitor: ' + name + ' , Age: ' + Age) >>> MyFirstFunction doc__ displays a .__ ' this document is a function of ' >>>
3. keyword arguments
Common parameter is called positional parameters, location parameters can be solved using the programmer in order to call a function when messing positional parameters, so that potential problems can not be achieved function as expected.
>>> DEF saySome (name, words): Print (name + " -> " + words) >>> SaySomething ( " little turtle " , " let the programmer change the world! " ) Small turtles - > let the programmer change the world! SaySome >>> ( " let the programmer change the world! " , " Little turtle " ) allows the programming to change the world! - > Small turtles
'position in order not to invoke the argument confused, with keyword arguments, will not be wrong' >>> saySome (words = " ! Let programmed to change the world " , name = " little turtle "
Keyword argument specifies the variable name is actually a parameter passed in the argument, used when calling the function !
4. The default parameter
default parameters when the function is defined, given the parameters into the parameter default values:
>>> DEF saySome (name = " little turtle " , words = " let the program change the world! " ): Print (name + ' -> ' + words) >>> saySome () # function is called with no parameters, it will go to the default parameters of small turtles - > let the programmer change the world! SaySome >>> ( "small squid " , "can change the world " ) # reassign arguments, arguments invoked when the function is called, does not call the default parameters of small squid - > You can also change the world
DEF test1 >>> (name = ' Turtle ' , Age = ' 20 ' ): Print ( ' language teacher's name is: ' + name + ' , Age: ' + Age) >>> test1 () language teacher's name is: turtle, Age: 20 >>> test1 ( ' Youyu ' ) language teacher's name is: youyu, Age: 20 >>> test1 (name = ' lucu ' ) language teacher's name is: lucu, Age: 20 > >>
Use the default parameters, then it can no arguments to call the function. So, the difference between them is: keyword arguments when the function is called, specify the parameters to be assigned by a parameter name, not afraid to do so because the order of the parameters which led to confuse the function call error; and the default parameters in the parameter definition process, the initial value of parameter, when the function call, the parameter is not transmitted, instead of using the initial value of the parameter is default.
The collection parameters (variable parameter)
indicates Function (* param)
collection parameters is referred most of the time variable parameters, only before the parameter with an asterisk (*) to:
>>> DEF TestFun (* param): Print ( ' length function is: ' , len (param)) Print ( ' second parameter is: ' , param [. 1 ]) >>> TestFun (. 1, ' Hello ' , 3.14 ) length function is: 3 the second parameter is: Hello >>>
Python is to mark the parameters were collected parameters packed into a tuple.
Note: If you need to specify additional arguments after the collection parameters when calling the function should use keyword arguments to specify , otherwise it will be your Python arguments are included in the scope of the collection parameters.
>>> DEF TestFun (* param, On Dec): Print ( ' length is a function of D% ' % len (param)) Print ( ' last parameter is: ' , param [-1 ]) >>> TestFun (2 ,. 4, 6, ' World ' , 7, 8 ) Traceback (MOST Recent Last Call): File "<pyshell # 18 is>", Line. 1, in <Module1> TestFun (2,4,6, 'World',. 7 ,. 8) TypeError: TestFun (). 1 Missing keyword-only required argument: 'On Dec' >>> TestFun (2,4,6, ' World ' ,. 7,=. 8 On Dec ) length function is 5 last parameter is:7 >>>
Suggest:
With collection parameters in the parameter, the other parameters may be set to default parameters, this is not easy to make mistakes
>>> DEF TestFun (param *, = On Dec. 9 ): Print ( ' length is a function of D% ' % len (param)) Print ( ' last parameter is: ' , param [-1 ]) >>> TestFun (2,4,6, ' World ' , 7,5 ) length function 6 is the last parameter is: 5 >>>
>>> DEF Test (param *, KeyName =. 8 ): Print ( ' collection parameters: ' , param) Print ( ' Keyword parameters: ' , KeyName) >>> Test (1,2,3,4, 9 ) collection parameters are: ( 1, 2, 3, 4, 9 ) key parameters are: 8 >>>
And after-school exercise answers
0. ask which is the parameter which is the argument?
>>> def MyFun(x): return(x**3) y = 3 print(MyFun(y)) >>> MyFun(3) 27
is the parameter x, y is the argument
1. Documentation and function directly as a function to write notes # What is the difference
function documentation can attribute .__ doc__ call that Function .__ doc__,
# Comments
2. Use keyword parameters, to avoid any problems arise
avoid calling function parameter passing problem is wrong
3. Use help (print) see print () What are the BIF default parameters? Respectively, play what role?
4. What are the default parameters and the biggest difference surface is a key parameter?
The default parameters on assignment in the function definition, keyword arguments are evaluated when the function call
Move hands:
0. knitting a function meets the following requirements:
A) is calculated by multiplying print all parameters and base (base = 3) result of
b) If the last parameter (base = 5) parameters, the set cardinality 5, the base is not involved in the summation calculations.
>>> def Sum_X_base(*param,base = 3): result = 0 for each in param: result += each if param[-1] == 5: base = 5 Sum = result *base return Sum >>> Sum_X_base(1,2,3) 18 >>> Sum_X_base(1,2,5) 40 >>>
Looking narcissistic number 1.
If it is equal to a 3-digit and the digits of the cubic, the called number is a number from daffodils, e.g. 153 + 5 = 1 ^ 3 ^ 3 ^ 3 + 3, 153 is thus a narcissistic number , write a program to find all the numbers daffodils.
Narcissus [nɑːsɪsəs]: Narcissus
>>> def FoundNarcissus(): for each in range(100,1000): temp = each sum = 0 while temp: sum += (temp % 10)**3 temp = temp // 10 if (sum == each): return each >>> FoundNarcissus() 153 >>>
The correct code is as follows:
>>> def Narcissus(): for each in range(100,1000): temp = each result = 0 while temp: result += (temp %10)**3 temp = temp // 10 if (result == each): print(each) >>> Narcissus() 153 370 371 407 >>>
2. Write a function of the findstr (), the function of the statistical number of times a length of 2 sub-string appearing in another string, for example: if the input string is: You can not improve your past, but you can improve your future.Once time is wasted, life is wasted. substring is im, after the execution of the print function "substring in the target string CCP often appear three times."
findstr DEF (desStr, SUBSTR): COUNT = 0 length = len (desStr) IF SUBSTR not in desStr: Print ( ' not found in the string target string! ' ) the else : for each1 in the Range (Length- 1 ): IF desStr [each1] == substr [ 0 ]: IF desStr [each1 + . 1 ] == substr [ . 1 ]: COUNT + = . 1 Print ( ' substring of CPC appears in the target string for% d' % COUNT) desStr = INPUT ( ' Enter target string: ' ) substr = INPUT ( ' Enter substring (two characters): ' )