(05) function

Source: Internet
Author: User

function definition
Defines the DEF function name (parameter list) such as Def fun (x, y). Do not keep keyword conflicts when you take a name
Call function Fun function definition, to invoke will execute the code inside

function parameters
The parameter list after the formal parameter is defined in the function, which is the formal parameter def fun (x, Y.) X, y these are formal parameters
Arguments in the argument list after the call to the function are arguments.
Given an argument, the number is the same as the number of forms, and what type is not specified
Default parameter, specified at formal parameter, must be specified from right, otherwise the order of the parameter list is adjusted
def machine (x=3,y) Illegal def machine (x,y= ' legit ')

Scope of function-related variables
Local variables, variables defined in the function are local variables, can only be used by the function itself, not externally visible
Global variables, in addition to the variables defined in the function, are global variables that can be used by subsequent code snippets and functions
In a function declared as a global variable with the keyword global, after the declaration, the following code snippet can be used

function return value
No return, default returned to None
function return value, is the pass Retrun return statement
The function can return any type of value, numeric, string, list, ....
function calls Retrun, the function ends accordingly, and the code behind the Retrun does not execute

Other applications of the function
Multi-type pass-through values
Passing lists, tuples, dictionaries to functions
Ensure that good formal parameters and real parameter purposes are consistent, pass a list, tuples, dictionaries are a whole, equivalent to an argument,
Tuples as arguments, to functions, to tell if tuples are preceded by a tuple variable "*" such as Fun (*T)
Dictionaries as arguments, to functions, to tell that dictionaries are prefixed with "* *" (**d) parameters and dictionary keys in front of the dictionary variables.
If there is no corresponding, we should take the dictionary key corresponding value

Handling Redundant parameters
When defining a shape parameter, the background defines a tuple that can be received, such as Def Fun (X,*args), so that you can pass multiple values fun (8,3,3,4)
When defining a shape parameter, the background defines an acceptable tuple and dictionary such as Def Fun (X,*args,**kwargs) so that you can pass multiple values fun (8,3,name=toby)

Lambda expression (building an object)
Anonymous functions (mainly used in, execute scripts, once functions, only one line of functions)
G=LAMBDA x,y:x*y Definition: Defines an x, y variable and makes a x*y
G (2,3) call
Factorial Lambda simplifies code
L=rang (1,6)
Reduce (lambda x,y:x*y,l) reduce is a function that takes each of the two values in a list to the front

Built-in functions
Python itself has many built-in functions, as far as possible, high performance, do not invent their own wheels, their own definition, can help document view
Command line can use Help (command) to check usage
List of commonly used functions: ABS ()-Absolute max ()-Max min ()-min
Len ()-Length Divmod ()-division result (quotient and modulo) pow ()-Power Operation round ()-return floating-point number
Callable ()-Determine if there is an over function isinstance ()-Determine the type of the object CMP () compare two strings
Rang ()-used to quickly form a sequence Xrang ()-efficiency will be higher than rang () high type ()-Get the types of the variables
Int () long () float () complex () str () list () Truple () Hex () Oct () Chr () Ord ()-Type conversion
String function: Str.capitalize () str.replace () Str.split () filter () Zip () map () reduce () ... Help Rib Document view

(05) 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.