首页 Python基础入门视频教程 Python函数单参数、多参数和不定长参数使用
pay pay

Python函数单参数、多参数和不定长参数使用

日期: 二月 14, 2023, 8:13 a.m.
阅读: 555
作者: Python自学网-村长

摘要: Python函数单参数、多参数和不定长参数使用。

一、单参数

def p_num():
    print(3 ** 2)
    print(3 ** 3)
    print(3 ** 4)

p_num()
def p_num(n):  # 设置一个形参n
    print(n ** 2)
    print(n ** 3)
    print(n ** 4)

p_num(5)  # 传递实参为5

二、多参数

1.固定参数

def test():
    print(5 * 3)
    print(5 - 3)

test()

# 1.固定参数
def test(m, n):
    print(m * n)
    print(m - n)

test(5, 3)
test(985, 211)

2.关键参数

def test(m, n):
    print(m * n)
    print(m - n)

test(n=8, m=9)

3.缺省参数

def test(m, n=15):
    print(m * n)
    print(m - n)

test(m=9, n=14)
test(m=9)

三、不定长参数

def test(*m):  # 这里加了一个*号,当成元祖的形式来接受参数,一般写成:*args
    print(m)  # 注意这里的形参不能带*号
    print(type(m))

test((3, 5, 7))
test(3, 5, 7)
def test(**m):  # 这里加了2个*号,当成字典的形式接受参数,一般写成:**kwargs
    print(m)
    print(type(m))
    # print(**m)  # 这里不能加**

test(a=3, b=5, c=7)

 

原创视频,版权所有,未经允许,切勿转载,违者必究!
回顶部