# 嵌套
name="python"
def change():
name="python1"
def change2():
name="python2"
print("我是第二层",name)
change2()
print("我是第一层",name)
change()
print("我是最外层",name)
name="python"
def change():
# name="python1"
def change2():
# name="python2"
print("我是第二层",name)
change2()
print("我是第一层",name)
change()
print("我是最外层",name)
# 匿名,只能写简单的三元运算
def calc(x,y):
return x*y
print(calc(3,4))
a=lambda x,y:x*y
print(a(3,4))
def calc(x):
return x**2
res=map(calc,[1,2,3,4,5,6])
print(res)
for i in res:
print(i)
res1=map(lambda x:x**2,[1,2,3,4,5,6])
print(res1)
for i in res1:
print(i )
# 高阶
def get_abs(n): # 取绝对值函数
return int(str(n).strip("-"))
print(get_abs(-9999))
def get_abs1(n):# 取绝对值函数
if n>0:
return n
else:
return -n
print(get_abs1(-9999))
# 把一个函数a当作参数传给一个b函数,b 可以接收函数作为参数,b就是高阶函数,返回另外一个函数,也是高阶函数
def add(x,y,f):
return f(x)+f(y)
print(add(-7,-999,get_abs))