Python functions and lambda expressions

Function is a piece of code that perform specific tasks, the program will be defined by a piece of code into a function, and specify a function name for the function, so you can call when you need the code several times. Therefore, the function is an important means of code reuse.

It is important to grasp the learning function defined function, method calls the function. In addition, this chapter will also introduce advanced content a lot about Python, the reader should keep up this chapter explain key points.

Another point of knowledge is closely related to the function of the lambda expression. lambda expressions can be used as an expression, function parameter, or function return values, so use lambda expressions can make the program more concise.

Tissue function is good, reusable, code segments used to implement a single, or the associated function.

Function module of the application can be improved, and code reuse. You already know that Python provides many built-in functions, such as print (). But you can also create your own functions, which are called user-defined functions.

Define a function 
you can define a function of the desired function by themselves, the following rules are simple: 
the function code blocks def begins with keyword followed by the function name and identifiers in parentheses (). 
Any incoming parameters and arguments in parentheses must be placed in the middle, between parentheses may be used to define the parameters. 
The first statement function can be selectively used documentation string - for storing function instructions. 
Start function contents colon, and indentation. 
return [Expression] End function, selectively returns a value to the caller. Return without an expression equivalent to return None.
Anonymous function 
python using lambda to create an anonymous function. 
The so-called anonymous, which means no longer using the def statement of such standards in the form of a defined function.
lambda is just an expression, function body than def much simpler. 
lambda expression is a body, instead of a code block. We can only package a limited logic into the lambda expression. 
lambda function has its own namespace, and can not be accessed outside its own parameter list or the global namespace parameters. 
Although lambda function looks can only write a single line, but not equivalent to C or C ++ inline function, which aims not take up the stack memory when calling small functions to increase operating efficiency.

 

Guess you like

Origin www.cnblogs.com/zy09/p/11628439.html