parameter 形式参数
argument 实际参数
1. 函数
>>> def add(num1, num2):
result = num1 + num2
print(result)
>>> add(1,2)
3
2. 默认参数
>>> def SaySome(name='小甲鱼', words='让编程改变世界'):
print(name + '->' + words)
>>> SaySome()
小甲鱼->让编程改变世界
3. 收集参数
>>> def test(*parameter):
print('参数的长度是:',len(parameter))
print('第二个参数是:', parameter[1])
>>> test(1,'小甲鱼', 3.14, 5, 7, 8)
参数的长度是: 6
第二个参数是: 小甲鱼
>>> def test(*params, exp):
print('参数的长度是:',len(params),exp);
print('第二个参数是:',params[1]);
>>> test(1, '小甲鱼', 3.14, 5, 7, exp = 8)
参数的长度是: 5 8
第二个参数是: 小甲鱼
1. 函数返回值
>>> def hello():
print('Hello FishC!')
>>> temp = hello()
Hello FishC!
>>> temp
>>> print(temp)
None
>>> def back():
return [1, '小甲鱼', 3.14]
>>> back()
[1, '小甲鱼', 3.14]
>>> def back():
return 1, '小甲鱼', 3.14
>>> back()
(1, '小甲鱼', 3.14)
2. 局部变量和全局变量
文件1:
def discount(price, rate):
final_price = price * rate
old_price = 50
print('这里试图打印局部变量:',old_price)
return final_price
old_price = float(input('请输入原价:'))
rate = float(input('请输入折扣率:'))
new_price = discount(old_price, rate)
print('打折后的价格是:',new_price)
print('这里试图打印全局变量old_price:',old_price)
运行结果:
请输入原价:110
请输入折扣率:0.8
这里试图打印局部变量: 50
打折后的价格是: 88.0
这里试图打印全局变量old_price: 110.0
>>>