python-commonly used built-in functions


1. Introduction of functions

From the perspective of implementing functions, at least the following three points need to be considered:
1. The function needs several key data that need to change dynamically, and these data should be defined as the functionparameter.
2. The function needs to pass out several important data (that is, the data that the person who calls the function wants to get), these data should be defined asreturn value
3.The internal implementation process of the function

常用的内置函数: max,min,sum, divmod
函数必须有输入和输出。
max_num = max(1, 2, 3)
print(max_num)

1.如何创建函数?定义函数,函数内容并不会执行
函数的输入专业叫参数, 函数的输出叫返回值。
 重点:
       - 形参: 形式参数,不是真实的值(定义函数时的参数)
       - 实参:实际参数,是真实的值(调用函数时的参数)
def get_max(num1, num2):
    result = num1 if num1 > num2 else num2
    return result
2. 如何调用函数?
max_num = get_max(30, 80)
print(max_num)

insert image description here

Second, the scope of variables

"""
可变数据类型:list, dict,set
不可变数据类型: 数值型, str, tuple
"""

# 1. 全局变量: 全局生效的变量。函数外面的变量。
name = 'admin'
def login():
    print(name)
login()

# 2. 局部变量: 局部生效的变量。函数内部的变量。
def logout():
    age = 19
    print(age)
logout()
# print(age)


# 3. 函数内部修改全局变量.
# 1). money是局部变量还是全局变量? 全局变量
# 2). 如果要在函数中修改全局的变量,不能直接修改。 需要用global关键字声明修改的变量是全局变量。
# 3). 不可变数据类型修改全局变量一定要global声明, 可变数据类型不需要。
def hello():
    global money
    money += 1
    users.append('user1')
    print(money, users)
money = 100  # 不可变数据类型
users = []  # 可变数据类型
hello()

insert image description here
insert image description here

3. Parameter passing

insert image description here

4. Common 4 types of formal parameters

1. Mandatory parameters: parameters that must be passed

2. Default parameters: parameters that can be passed or not

3. Variable parameters: the number of parameters will change, you can pass 0, 1, 2, 3,...n

4. Keyword parameters: key and value can be passed

"""
必选参数:必须要传递的参数
默认参数:
可变参数:*args - 元组
关键字参数:**kwargs - 字典
"""

# 1. 必选参数:必须要传递的参数
def get_max(num1: int, num2: int) -> int:
    return num1 if num1 > num2 else num2

result = get_max(20, 30)
print(result)

# 2. 默认参数:可传可不传的参数
def pow(x, y=2):
    return x ** y

result = pow(3)  # x=3, y=2, result=9
print(result)
result = pow(2, 4)  # x=2,y=4, result=2**4=8
print(result)

# 3. 可变参数: 参数的个数会变化,可以传0,1,2,3,......n
# args是元组
# args=arguments
def my_sum(*args):
    return sum(args)

result = my_sum(4, 5, 6)  # 15
print(result)

# 4. 关键字参数:可以传递key和value
# kwargs存储在字典中
def enroll(name, age=18, **kwargs):
    print(f"""
        入学信息
    1. 姓名:{name}
    2. 年龄:{age}
    3. 其他:{kwargs}
    """)

enroll('张三', country='china', english='GRE', sports=['篮球', '羽毛球'])

from collections import  namedtuple

insert image description here
insert image description here

5. Anonymous functions

insert image description here

6. The leetcode topic corresponding to the anonymous function

insert image description here

Seven, recursive function

insert image description here

insert image description here
Recursive implementation of fib sequence
insert image description here


Guess you like

Origin blog.csdn.net/Gong_yz/article/details/131069847