第 018讲:函数:灵活即强大(关键字函数,默认函数,收集函数)

数:

0.形参和实参:

>>> def saysomething(name, words): \\name是形参; Jenny是实参
print(name + '->' + words)


>>> saysomething("jenny", "happy")
jenny->heppy

1.关键字函数:

>>> def saysomething(name, words):
print(name + '->' + words)

 

>>> saysomething(words = "happy", name = "jenny")
jenny->happy

2. 默认函数:

>>> def saysomething(name = "jenny", words = "happy"):
print(name + '->' + words)

>>> saysomething()
jenny->happy

 

>>> def saysomething(name, words):
print(name="jenny" + '->' + words="happy") \\不能在print里面赋实参

SyntaxError: invalid syntax

3. 收集函数(可变参数):

 

>>> def test(* paratest):
print("有 %d 个参数" % len(paratest)) \\  不明白
print("第二个参数是:", paratest[1])

 


>>> test('j','e','n','n','y')
有 5 个参数
第二个参数是: e

 

 

 

 

posted @ 2018-05-24 19:36  Jenny测试开发  阅读(164)  评论(0编辑  收藏  举报