python自学笔记13_Python 学习笔记(十三)Python函数(二)

本文介绍了Python函数中的参数和变量,包括按引用传递参数的概念,强调了局部变量和全局变量的区别。通过示例解释了命名空间、*args和**kwargs的参数收集方式。此外,还探讨了zip函数、lambda表达式以及map、reduce、filter等高阶函数的使用。
摘要由CSDN通过智能技术生成

参数和变量

1 >>> deffoo(a,b): #函数是一个对象2 return a+b3

4 >>> p =foo #对象赋值语句。将foo函数赋值给p这个变量5 >>> foo(4,5)6 9

7 >>> p(4,5) 变量p就指向了foo这个函数8 9

9 >>>

按引用传递参数

按照顺序进行赋值,函数中的变量a就指向了x,x是第一个实参,a这个参数指向了x所引用的对象,并不是把3这个数复制一个放到函数中,这种调用对象的方式,称之为按引用传递。

1 >>> x,y=3,4 #x,y两个变量分别指向了3和4

2 >>>foo(x,y) #函数中有两个参数a和b3 7

4 >>> defbar(x):5 x =1

8 >>> m = 9

9 >>>bar(m)10 >>>m11 9

12 >>>

一个程序的所有的变量并不是在哪个位置都可以访问的。访问权限决定于这个变量是在哪里赋值的。

变量的作用域决定了在哪一部分程序你可以访问哪个特定的变量名称。两种最基本的变量作用域如下:

全局变量和局部变量

定义在函数内部的变量拥有一个局部作用域,定义在函数外的拥有全局作用域。

局部变量只能在其被声明的函数内部访问,而全局变量可以在整个程序范围内访问。调用函数时,所有在函数内声明的变量名称都将被加入到作用域中

1 >>> x =2 #全局变量

2 >>> deffoo():3 x =9 #局部变量

4 print "this x is the fun:",x5

6

7 >>>foo()8 this x is the fun: 9

9 >>>x10 2

函数中的x 与函数外面的x,是不一样的。我们把像x=9这样的x,作用于某个函数体范围内的,称之为局部变量。

11 >>> defbar():12 globalx #在函数体中声明全局变量13 x =9

14 print "this x is the fun:",x15

16

17 >>>x18 2

19 >>>bar()20 this x is the fun: 9

21 >>>x22 9

23 >>>

命名空间:命名空间是对作用域的一种特殊抽象

参数收集和传值

收集方式1:*args

* args 是以元组的形式接收参数

1 >>> def foo(*arg):2 printarg3

4

5 >>> foo(1,2,3)6 (1, 2, 3) #元组形式接收参数7 >>> foo("baidu","ali","qq","weixin")8 ('baidu', 'ali', 'qq', 'weixin')9 >>> foo("web",[1,2,3,"pythom"])10 ('web', [1, 2, 3, 'pythom'])11 >>> def foo(x,*arg):12 print "x:",x13 print "arg:",arg14

15

16 >>> foo(1,2,3)17 x: 1

18 arg: (2, 3)19 >>> foo(7)20 x: 7

21 arg: ()22 >>>

收集方式2:**kargs 是以字典形式接收参数

1 >>> def foo(**karg):2 printkarg3

4

5 >>> foo(a=1,b=2,c=3)6 {'a': 1, 'c': 3, 'b': 2}7 >>> def foo(x,*arg,**karg):8 printx9 printarg10 printkarg11

12

13 >>> foo(1)14 1

15 ()16 {}17 >>> foo(1,2)18 1

19 (2,)20 {}21 >>> foo(1,2,3)22 1

23 (2, 3)24 {}25 >>> foo(1,2,3,name="python")26 1

27 (2, 3)28 {'name': 'python'}29 >>>

>>> defbook(author,name):print "{0} has a book :{1}".format(author,name)>>> bars={"name":"learn python with cc","author":"cc"}>>> book(**bars)

cc has a book :learn python with cc>>>

特殊函数

zip() 补充

1 >>> colors =["red","green","blue"]2 >>> values=[234,12,89,65]3 >>>zip(colors,values)4 [('red', 234), ('green', 12), ('blue', 89)]5 >>> dots=[(1,2),(3,4),(5,6)]6 >>> x,y=zip(*dots)7 >>>x8 (1, 3, 5)9 >>>y10 (2, 4, 6)11 >>> seq =range(1,10)12 >>> zip(*[iter(seq)]*3)13 [(1, 2, 3), (4, 5, 6), (7, 8, 9)]14 >>> x =iter(range(1,10))15 >>>x16

17 >>>list(x)18 [1, 2, 3, 4, 5, 6, 7, 8, 9]19 >>>zip(x,x,x)20 []21 >>> x=iter(range(1,10))22 >>>zip(x,x,x)23 [(1, 2, 3), (4, 5, 6), (7, 8, 9)]24 >>>

lambda  lambda x: x+y lambda 变量: 表达式

map、reduce、filter

1 >>> deffoo(x):2 x+-3

3 returnx4

5 >>> foo(4)6 4

7 >>> n =range(10)8 >>>n9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]10 >>> [i+3 for i inn ]11 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]12 >>> lam =lambda x:x+3

13 >>> n2=[]14 >>> for i inn15 SyntaxError: invalid syntax16 >>> for i inn:17 n2.append(lam(i))18

19

20 >>>n221 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]22 >>> g =lambda x,y:x+y23 >>> g(3,4)24 7

25 >>> lambda x:x+4

26 at 0x0000000003E91438>

27 >>>n28 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]29 >>>map(foo,n)30 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]31 >>> map(lambda x:x+3,n)32 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]33 >>> lst1 =[1,2,3,4,5]34 >>> lst2=[6,7,8,9,0]35 >>> map(lambda x,y:x+y,lst1,lst2)36 [7, 9, 11, 13, 5]37 >>> reduce(lambda x,y:x+y,lst1)38 15

39 >>> n =range(-5,5)40 >>>n41 [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]42 >>> filter(lambda x:x>0,n)43 [1, 2, 3, 4]44 >>> [x for x in n if x>0]45 [1, 2, 3, 4]46 >>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值