Python Notes (3) Function Higher-Order Functions

function

1. What is a function

In the development of the project, there are some code blocks that are encapsulated and encapsulated after repeated use. We call them functions.

2. Why define a function

  1. Advantages: Convenience (improves code utilization)
  2. Improve code readability
  3. Easy to debug bugs:

3. Classification of functions

  1. System library functions
  2. custom function

4. Custom function

  • grammar:
def funcName():
    函数体
----------------
funcName()   //调用函数
基本格式:
  1.以def开始
  2.自定义函数名
  3.小括号()函数的参数写在里边,后跟冒号
  4.函数体注意缩进
  • name:
遵循标识符:
1.   以数字字母下划线组成
2.不能以数字开始
3.不能是系统关键字
  func_name funcName
  • function call
在函数定义之前不能调用
可以重复定义函数 后边的覆盖前边的
形参无默认值时不传参会报错
形参有默认值时可以不传参
形参有默认值且有多个参数时默认值在最后
多个参数有默认值的情况 传参率高的房子前边
  • keyword arguments
//实参的值可以通过形参的名字来传值
def demo(age,name,height):
    print(age,name,height)

demo(name = 'zhang',age = 1,height = 180)
//所有实参传参如果使用了关键字方式,name所有的参数都要指定对应关键字
  • parameter

    1. After the function name is defined, it is called a formal parameter
    2. Arguments after calling a function
    3. Parameter values ​​can be changed to improve code reusability
    4. Formal parameters and actual parameters are one-to-one correspondence
    5. A formal parameter is a formal parameter with a default value that can have a default value
  • return value

函数里边没有return 返回None
return 后边的值会返回给函数的调用处
想要返回多个值时 可以使用元组 列表 字典等形式
return 返回值可选
return后边只执行依据 其余下边有再多都不执行
def demo(num):
    num=30             
    print(num)            //30
    print(id(num))

num1 = 20
print(id(num1))
demo(num1)                //30
print(num1)               //20
  • variable length parameter
1.形参前面的*会把实参当做列表形式传
def goodMan(name,*goodList):
    print(goodList)

goodMan('a',['b'])
goodMan('b','c','d',['a','b'])
2.形参前面两个*会把实参当做字典形式传
3.实参前边的*会把 列表 元组转成可变参数
#计算一组数字的平方和
def calc(*nums):
    res = 0
    for x in nums:
        res+=x*x
    print(res)
calc(2,3,4,5)
4.实参前面**会把字典变成可变参数

Variable scope:

  • Local variables: variables defined in the function body are not available outside the function
  • Global variable: A variable defined outside the function body is called a global variable. It can get the value inside the function body, but cannot modify the value. If it is modified, a new variable will be declared in the function body.
    If you want to use a global variable to declare global, you can modify it

global uses the global variables outside the entire function
nonlocal uses the local variables of the outer function

  • recursive function

The function itself calls its own process:
the execution core
Termination conditions
If the problems that can be solved by loop and traversal in sub-projects, try not to use recursion? memory usage

  • Anonymous functions Define functions
    without def and use the ==lambda== keyword
func = lambda a,b:a-b
print(func(2,3))  //-1

print((lambda a,b:a-b)(10,20))  //-1
  • Closure function A
    function is nested within a function and the return value of the outer function is a function

Functions are strictly case sensitive

  • identity operator
is
not is

Higher order functions

Functions that pass functions as parameters are called higher-order functions
- map()

    第一个参数:函数 第二个参数是 列表
    def myMap(x):
        return x*2
    print(list(map(myMap,[1,2,3,4,5])))
  • reduce

    The first argument is a function and
    the second argument is a list
    def mySum(x,y):
    return x+y
    print(reduce(mySum,[1,2,3,4])) //10

  • filter

    Parameter: function list
    General users filter the sequence
    according to the function return value of the parameter to determine whether to filter out
    according to True and False

    def func(num):
    if num%2 ==0:
    return True
    return False

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324810913&siteId=291194637