My Python introductory notes (14)

Chapter XI, function (under)


If the implementation is hard to explain, it's a bad idea.——The Zen of Python


First, the return value

  In Python, the return statement may be used in the body of the function, the return value for the specified function. The return value can be of any type, and no matter what position the return statement appears in a function, as long as implemented, it will directly end the execution of the function, its syntax is as follows:

  result = return [value]

  result: for storing returned results, if only returns a value, a stored value returned is the result, the value may be any type. If the return multiple values, then the result is stored in a tuple.

  value: Optional parameter specifies the value to be returned to, can return a value may return multiple values.

  When the function does not return statement, return statement or the parameter is omitted, the return None, i.e. returns NULL.

. 1  DEF funname (name):
 2      Nickname = '' 
. 3      IF name == ' rain Ni ' :    # if rain is entered Ni 
. 4          Nickname = 1210.
 . 5      elif name == ' Shu snow ' :
 . 6          Nickname = 1184
 . 7      elif name == ' Cream order ' :
 . 8          Nickname = 220
 . 9      the else :
 10          Nickname = ' Unable to find correspondence information ' 
. 11      return Nickname   #Returns the corresponding names of favorite number 
12 is  the while True:
 13 is      name = INPUT ( ' Enter your name: ' )   # receiving user input 
14      Nickname = funname (name)   # calling function 
15  Print ( " Name: " , name, ' favorite number : ' , Nickname)
 16  output:
 17  enter your name: rain Ni
 18 name: rain Ni favorite number: 1210

Second, the scope of variables

  Scope of a variable refers to the program code to access the region of the variable region above which, when re-access error occurs, generally in accordance with the "Effective range" variable, the variable will be classified as " local variables " and " global variables "

  1. Local variables

  Local variables are defined and internal variables used in the function, it is only effective in the contents of a function. That internal function name at run-time function, will create, or run to completion before it is executed, all of the name does not exist, so if you use a function internal variables defined outside the function, will throw NameError abnormal example: define a name for the function f_demo, as defined in the interior of the function of one variable message (referred to as a local variable) and assign it, and then outputs the variable, the final output variable message again outside the body of the function will be given:

1  DEF f_demo ():
 2      the Message = " If I 284, I 220 Where? " 
3      Print ( ' local variables the Message: ' , the Message)
 4  f_demo ()
 5  Print ( ' local variables the Message: ' , the Message)
 6  output:
 . 7    File " C: /Users/Mortal/PycharmProjects/Python_Note/venv/test1.py " , Line. 5, in <Module1>
 . 8      Print ( ' local variables Message: ' , Message)
 . 9 NameError: name ' Message' is not defined

  2. Global variables:

  Corresponding to the local variable, global variable is a variable function capable of acting on the inside and outside. Global variables are the following two situations:

  (1) If a variable is defined outside a function, not only is access to the outside of the function in the function may be accessed. Variables defined outside the body of the function is a global variable. Examples are as follows:

. 1 Message = " If I 284, I 220, where? "   # Globals 
2  DEF f_demo ():
 . 3      Print ( ' local variables Message: ' , Message)   # output value of the global variable in a function in vivo 
. 4 f_demo ()                               # call the function 
5  Print ( ' local variable message: ' , the Message)      # value of the output of a global variable in a function in vitro 
6  output:
 7  local variable message: If I 284, I 220 where?
8 local variable message: If I 284, I 220 Where?

  When the local variable with the same name as a global variable, the variable assignment function body, does not affect the function of the variables in vitro.

 (2) defined in the body of the function, and after use the global keyword modification, the variable may be a global variable. In vitro function you can also access to the variable, and may be further modified in vivo function.

1 the Message = " If I was 284, where my 220? "   # Global variables 
2  Print ( ' function in vitro: the Message = ' , the Message)      # output global variables in the function in vitro 
3  DEF f_demo ():
 4      the Message = ' others are concerned about your flying Gaobu Gao, I just do not care about your wings taste delicious! '   # Local variables 
. 5      Print ( ' local variables Message: ' , Message)   # in vivo function of the output values of the local variables 
. 6 f_demo ()                               # call function 
. 7  Print ( ' function in vitro Message: ', the Message)      # value of the output of a global variable in a function in vitro 
8  
9  output:
 10 function in vitro: message = If I 284, I 220 Where?
11  local variable message: people are concerned about your flying Gaobu Gao, I just do not care about your wings taste delicious!
12 function in vitro message: If I were 284, where my 220?

  Variables inside a function defined global variables even if the same name does not affect the values ​​of global variables. If you want to modify the values ​​of global variables within the function body, we need to re-define local variables, use the global keyword modification.

. 1 Message = " If I 284, I 220, where? "   # Globals 
2  Print ( ' function in vitro: Message = ' , Message)      # output global variables in vitro function 
. 3  DEF f_demo ():
 . 4      Global Message             # will message declared as a global variable 
5      message = ' others are concerned about your flying Gaobu Gao, I just do not care about your wings taste delicious! '   # Local variables 
. 6      Print ( ' local variables Message: ' , Message)   # in vivo function of the output values of the local variables 
. 7 f_demo ()                               # call function
. 8  Print ( ' function in vitro: message =: ' , Message)      # output value of the global variable in a function vitro 
9  Output:
 10  
. 11 function in vitro: message = if I 284, I 220, where?
12  local variable message: people are concerned about your flying Gaobu Gao, I just do not care about your wings taste delicious!
13 function in vitro: message =: others are concerned about your flying Gaobu Gao, I just do not care about your wings taste delicious!

  Note: Although Python allows global and local variables the same name, but in the actual development, is not recommended, because it is easy to make the code confusion, it is difficult to distinguish between those that are global variables, those are local variables.

Third, the anonymous function

  Anonymous functions are functions with no name, it needs a function in the main application, but do not want to bother to name the occasion of this function is usually the case, this function can only be used once, in Python, using the lambda expression to create anonymous function, its syntax is as follows:

  result = lambda[arg1[,arg2,…,arg n]]:expression

  the Result : used to call the lambda expression

  [arg1 [, arg2, ..., arg n]]: optional parameter specifies the list of parameters to be passed, a plurality of parameters separated by commas

       expression The : Required parameter to achieve expression specifies a particular function. If there are arguments, then the application of these parameters in the expression.

  When using lambda expressions, can have multiple parameters, separated by commas, but only one expression that can only return a value, but also can not appear other non-expression statement (for or while).

. 1  Import Math
 2  DEF circlearrea (R & lt):
 . 3      Result = R & lt Math.PI * * R & lt
 . 4      return Result
 . 5 R & lt = 10
 . 6  Print ( ' radius ' , R & lt, ' the area of a circle: ' , circlearrea (R & lt))
 7  output:
 8 circle with a radius of 10 area: 314.1592653589793

  Use a lambda expression:

. 1  Import Math
 2 R & lt = 10
 . 3 Result = the lambda R & lt: R & lt Math.PI * * R & lt
 . 4  Print ( ' radius ' , R & lt, ' the area of a circle: ' , Result (R & lt))
 . 5  Output:
 6 radius 10 the area of a circle is: 314.1592653589793

  Although the use of lambda expressions custom code function is less than the use. However, the use lambda expressions, we need to define a variable to call the lambda expression.

  Lambda primary use of the expression is designated short callback function .

  emmm about Python's built-in functions do not do too much description, you will encounter later!

Guess you like

Origin www.cnblogs.com/1210x1184/p/11129589.html