Basic knowledge of python-function

Source: Internet
Author: User

First, why use the function of the problem of no function
#1, the organization of the code is not clear, the readability of the poor, the repetition of the function can only repeat the implementation of code, code redundancy, the function needs to be expanded, need to find out all the implementation of the function of the local modification, not unified management and maintenance is very difficult
second, what is the function
For the second problem, imagine the example of life, the repairman needs to get ready to put a tool in the toolbox, hammer, wrench, pliers and other tools, and then encounter the hammer nail scene, with a hammer can be used, without the need to temporarily build a hammer. Repairman ===> Programmer with a tool for a function ===> function to use a tool, you need to prepare it beforehand, then use it and reuse it to use the function, you need to define it, then use
III. Classification of functions
#1, built-in functions in order to facilitate our development, for some simple functions, the Python interpreter has been defined for our functions as built-in functions. For built-in functions, we can use them without having to define them beforehand, such as Len (), sum (), Max () PS: We'll go through the usual built-in functions in detail at the end. #2, custom functions It is obvious that the functions provided by the built-in functions are limited, which requires us to customize our own functions to implement some function according to our requirements, and then, when we encounter the application scenario, we can call the custom function. For example
Iv. How to customize functions?
#语法def function name (parameter 1, parameter 2, Parameter 3,...):    The value returned by the "comment" function body return # function name to reflect its meaning
v. Three forms of defining functions
#1, no parameter: The scenario simply performs some action, such as interacting with the user, printing # #, having a parameter: to perform the corresponding logic, such as the statistical length, the maximum minimum value, and the empty function, according to the parameters passed in externally: Design code structure
Six, call the function 1. Calling Functions
Function call: function name in parentheses 1 first find name 2 call code by name
2. function return value
No Return->nonereturn 1 value returned 1 values return comma separated multiple values--tuples
When should I have a return value?    Call the function, after a series of operations, finally to get a definite result, you must have a return value usually has the parameter function needs to have the return value, the input parameter, after computation, obtains the final result when does not need to have the return value? Call a function, just perform a series of operations, and finally do not need to get any results, there is no need to have a return value normally no parameter function needs to have a return value
3. Three forms of function call
1 Statement form: foo () 2 expression form: 3*len (' Hello ') 3 when another function parameter: Range (len (' Hello '))
Vii. parameters of the function 1. Participate in the actual parameter
#形参即变量名, the argument is the variable value, when the function is called, the value is bound to the variable name, the function call ends, and the binding is unbound
2. Specific application (emphasis)
#1, positional parameters: parameter positional parameters defined in left-to-right order: Required parameter position argument: value by location parameter: # #, keyword parameter, an argument that is defined by the form of a key=value does not need to be noted by location as a parameter value: 1. The keyword argument must be on the right side of the positional argument 2. Cannot repeat the value of the same parameter # #, the default parameter: The parameter is defined when it has been assigned to its value can be passed or not, often need to become parameters defined as positional parameter, the smaller parameter is defined as the default parameter (formal parameter) attention to the problem: 1. Assign a value of 2 only once at the time of definition. The definition of the default parameter should be 3 on the right of the positional parameter. The default parameters should usually be defined as immutable types # #, variable length parameters: variable length refers to the number of arguments is not fixed, and the arguments are defined by position and by the keyword, for the two forms of variable length, formal parameters corresponding to two solutions for the complete storage of them, respectively, is *args,**kwar GS ===========*args=========== def foo (X,y,*args): print (x, y) print (args) foo (1 , 2,3,4,5) def foo (X,y,*args): print (x, y) print (args) foo (1,2,*[3,4,5]) def foo (            X, Y, z): print (x, y, z) foo (*[1,2,3]) ===========**kwargs=========== def foo (X,y,**kwargs): Print (x, y) print (Kwargs) foo (1,y=2,a=1,b=2,c=3) def foo (X,y,**kwargs): Print    (x, Y) print (Kwargs)    Foo (1,y=2,**{' a ': 1, ' B ': 2, ' C ': 3}) def foo (x, z): print (x, y, z) foo (**{' d ': 1, ' × ': 2, ' Y ': 3})            ===========*args+**kwargs=========== def foo (x, y): print (x, y) def wrapper (*args,**kwargs): Print (' ====> ') foo (*args,**kwargs) #5, named keyword parameter: * After the parameter defined, must be passed the value (with the exception of the default), and must be passed as a keyword argument to ensure that the incoming parameters must contain some            Keyword def foo (x,y,*args,a=1,b,**kwargs): print (x, y) print (args) print (a)            Print (b) print (Kwargs) foo (1,2,3,4,5,b=3,c=4,d=5) Results: 2 (3, 4, 5) 3 {' C ': 4, ' d ': 5}

  

Basic knowledge of python-function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.