缺省参数,可变参数
Python中形参有确定参数,缺省参数,可变参数,关键字参数。具体的使用和区别如下:
确定参数:平时最常用的必传确定数量的参数即为确定参数
缺省参数:在调用函数时可以传也可以省去的参数,如果不传将使用默认值
可变参数:可变长度参数
关键字参数:长度可变,但是需要以kv对形式传参
举栗子:
# 缺省参数 即有默认值的参数
1
2 def test(a,b,c=33):
3 res = a+b+c
4 print(a)
5 print(b)
6 print(c)
7 print(res)
8
9 test(11,22)
10 print("-"*20)
11 test(11,22,44)
结果:
ubuntu@ubuntu-Lenovo:/work/python$ python3 缺省参数.py
11
22
33
66
--------------------
11
22
44
77
def test(a,*args):# *args是以元组的形式保存数据
i = 0
sum1 = 0
len1 = len(args)
print(args)
while i < len1:
sum1 += args[i]
i += 1
print("sum1 = %d" %sum1)
sum = a + sum1
print("sum = %d" %sum)
test(11,22,33,1,2,3)
#结果:
ubuntu@ubuntu-Lenovo:/work/python$ python3 不定长参数.py
(22, 33, 1, 2, 3)
sum1 = 61
sum = 72
def test(a,b,c=4,*args,**kwargs):#**kwargs是以字典形式存储
#也就是参数以 key=value的形式给出
print(a)
print(b)
print(c)
print(args)
print(kwargs)
test(11,22,3,4,5,task=6,done=7)
结果:
ubuntu@ubuntu-Lenovo:/work/python$ python3 不定长参数2.py
11
22
3
(4, 5)
{'task': 6, 'done': 7}
元组、字典的拆包
def test(a,b,*args,**kwargs):
print(a)
print(b)
print(args)
print(kwargs)
A = (33,44,55)
B = {'name':'laowang','age':18}
#test(11,22,A,B) #会报错
test(11,22,*A,**B)
结果:
ubuntu@ubuntu-Lenovo:/work/python$ python3 拆包_元组_字典.py
11
22
(33, 44, 55)
{'name': 'laowang', 'age': 18}
引用
我们之前讲的变量的定义赋值,在python语言中并不是给变量分配空间,这是区分于C/C++语言的,准确来说应该是引用。
比如:
a = 100
b = a
在C/C++中变量b会有自己的地址空间,但在python中,b和a的地址是一样的。可以使用id()去查看
In [1]: a = 100
In [2]: b = a
In [3]: id(a)
Out[3]: 10108256
In [4]: id(b)
Out[4]: 10108256
如果此时修改a = 200,是什么情况呢?
In [5]: a = 200
In [6]: a
Out[6]: 200
In [7]: b
Out[7]: 100
In [8]: id(a)
Out[8]: 10111456
In [9]: id(b)
Out[9]: 10108256
由上述可以下结论说数字是不可变类型。
那么我们学过的类型有哪些?
数字,字符串,元组,列表,字典
其中可变类型:列表,字典
不可变类型:数字,字符串,元组
In [10]: a = "hello" #字符串不可变类型
In [11]: a[0] = 'H'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-ecb470cdfa8a> in <module>()
----> 1 a[0] = 'H'
TypeError: 'str' object does not support item assignment
In [12]: a = (1,2)
In [13]: a[0] = 2 #元组是不可变类型
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-48f8c1dbf98d> in <module>()
----> 1 a[0] = 2
TypeError: 'tuple' object does not support item assignment
In [14]: a = [11,22,33]
In [15]: a[0] = 44 #列表是可变类型
In [16]: a
Out[16]: [44, 22, 33]
In [17]: a = {'name':'lw',1:2,(1,2):34} #字典是可变类型
In [18]: a
Out[18]: {(1, 2): 34, 'name': 'lw', 1: 2}
In [19]: a['name'] = 'laoli'
In [20]: a
Out[20]: {(1, 2): 34, 'name': 'laoli', 1: 2}
递归
#使用递归调用实现一个阶乘,如5!= 5*4*3*2*1
'''
def recall(5):
if 5 > 1:
return 5 * recall(5-1)
else:
return 5
def recall(4):
if 4 > 1:
return 4 * recall(4-1)
else:
return 4
def recall(3):
if 3 > 1:
return 3 * recall(3-1)
else:
return 3
def recall(2):
if 2 > 1:
return 2 * recall(2-1)
else:
return 2
def recall(1):
if 1 > 1:
return 1 * recall(1-1)
else:
return 1
'''
def recall(num):
if num > 1:
return num * recall(num-1)
else:
return num
num1 = int(input("请输入你要计算的阶乘数:"))
res = recall(num1)
print("%d!=%d"%(num1,res))
注意递归的退出条件,谨慎使用,一旦出错必然会导致程序崩掉。
匿名函数
格式:
lambda x,y:x+y
lambda代表的是匿名函数的标志
x,y是匿名函数的形参
x+y是函数的实体,也就是运算单元
注意匿名函数一般处理简单的功能
def test(a,b):
a+b
result1 = test(11,22) #没有定义返回值,所以结果为None
print(result1)
func = lambda x,y:x+y
result2 = func(11,22)#匿名函数自带返回值,适用于简单功能的函数
print(result2)
结果:
ubuntu@ubuntu-Lenovo:/work/python$ python3 匿名函数.py
None
33
匿名函数的应用
infors = [{"name":"laowang","age":18},{"name":"lisi","age":20},{"name":"xiaoming","age":10}]
infors.sort(key=lambda x:x['name'])#sort()从小到大排序,将匿名函数传入,匿名函数的返回值复制给key
print(infors)
infors.sort(key=lambda x:x['age'])
print(infors)
结果:
ubuntu@ubuntu-Lenovo:/work/python$ python3 匿名函数的应用.py
[{'name': 'laowang', 'age': 18}, {'name': 'lisi', 'age': 20}, {'name': 'xiaoming', 'age': 10}]
[{'name': 'xiaoming', 'age': 10}, {'name': 'laowang', 'age': 18}, {'name': 'lisi', 'age': 20}]
'''
def test(a,b): #该函数的功能定义死了,就是做个加法,但如果我想做减法,又需要定义一个新的函数
result = a+b
print(result)
'''
def test(a,b,func): #使用匿名函数定义一个灵活的函数
result = func(a,b)
print(result)
test(11,22,lambda x,y:x+y)
test(22,11,lambda x,y:x-y
结果:
ubuntu@ubuntu-Lenovo:/work/python$ python3 匿名函数的应用2.py
33
11
#coding=utf-8
'''
def test(a,b): #该函数的功能定义死了,就是做个加法,但如果我想做减法,又需要定义一个新的函数
result = a+b
print(result)
'''
def test(a,b,func): #使用匿名函数定义一个灵活的函数
result = func(a,b)
print(result)
func_new = input("请输入一个匿名函数:")
func_new = eval(func_new) #转换成python2中命令
test(11,22,func_new)
结果:
ubuntu@ubuntu-Lenovo:/work/python$ python3 匿名函数的应用3.py
请输入一个匿名函数:lambda x,y:x+y
33
ubuntu@ubuntu-Lenovo:/work/python$ python3 匿名函数的应用3.py
请输入一个匿名函数:lambda x,y:y-x
11