Python---Several parameters of functions

  1. Positional parameters
  2. Keyword arguments (key=value)
  3. Default parameters (default values ​​must be written at the end)
  4. Indefinite length parameters (1: positional transfer, 2: keyword transfer)
  5. Functions are passed as parameters (passing of calculation logic, not data)

example:

# 参数传递参数
def user_info(name, age, gender):
    print(f"姓名:{name},年龄:{age},性别:{gender}")


# 位置参数
user_info("小米", 18, "男")

# 关键字参数  ----- 键=值
user_info(name="小米", age=18, gender="男")
user_info(age=18, gender="男", name="小米")
user_info("小米", age=18, gender="男")


# 缺省参数  ------- 必须在最后
def user_info1(name1, age1, gender1="男"):
    print(f"姓名:{name1},年龄:{age1},性别:{gender1}")


user_info1(name1="小米", age1=18)
user_info1(name1="小米", age1=18, gender1="女")


# 不定长参数   ------ 类型1:位置传递  类型2:关键字传递
# 位置传递不定长  ---- 元组的形式
def user_info2(*args):  # 元组
    print(args, type(args))


user_info2(1, 2, "你好", "hello")


# 关键字传递不定长  ---- 字典的形式
def user_info3(**kwargs):  # 字典
    print(kwargs, type(kwargs))


user_info3(a=1, b=2, c="你好", d="hello")


# 函数作为参数传递 ----- 计算逻辑的传递,而非数据的传递
def user_info4(compute):
    result = compute(1, 2)
    print(result,type(compute))   # 3 <class 'function'>

def compute(x, y):
    return x + y

user_info4(compute)
How to write a function that returns multiple return values?
According to the order of the return value, just write multiple variables in the corresponding order to receive. The variables are separated by commas. Different types of data returns are supported.
def r_test():
    return 1, 2, 3


x, y, z = r_test()
print(x)
print(y)
print(z)

Guess you like

Origin blog.csdn.net/weixin_52053631/article/details/132866878