Python function foundation ten: return value and global variables

1. Return value
1) What is return value
Return value is the data passed from inside the function to outside of the function. (If the function of the function is implemented, the new data must be returned through the return value if the new data is generated)
2) How to determine the function return value
Use the return keyword in the function body to return the return value: return data
Note: the same In a function, only one return is valid. (Because when the function body is executed, the return function will end directly.)
If you want to return multiple data in a function, use a container that can hold multiple data. Commonly used tuples: return data 1, data 2, data 3,...

3) How to get the return value of the function outside the function Get the value of the
function call expression is to get the return value of the function. (The data corresponding to the return value can be done, and the function call expression can be done)

def func4():
    return [1, 2, 3]


print(func4())
print(func4()[0])
for x in func4():
    print(f'x: {x}')

def sum1(num1, num2):
    s = num2 + num1
    return s, s/2


def func1():
    print('111111')
    return     # 相当于 return None
    print('222222')

练习:删除指定列表中所有指定元素
# [1, 23, 4, 5, 1, 34, 2, 1]  删除元素 1  -> [23, 4, 5, 34, 2]
# 方法一:
def del_item(list1: list, item):
    for x in list1[:]:
        if x == item:
            list1.remove(x)

According to the scope of the variable, variables are divided into global variables and local variables
. 1. Global variables Variables
defined outside functions and classes are
global variables. The scope of global variables: from the beginning of the definition to the end of the program. use

2. Local variables The variables
defined in the function are local variables
Scope: from the beginning of the definition to the end of the function

Reasons for local variables: Every time a function is called, the system will automatically open a separate memory space in the stack for the called function, which is specifically
used to save the variables defined in this function. This memory space will be automatically destroyed when the function call ends.

3.
Global and nonlocal Global and nonlocal are keywords in the function body, which can only be used in the function body.
1)
The role of global global: define or modify global variables in the function.
Usage:
global variable name
variable name = value

2) *
nonlocal function: modify the value of a local variable in the local local nonlocal
variable name
variable name = value

1. What is an
anonymous function? The essence of an anonymous function is still a function; but an anonymous function can only achieve the function of a function through a statement.
1) Syntax:
function name = lambda formal parameter list: return value
equivalent:
def function name (formal parameter list):
return return value

# 练习:写一个匿名函数,返回指定的年是否是闰年
is_leap_year = lambda year: (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
print(is_leap_year(2020))

is_leap_year2 = lambda year: '闰年' if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else '平年'
print(is_leap_year2(2021))

Guess you like

Origin blog.csdn.net/SaharaLater/article/details/111564460