2.python中一个函数传递任意个参数

python中一个函数传递任意个参数

在python中,函数可以由任意个参数,而不必在声明函数时对所有参数进行定义,用循环实现,在参数名前加一个星号*,则可以表示该参数是一个可变长参数。示例如下

def mylistappend(*a):
    l=0
    for i in a:
        l = l  +  i
    return l

print(mylistappend(1,2,3))

F:\python下载\python.exe F:/python下载/practical/untitled1/wen_03.py
6

Process finished with exit code 0





def mylistappend(*list):
    l=[]
    for i in list:
        l.extend(i)
    return l

a = [1,2,3,4]
b = [5,6,7,8]
c = [9,10,11,12]
print(mylistappend(a,b))
print(mylistappend(a,b,c))

结果:F:\python下载\python.exe F:/python下载/practical/untitled1/wen_03.py
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Process finished with exit code 0

 

;