From entry to prison-------function (two)

Getting started N day

----The more important part is here, welcome to collect if you like! ----
  • The return value of the function

    The return value is the data transferred from inside the function to the outside. By default, the new data generated inside the function cannot be used outside the function

    def func1(a, b):
        c = a + b
        print(c)
    n(1, 2)   #3
    '''
    print(c) # NameError: name 'c' is not defined
    '''
    

    How to determine the function return value?

    Every function in python has a return value. The return value depends on the data after the return keyword encountered when the function body is executed. If return is not encountered, the return value is None. It is hoped that the data as the return value will be placed after the return at the end of the function. The value of the function call expression is the return value of the function

    def func1(a, b):
        print(a + b)
    
    
    c = func1(1, 20)   # 没有遇到return  
    print(f'函数返回值是:{c}')  # 函数返回值是:None
    

    The role of return: When you encounter return, you will determine the return value of the function and end the function directly. Receive the return value outside the function, and get the value of the function call expression outside the function is to get the return value of the function

    def func2(a, b):
        return a + b
        print(a + b)  # 不会执行
    
    
    c = func2(10, 2)
    print(f'函数返回值是:{c}')  # 函数返回值是:12
    
    
    def func3(a, b):
        return a + b, a * b
    
    
    c = func3(10, 2)
    print(f'函数返回值是:{c}')  # 函数返回值是:(12, 20)
    
    

    Essentially the returned value is a tuple, but you can use multiple variables to get multiple elements in the tuple

    def func3(a, b):
        return a + b, a * b
    
    
    c, d = func3(10, 2)
    print(f'函数返回值是:{c}和{d}')  # 函数返回值是:12和20
    
    

    Exercise: Define a function that can find the sum and average of multiple numbers

    def sums(*args):
        if not args:
            return None, None
        return sum(args), sum(args) / len(args)
    
    
    sums1, mean1 = sums(1, 2, 3, 4, 56)
    print(f'它们的和是{sums1},它们的平均值是{mean1}')
    

    Pack and unpack with *

    # 打包
    a, *b = 1, 2, 3, 4, 5
    print(a, b)  # 1 [2, 3, 4, 5]  通过*将b打包为列表
    # 解包
    b = [1, 2, 3, 4, 5]
    print(*b)  # 1 2 3 4 5  通过*将b列表解包
    
  • Global variable

    Variables defined outside of functions and classes are global variables. The scope of global variables can be used anywhere from the beginning of the definition to the end of the program.

    # 例如
    a = 100
    for i in range(1):
        print(f'循环内部{a}')  # 循环内部100
    
    
    def x():
    
        a='0000000000'
    
        print(f'函数内部{a}')
    
    x()  # 函数内部100
    
    # 注意:这里的j和m 都是全局变量,因为它们都在函数和类的外面定义的
    for j in range(3):
        m = 1
        print(f"循环内部{j}和{m}")
    print(f"循环外部{j}和{m}")  # 循环外部2和1
    
    
    def x1():
        print(f"函数内部{j}和{m}")
    
    
    x1()  # 函数内部2和1
    
  • Local variable

    The variables defined in the function are local variables (variables defined in the class are not local variables but attributes). The scope is from the beginning of the definition to the end of the function

    def func1(x, y=100):
        d = 123  # d,x,y 为局部变量
        print(f'函数内部{d},{x},{y}')
    
    
    func1(1)  # 函数内部123,1,100
    # print(d,x,y)     # NameError: name 'd' is not defined  不能在外部使用局部变量d
    

    Memory changes during function calling: every time a function is called, the system will automatically open up a temporary memory space in the stack area to save the data (defined variables) generated in the function. When the function call is over, this memory will Automatic release. View the changes at each step of the execution process (URL is slow to open)
    http://pythontutor.com/visualize.html#mode=edit

  • global: Define global variables in the function, which can only be used in the function body

    # 定义一个全局变量
    m = 100
    n = 1
    def func():
        # 定义一个新的局部变量m
        m = 200
        global n
        n = 100  # 将全局变量n的值变为100 n依然是全局变量
        global p
        p = 10  # 在函数内部定义一个全局变量p
        print(f"局部变量m:{m},n:{n}")
    
    
    func()  # 局部变量m:200 n:100
    print(f"全局变量m:{m},使用global后的n:{n}")  # 全局变量m:100,使用global后的n:100
    
  • nonlocal: The function rework in the function body modifies the value of the first-level local variable

    def func5():
        t = 1
        s = 1
    
        def func6():
            nonlocal s
            s = 200
    
        func6()
        print(f'函数中的变量t:{t},s:{s}')  # 函数中的变量t:1,s:200
    
    
    func5()
    
  • Anonymous function

    An anonymous function is essentially a function, and it does not need a function name when it is defined (in general, it will not be used) and it is used more when calling a higher-order function with actual parameters.

    Syntax: lambda parameter list: return value

    Equivalent to: def a (parameter list):

    ​ return return value

    def _(a, b):
        return max(a,b)
    
    
    re = _(1, 2) #获取函数的返回值
    print(re)  # 3
    re=lambda m, n: m + n
    re1 = re(1, 3)
    print(re1)  # 4
    
    # 练习:写一个匿名函数,获取两个数中最大的那个数
    a = lambda *args: None
    a1 = a(1, 50, 132, 4556)
    print(a1)
    
  • Functions are variables

    Defining a function in Python means defining a variable whose type is function, and the function name is the variable name.

    Functions can do what ordinary variables can do
    a = lambda i: i * 2
    print(f'a的类型{type(a)}')  # a的类型<class 'function'>
    
    
    def b(x):
        return x * 2
    
    
    print(f'b的类型{type(b)}')  # b的类型<class 'function'>
    c = b  # 不加括号是给变量c赋值,加括号是获取返回值
    print(c(2))  # 4
    
    list1 = [1, 2, 3, c(2), b(3)]  # 将函数放入列表
    print(list1)  # [1, 2, 3, 4, 6]
    
    
    def func4(f):
        print(f(10, 20) * 2)
    
    
    func4(lambda *a: 12)
    
    
    
    def b(*x):
    
        return '*'
    
    a=b(123,2,5,6)*2
    print(a)
    
    
    list2=[i if i < 10 else i for i in range(0,100,3)]
    print(list2)
    
    
    list2=[]
    for i in range(0,100,3):
        if i <10:
            list2.append(i)
        else:
    
            list2.append(i)
    
    print(list2)
    

Guess you like

Origin blog.csdn.net/weixin_44628421/article/details/109034426