Function definition and use

Defined using functions

Understand the function

Function is a statement group having a specific function for some reusable, abstract function is one function, the general expression of specific meaning, mainly to reduce the difficulty, and reuse.

definition

def <function name> ():
return <return>

Case

Calculating the factorial of n

def fact(n):
   s= 1
   for i in range(1, n+1):
      s*= i
   return s

y = f (x)
defining a function, the specified parameter is a placeholder for
the function definition, if not through the call will not be executed
when the function is defined, the parameter is input, processing body is a function, the result is output ( IPO)

Function uses

Calling a specific function representing the given parameter, alternate definitions of parameters
A = FACT (10):
DEF FACT (n-):
S =. 1
for I in Range (. 1, n-+. 1):
S = I *
return S
Print (A )
function can have arguments, or may not, but must retain the brackets
def <function name> ():
<function body>
return <return value>
EG:
: DEF FACT ()
Print ( "I am also a function")
can function definition specify default values for some parameters, optional parameters constituting
DEF <function name> (<Required number>, <optional parameters>):
<function body>
return <return value>
EG
calculate the factorial of n // m

def fact(n, m=1):
   ...:     s = 1
   ...:     for i in range(1, n+1):
   ...:         s *= i
   ...:     return s//m

Function definition can be designed variable number of parameters, the parameters determining the total number neither
DEF <function name> (<parameters>, B *):
<function body>
return <Return value>

def fact(n, *b):#*b是可变参数
    s = 1
    for i in range(1, n+1):
        s *= i
    for item in b: #增加一个循环
        s *= item
    return s

Parameters passed in two ways

Function call, the parameters can be passed by location or name mode

def fact(n, m=1):#m是可变参数
    s = 1
    for i in range(1, n+1):
        s *= i
    return s//m
fact(10, 5)
Out[39]: 725760

return of reserved words

Function can return a value, or may not, can return, may not
return 0 return value can be passed, may be any of a plurality of transmitting return values

n, s = 10, 100 #n,s是全局变量
def fact(n):
    s = 1      #fact()函数中的n和s是局部变量与全局s不同
    for i in range(1, n+1):
        s *= i
    return s   #此处是全局变量
print(fact(n), s) #此处是全局变量100

Output 3628800100

# Local and global variables
<chunks sentence 1>
DEF <function name> (<parameters>):
<function body>
return <Return Value>
<statement block 2>
Rule 1: the local and global variables are different variable
local variables is a placeholder for the internal functions, global variables may be the same name but a different
end of the function calculation, the local variables could be released

global reserved words

It used to declare global variables inside function

n, s = 10, 100 #n,s是全局变量
def fact(n):
    global s #声明全局变量s
    s = 1      #fact()函数中的n和s是局部变量
    for i in range(1, n+1):
        s *= i
    return s #此处是s是全局变量
print(fact(n), s) #此处全局变量s被修改

Output 36288003628800
Rule 2: Create a local variable and not as a combination of data types, the global variable is equivalent to

Is = ["F", "f"] #通过使用[]真实创建了一个全局变量列表Is
def func(a):
    Is.append(a) #此处Is是表示列表类型,未真实创建,则 等同于全局变量
    return
func("C")  #全局变量Is被修改
print(Is)

Output [ 'F', 'f', 'C']

If a combination of the array is created in real function, that is, after local variables do not appear to run the local

Is = ["F", "f"] #通过使用[]真实创建了一个全局变量列表Is
def func(a):
    Is = [] #此处IS是列表类型,真实创建,Is是局部变量
    Is.append(a) #此处Is是表示列表类型,未真实创建,则 等同于全局变量
    return
func("C")  #全局变量Is被修改
print(Is)

Output [ 'F', 'f']

Use Rules

The basic data types such as integers, floating point, regardless of whether the same name, different local variables and global variables
can be declared within a function reserved word in the global variables by global
as the list for a combination of data types, if a local variable is not real created, all local variables

lambda functions

lambda is a function anonymous function, neither the function name
with lambda reserved words defined, the function returns the name of the result is
lambda function for simple definition, a function can be represented in a row
<function name> = lambda <parameter>: < expression>
is equivalent to
DEF <function name> (<parameters>):
<function body>
return <return value>
EG:

`f = lambda x, y : x + y #定义了x+y
f(10, 15)#选取了10+15
Out[44]: 25`#输出结果25

eg

f = lambda : "lambda函数" #定义输出函数名字
print(f())
lambda函数

Special parameter lambda function or method of application function definitions, lambda functions have some fixed use, it is recommended to gradually grasp, in general, it is recommended to use ordinary function def definition.

Published 11 original articles · won praise 0 · Views 72

Guess you like

Origin blog.csdn.net/kyra1997/article/details/105127066