Function
Built-in functions
Common built-in functions:
Type () lists the types of the specified objects help () can provide detailed assistance Dir () lists all the properties of the object VARs () lists all the variables of the current module file,doc,name__file__ lists the file location __doc__ the document string __na me__ executed script, name = = __main__
if " __main__ " : print ("hello")
Int () ABS () max () min () POW () bool () Divmod (x, y) #divide, get a tuple of quotient and remainder, use in page paging operation. SUM () Len () All () takes a sequence to determine that all values are true before returning to Trueany () to accept a sequence, as long as there is an entry that is true, that is true Chr () The return value is the ASCII character corresponding to the current integer. The Ord () return value is the integer corresponding to the current ASCII character. Hex () 16 Conversion Oct () 8 Binary Conversion bin () 2 conversion range () Create an integer iterator enumerate () enumeration function
for in Enumerate (range (5)): print (k,v)
Print () eval (Sourece[,globals[,locals]]) evaluates the string as an expression and returns a value of Zip (seq1,seq2,...) Create a new sequence for parallel iterations sorted () returns the sorted list in SEQ reversed () produces a SEQ in the reverse version that is worth iterating over
Custom functions
def defines a keyword for a function
function name, calling function by function name
function declaration, not executed automatically;
The parameters of the function provide data for the function, common parameters, keyword parameters, default parameters, dynamic parameters;
Returns the data returned after the Worth function has finished executing.
To create a function:
def fib (n): result = [0,1] for in range (n- 2): result.append (result[-2]+result[-1]) return result
return value of the function
1. Return value not explicitly specified, return none
Not a real function:
def Show (): print ('a') return print ( ' b ' Show ()
Print (x)
A
None
the #函数 executes print (' a ') and is terminated by return and does not proceed to print (' B ')
2. The return value can be assigned to a variable
def Show (guess): ret=FalseifGuess = ='Xixi': Ret=TruereturnRetret= Show ('Xixi')ifRet:print ("guess right.")Else: Print ("wrong guess.") Guess right.
General parameters:
Formal parameters, formal parameters, actual arguments, arguments, parameters can have n, passing n parameters
def fun2 (a1,a2): print (A1,A2) fun2 ('Hello,','wolrd ')
Keyword parameters
Sometimes the ambiguous order is difficult to remember, can provide the name of the parameter, so the parameter order is not affected, but the parameter name and value must correspond.
def hello (greet,name): print ('%s,%s! '%(greet,name)) Hello (name='CSP', greet='Hello ')
Hello,csp
Default parameters
The default parameter can only be placed at the end, and the parameter supplied by default is used without passing in parameters.
def hello1 (greet,name="koka"): print ('%s,%s! '%(greet,name)) hello1 ('hello')
Hello,koka!
Dynamic parameters
Add * when defined, accept multiple parameters, internal constructor tuple
Def xixi (*arg): print (Arg,type (ARG)) Xixi (1,2,3,4) ( 1 2 3,4) <class'tuple'>
Add * * When defined, accept multiple arguments, internal constructed dictionary
def haha (* *args): print (Args,type (args)) haha (name='koka', age=24 {'name''koka' age '<class'dict'>
Add * or * * When called to separate sequences and dictionaries, avoiding the internal construction of tuples or dictionaries
def haha (*arg,**args): Print (Arg,type (ARG)) print (Args,type (args)) haha ( One, A, -, -, name='Doubi')( One, A, -, -) <class 'tuple'>{'name':'Doubi'} <class 'Dict'>Li1= [ One, A, -, -]name= {'haha':123,'Xixi':456}haha (*li1,**name) ( One, A, -, -) <class 'tuple'>{'haha':123,'Xixi':456} <class 'Dict'>#星号只有在定义 (allows the use of an indefinite number of arguments) or calls ("Split"dictionary or sequence). #字典元素必须放在后面, otherwise an error haha (name='Test', A, -, -) haha (name='Test', A, -, -) ^syntaxerror:positional argument follows keyword argumentView Code
Some function functions
Lambda functions
#普通函数def func1 (ARG): return 1 1print (func2= lambda a,b,*arg,**args:print (A,b,arg,args) fun3 (2,3,4,5,6, name='koka' )
Filter function
The filter function filters the elements in the sequence and finally gets the sequence that matches the condition
= [one,all,$,]filter (lambda a:a>, seq)# Returns an iterator that can filter elements based on a function that returns a Boolean value
map FunctionPass all elements in a sequence to a function
#map (func, *= [one,one,"= [] def func1 (A1,A2): return A1 + A2map (func1,l1,l2) [in. []
Python Learning note three (Basic article)