4.2 函数形参、实参、默认参数

来源:互联网 发布:公众号小说收费源码 编辑:程序博客网 时间:2024/06/06 10:01

4.2 函数形参、实参、默认参数
定义函数时,------- 形式参数
调用函数时,------- 实际参数
example4.2.1:

>>> def fun(x):print('I get a :',x)>>> s = input('Please input something:')Please input something: cuixiaohui>>> fun(s)I get a : cuixiaohui
example4.2.2:
>>> def fun(x,y):if x == y:print(x,'=',y)else:print(x,'!=',y)>>> s1 = input('pls input sth:')pls input sth:lixiaotao>>> s2 = input('pls input sth:')pls input sth:cuixiaohui>>> fun(s1,s2)lixiaotao != cuixiaohui
默认参数
example4.2.3:
>>> def mashine(x,y='默认'):print('生成一个',x,'元',y,'口味的冰淇淋!')>>> mashine(5)生成一个 5 元 默认 口味的冰淇淋!

Attation:由右向左定义参数

>>> def mashine(x='默认',y):print('生成一个',x,'元',y,'口味的冰淇淋!')SyntaxError:non-default argument follows default argument

注:学习内容来源于网易云课堂《疯狂的Python:快速入门精讲》

0 0