Python3语法之函数补充(函数编程和内置函数)思维导图

无情 阅读:655 2021-03-31 13:42:23 评论:0

Python3 相关功能代码:

import io 
import sys 
import math 
# 调用reduce 函数 
from functools import reduce 
  
sys.stdout = io.TextIOWrapper(sys.stdout.detach(),encoding='utf-8') 
 
# Python3 高阶函数之函数即变量 
 
# print函数赋值给变量p 
p = print 
# 通过变量p 调用print函数 
p("高阶函数之函数即变量") 
 
# Python3 高阶函数之传入函数 
 
def add(x,y,z): 
    return abs(x) + abs(y) 
result = add(-11,128,abs) 
print("result is:",result) 
 
# Python3 匿名函数 
 
r = 10 
area = lambda r:math.pi*r*r 
print('半价为',r,'的面积为:',area(r)) 
 
# Python3 map 函数 
 
number =[1,2,3,4,5,6,7,8,9] 
print(list(map(lambda x: x*x, number))) 
 
# Python3 reduce 函数 
 
number =[1,2,3,4,5,6,7,8,9] 
print(print(reduce(lambda x,y:x+y, number))) 
 
# Python3 filter 函数 
 
st =['A', '', 'C', 'D', ''] 
print(list(filter(lambda x: x and x.strip(),st))) 
 
# Python3 sorted 函数之数字排序 
 
a =[11,90,-1,20,17] 
print(sorted(a)) 
 
# Python3 sorted 函数之字符串排序 
 
aa =['a','12','b','z'] 
print(sorted(aa)) 
 

 

 

标签:Python
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号