python学习笔记(3)-函数

函数

def MyFirstFunciton():
    print('这是我创建的第一个函数')
    print('我表示很激动')
    print('感谢CCTV')

MyFirstFunciton()

def MySecondFunction(name):
    print(name + 'woaini')

MySecondFunction('xiaojiayu')

def add(num1,num2):
    return (num1+num2)
print(add(1,2))

这是我创建的第一个函数
我表示很激动
感谢CCTV
xiaojiayuwoaini
3

形参和实参

  • 形参:def MyFirstFunction(name):
    函数定义过程中的name是叫形参,因为ta知识一个形式,表示占据一个参数位置
  • 实参:def MyFirstFunction(‘小甲鱼’):
    传递进来的小甲鱼叫做实参,因为Ta是具体的参数值。

关键字参数

def saysome(name,words):
    print(name + '->' + words)
saysome(words = '让编程改变',name = '小甲鱼')#多加几个关键字可以不需要管他们的位置

默认参数

当调用函数的时候如果没有传递参数,函数会按照默认参数来进行运行

def saysome(name = '小甲鱼',words = '让编程改变世界 '):
    print(name + '->' + words)
saysome()
saysome('小鱿鱼')
saysome('小鱿鱼','你好啊')

收集参数

def test(*params):
    print('参数的长度是',len(params))
    print(params[1])
test(1,2,3,4,5,6)

结果:

参数的长度是 6
2

当形参既有收集参数也有普通形参的时候:

def test(*params,exp):
    print('参数的长度是',len(params),exp)
    print(params[1])
test(1,2,3,4,5,6,exp=9)#这个exp的参数要进行命名才能传递进去,不然都话全部都认为是收集参数,同时建议在定义函数的时候最好直接给默认参数。

函数与过程

返回值

python是动态地确定类型,是可以返回多个值的

def back():
	return[1,'xiaojia',3]
back()#返回列表
def back1():
	return 1,'xiaojia',3
back1()#返回元组
函数的作用域问题
  • 局部变量
def discount(price,rate):
    final_price = price * rate
    return final_price

old_price = float(input('请输入原价'))
rate = float(input('请输入折扣率'))
new_price = discount(old_price,rate)
print('打折后的价格是:',new_price)
print(final_price)#会报错,是访问不到定义函数里边的变量的

结果:

请输入原价100.0
请输入折扣率0.7
打折后的价格是: 70.0
  • 假如在函数里试图修改全局变量,会在函数里暂时新建一个和全局变量名字一样的局部变量。
def discount(price,rate):
    final_price = price * rate
    old_price = 50
    print('old_price的值为1:',old_price)
    return final_price

old_price = float(input('请输入原价'))
rate = float(input('请输入折扣率'))
new_price = discount(old_price,rate)
print('old_price的值为2:',old_price)
print('打折后的价格是:',new_price)
请输入原价100
请输入折扣率0.7
old_price的值为150
old_price的值为2100.0#可以发现这个打印的又变成的全局变量的值
打折后的价格是: 70.0

内嵌函数和闭包

  • global关键字
count = 5
def MyFun():
    count = 10
    print(10)
MyFun()
print(count)#因为全局变量是5
count = 5
def MyFun():
    global count = 10#告诉编译器变成全局变量
    print(10)
MyFun()
print(count)

结果:

10
5

10
10
  • 内嵌函数
def fun1():
    print('fun1zhengzaidiaoyong')
    def fun2():
        print('fun2zhengzaidiaoyong')
    fun2()#只能在fun1里调用fun2,在外部调用不了
fun1()
  • 闭包
def FunX(x):
    def FunY(y):
        return x * y
    return FunY
#i = FunX(8)(5)
print(FunX(8)(5))
  • lambda函数的使用,它的好处在于可以省下定义函数的过程使得程序更加精简,并且不需要考虑命名的问题。
def ds(x):
    return 2 * x + 1
a = ds(5)
print(a)

g= lambda x: 2 * x + 1
print(g(5))

g = lambda x,y : x + y
print(g(2,3))
  • 两个牛逼的BIF函数
    filter():过滤器,保留所关注的信息,把非true的内容过滤掉
a=filter(None,[1,0,True])
print(list(a))

temp = range(10)
print(temp)

def odd(x):
    return x % 2
temp = range(10)
show = filter(odd,temp)#要的是返回值
print(list(show))
#如果用lambda函数
list(filter(lambda x : x % 2,range(10)))#同样可以实现功能
[1, True]
range(0, 10)
[1, 3, 5, 7, 9]

map():用作映射

print(list(map(lambda x : x * 2, range(10))))

结果:

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kelinnn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值