Python Basics (ii):
Novice Tutorial Basic knowledge of the very comprehensive, content selection I think the focus output again
Function: Defines a function:
You can define a function that you want to function, and here are the simple rules:
def Fun (ARG): Pass return Arg # function Call Syntax Fun (ARG)
- Arg:argument, parameters (also called prarmeter), any incoming parameters and arguments must be placed in the middle of the parentheses, and parentheses can be used to define the parameters.
- Def:define, define, create function, function code block begins with def keyword, followed by function identifier name and parentheses ().
- Code indentation represents a statement block that indicates a code dependency
- return [expression] ends the function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.
Call to function:
You can call this function by passing a parameter into a function that has already been defined
def printme (str): " print any passed-in string " Print (str) return # Call the function printme (" I want to invoke the user-defined function! ") " ) Printme (" call the same function again ") The output of the above instance: I want to invoke the user-defined function! Call the same function again
function return value:
The so-called "return value", that is, the function of the program to complete one thing, and finally to the caller's results
#定义函数 def Add2num (A, B): return a +b #调用函数, by the way, save the return value of the function result = Add2num (100,98) # Because result has saved the return value of Add2num, you can then use print (result)--->198
The function return value can be multiple:
def divID (A, b): ... shang = a//b ...Yushu = a%b ... return Shang, yushu...>>> sh, yu = divID ( 5, 2) >>> sh5>>> yu1
4 Types of functions:
function According to whether there are parameters, there is no return value, can be combined with each other, a total of 4
No parameter, no return value
No parameters, with return values
With parameters, no return value
With parameters, with return values
- Input parameters:
- Formal parameters: Formal arguments, arguments in parentheses () when declaring a function
- Arguments: Actual arguments, calling functions, parameters in parentheses ()
- The actual arguments value is passed to the formal parameter, which is essentially a variable assignment
- return value: Return
The category of the parameters in the function:
- Required Parameters
- keyword parameter
-
def about (Name,course, Site): print (name,course,site) About (Site= " www.baidu.com " , Name= " green Span style= "COLOR: #800000", ", course=100) # #以关健字方式传入参数 (can be ordered out of order)
- Default parameters
-
def About (name,course,site): Print (Name,course,site) about ('Jim','the ' ' www.baidu.com ') # # must be transmitted sequentially
- Indefinite length parameter
-
- non-keyword mutable parameters (tuples)
- Placed after the default value parameter
- An incoming indeterminate number of default parameters can be aggregated into tuples for easy function use
defFun (A, B, *args):Print(a)Print(b)Print(args)Print("="*30) ret= A +b forIinchargs: #便利args中的元素 ret+=IreturnretPrint(Fun (1,2,3,4)) Results:12(3, 4)==============================10
- keyword variable parameters (dictionary)
- Placed behind non-keyword mutable parameters
- You can aggregate the incoming indeterminate number of default parameter into a dictionary handy function using
defFun (A, B, *args, * *Kwargs):Print(a)Print(b)Print(args)Print(Kwargs) Fun (1, 2, 3, 4, name ="Hello", age = 20) Results:12(3, 4){'name':'Hello',' Age': 20}
尽量避免多种参数格式混合(主要是两种可变参数)
多参数混合时注意顺序: 一般顺序是:默认参数,默认值参数,非关键字可变参数,关键字可变参数
注意:
带关键字的参数必须放在最后
Anonymous functions:
- In semantic sense, lambda is just a grammatical technique in the definition of a common function, and the writing code is more concise (but not understandable).
- Lambda is an expression (non-code block), simpler than Def, can only encapsulate limited logic such as if Else branch, cannot use multiple branches and loops, is not suitable for complex operations
# Common Functions def Def_sum (A, B): return A + bdef_sum (5, ten)# anonymous function: variable name = lambda parameter ...: expression evaluates lambda A, b:a + blambda_sum (5, ten)
--->15
Branch anonymous functions#Common FunctionsdefDef_gender (g):ifG = = 1: return 'male' Else: return 'female'def_gender (0)#anonymous function: variable name = lambda parameter ...: value of branch 1 if judging condition Else branch 2Lambda_gender =LambdaA:'male' ifA = = 1Else 'female'Lambda_gender (1)---> Men
# normal function def Absum (A, B): return a + b def def_sum2 (A, B , c): return absum (A, b) + cdef_sum2 ( 1,2,3) # anonymous function: variable name = lambda parameter ...: expression of other functions def Absum (A, B): return a + blambda_sum = lambda x, y, z:absum (x, y) + Zlambda_sum ( 1,2,3)
--->6
Initial knowledge of Python functions (definitions, parameters, return values)