Python编程——函数

Python编程——函数

函数的作用:

降低编程难度,将大问题分解为小问题。

实现代码重用,提高工作效率。

1、传值调用

Eg1:

[root@cate my_python]# python test_func

please input the value of s:hello world

the value of s is: hello world

[root@cate my_python]#

Eg2:

[root@cate my_python]# python test_func

please input the value of s1:hello

please input the value of s2:hello

hello = hello

[root@cate my_python]# python test_func

please input the value of s1:hello

please input the value of s2:world

hello != world

[root@cate my_python]#

2、 默认参数(缺省参数)

Eg1:指定左边的参数

[root@cate my_python]# python test_func

hello != default

[root@cate my_python]#

Eg2:全面使用缺省参数

[root@cate my_python]# python test_func

default = default

[root@cate my_python]#

Eg3:指定右边的参数

[root@cate my_python]# python test_func

default != yum

[root@cate my_python]#

eg4:形参的缺省参数自右向左开始

[root@cate my_python]# python test_func

hello != world

3、函数的实参可以是任意值,包括数字、字符串、列表[]、元组()、字典{}

[root@cate my_python]# python

Python 2.7.5 (default, Apr 11 2018, 07:36:10)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> def f(x):

...     print x

...

>>> f(10)

10

>>> f('hello')

hello

>>> f([1,2,3])

[1, 2, 3]

>>> f(range(6))

[0, 1, 2, 3, 4, 5]

>>> f(('a','b','c'))

('a', 'b', 'c')

>>> f({1:111,2:222,3:333})

{1: 111, 2: 222, 3: 333}

>>>

4、传递一个元组给2个形参

5、传递一个字典给两个形参

6、 传递多个参数

7、传递可变参数

8、lambda匿名函数

Eg1:lambda匿名函数的含义

Eg2:lambda匿名函数的使用

reduce函数每次取两个数实现阶乘

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小橘猫cate

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

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

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

打赏作者

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

抵扣说明:

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

余额充值