重要内置函数及递归函数
一,print()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
print('a','b','c') print('a','b','c',sep="&",end="?") f=open(file,mode="w",encoding="utf-8") print("666",file=f) # 将666写入文件f中
二,sum()
sum(可迭代对象,start) 可迭代对象可以是list,tuple,set,当字典的key为数字是,也可以传入
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
print(sum(1,2,3)) #报错 print(sum(1,2)) #报错 print(sum(list(range(5)))) #10 print(sum(tuple(range(5)))) #10 print(sum({"1":1,"2":2})) #报错 print(sum(set(range(5)))) #10
print(sum(list(range(5)),10)) #20
三,reversed() 返回的是一个可迭代对象,有索引值的可迭代对象可以用reversed函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
a=reversed(list(range(5))) print(list(a))
四,min()和max()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import random a=list(range(5)) random.shuffle(a) print(a) b=min(a,key=lambda x:-x) print(b) dic={"a":1,"b":2} print(max(dic,key=lambda x:dic[x]))
五,sorted() 从小到大排序,返回的是一个列表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
print(sorted("abc")) #['a', 'b', 'c'] print(sorted({"a":1,"b":2})) #['a', 'b']
六,zip() 拉链方法(生成列表的元素个数等于传入可迭代对象元素个数最少的那个) 返回的是可迭代对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
a=[1,2,3] b=["a","b","c"] print(list(zip(a,b))) [(1, 'a'), (2, 'b'), (3, 'c')] a=[1,2,3] b=("a","b","c") print(list(zip(a,b))) [(1, 'a'), (2, 'b'), (3, 'c')] a=[1,2,3] b={"a":1,"b":2} print(list(zip(a,b)))
七,filter() 过滤方法 返回的是可迭代对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
dic={"a":8,"b":2,"c":3} a=filter(lambda x:dic[x]>5,dic ) print(list(a))
八,map() 循环方法 返回的是可迭代对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
dic={"a":8,"b":2,"c":3} print(list(map(lambda x:dic[x]*2, dic))) #[16, 4, 6]
递归函数 函数在函数的内部调用自己
斐波那契数列 0,1,1,2,3,5,8,13
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def func(n): if n==1: return 0 elif n==2: return 1 else: return func(n-1)+func(n-2) print(func(7)) #8