函数
- why
最大化代码重用
最小代码冗余
过程分解 - 定义
def函数名(参数1…);函数体 - 调用
函数名(实际参加) - 变量作用域
Bulit-in
Global
Enclousure
Local - 参数
传递:
不可变类型,传递副本给函数,函数内操作不影响原始值
可变类型,传递地址引用,函数内操作肯能会影响原始值
匹配:
位置匹配
关键字匹配
默认值(调用时省略传值)
*args 任意数量参数
**kwargs - Lambda表达式
定义匿名函数
基本格式 Lambda 参数1,…函数 - 高级工具
map(函数,可迭代对象)
filter(函数,可迭代对象)
#定义函数
def read_book():
print('拿到一本书')
print('看书')
print('收起')
#调用函数
read_book()
拿到一本书
看书
收起
def learning(name,course,start,end):
print('{}报名课程:《{}》'.format(name,course))
print('从第{}节学习到第{}节'.format(start,end))
print('{}学习结束'.format(name))
learning('Tom','Python入门',1,3)
Tom报名课程:《Python入门》
从第1节学习到第3节
Tom学习结束
def add_number(x,y):
result = x + y
print(result)
add_number(5,3)
8
def add_number(x,y):
result = x + y
return result
print(add_number(5,3))
8
def add_number(x,y):
result = x + y
return result
a = 10
result = a + add_number(5,3)
print(result)
18
def multiply(x,y):return x*y #逻辑简单可以写同一行
def multiply(x,y):
return x*y
print(multiply(3.14,5))
15.700000000000001
def intersect(seq1,seq2):
res = []
for x in seq1:
if x in seq2:
res.append(x)
return res
s1 = 'assdfefea'
s2 = 'ahhfesc'
l = intersect(s1,s2)
print(l)
[‘a’, ‘s’, ‘s’, ‘f’, ‘e’, ‘f’, ‘e’, ‘a’]
x = 55
def func():
x = 99
print(x)
print('全局x:',x)
print('函数内x:')
func()
全局x: 55
函数内x:
99
x = 55
def func():
global x
x = 99
print(x)
print('全局x:',x)
print('函数内x:')
func()
print('全局x',x)
全局x: 55
函数内x:
99
全局x 99
def func():
x = 100
def nested():
x = 99
print(x)
nested()
print(x)
func()
99
100
def func():
x = 100
def nested():
x = 99
print(x)
nested()
print(x)
func()
99
100
def func(): #用外侧封装的x
x = 100
def nested():
x = 99
print(x)
nested()
print(x)
func()
99
100
def func(): #用外侧封装的x
x = 100
def nested():
nonlocal x #非本地的x
x = 99
print(x)
nested()
print(x)
func()
99
99
def change_number(x):
x += 10
x = 5
print('x={}'.format(x))
change_number(x)
print('x={}'.format(x))
x=5
x=5
def change_number(x): #5传值的时候先做一个副本5,然后传递给函数,不会影响原来的值
x += 10
return x
x = 5
print('x={}'.format(x))
change_number(x)
print('x={}'.format(x))
x=5
x=5
def change_list(l):
l[0] = 99
l = ['qaa','dfdf','fdfdf']
print('原始列表:',l)
change_list(l)
print('操作后列表:',l)
原始列表: [‘qaa’, ‘dfdf’, ‘fdfdf’]
操作后列表: [99, ‘dfdf’, ‘fdfdf’]
def change_list(l):
l[0] = 99
l = ['qaa','dfdf','fdfdf']
print('原始列表:',l)
change_list(l.copy()) #这样就是复制了个副本也可以写l[:]
print('操作后列表:',l)
原始列表: [‘qaa’, ‘dfdf’, ‘fdfdf’]
操作后列表: [‘qaa’, ‘dfdf’, ‘fdfdf’]
def change_str(s):
s = 'abc'
url = 'shshshhh'
print('原始字符串:',url)
change_str(url)
print('操作后字符串:',url)
原始字符串: shshshhh
操作后字符串: shshshhh
def func(a,b,c):
print(a,b,c)
func(1,2,3)
1 2 3
func(c=1,a=2,b=3) #关键字匹配
2 3 1
def func(a,b=2,c=3): #b,c取默认值
print(a,b,c)
func(1)
1 2 3
def avg(score1,score2):
return (score1 + score2)/2
result = avg(98,87)
print(result)
92.5
def avg(*scores): # *接受一个touple
return sum(scores)/len(scores)
result = avg(98.8,88.1,66.4,70)
print(result)
80.82499999999999
def display(**employee): # **接受一个字典表
print(employee)
display( name='Tom', age=22,job='dev')
{‘name’: ‘Tom’, ‘age’: 22, ‘job’: ‘dev’}
d = {'name':'Jerry','age':22}
d2 = dict(name = 'Mike',age = 22,job='dev') #两种等价
def hello_Chinese(name):
print("你好",name)
def hello_English(name):
print('Hello',name)
hello = hello_Chinese #不加(),委托
hello('Tom')
你好 Tom
hello = hello_English #不加(),委托
hello('Tom')
Hello Tom
def hello_Chinese(name):
print("你好",name)
def hello_English(name):
print('Hello',name)
operation = {
'e':hello_English,
'c':hello_Chinese,
'r':lambda name : print('привет ',name)
}
while True:
name = input('请输入姓名:\n')
if name =='stop':
break
language = input('请选择语言版本:\n c =>中文版\n e =>英文版\n r=>俄文版\n')
operation.get(language,hello_Chinese)(name) #如果输错了成中文版
请输入姓名:
mike
请选择语言版本:
c =>中文版
e =>英文版
r=>俄文版
r
привет mike
请输入姓名:
stop
l = list(range(1,21))
res = []
def add_number(x):
return x+5
#1.使用循环
for n in l:
res.append(n+5)
print(res)
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
# 2.使用推导
res = [x + 5 for x in l ]
print(res)
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
# 3.map()
res = list(map(add_number,l))
print(res)
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
# 求平方和
res = list(map((lambda n : n**2),l))
print(res)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
l = list(range(1,11))
def even_number(x):
return x % 2 == 0
res = list(filter(even_number,l))
print(res)
[2, 4, 6, 8, 10]
l = list(range(1,11))
def even_number(x):
return x % 2 == 0
res = filter(even_number,l)
for n in res:
print(n,end=' ')
2 4 6 8 10
res = filter((lambda n : n%2 == 0),l)
for n in res:
print(n,end=' ')
2 4 6 8 10