容器序列
list、tuple collections.deque序列能存放不同类型的数据
扁平序列:
str、bytes、bytearray、memoryview和array.array 类型只能一种类型
可变序列
list、bytearray、array.array、collections.deque和memoryview.
不可变序列
tuple、str、bytes
字典
collections.ChainMap
把函数视作对象 factorial 函数类
>>> def factorial(n):
... 'return'
... return 1 if n<2 else n* fectorial(n-1)
...
>>> factorial(10)
3628800
>>> factorial(42)
1405006117752879898543142606244511569936384000000000
函数定义
>>> fact=fectorial
>>> fact(12)
479001600
高阶函数
参数是函数,如map函数
根据单词长度对一个列表排序
>>> fruits=['strawberry','fig','apple','cherry','banana']
>>> sorted(fruits,key=len)
#按照长度排序
['fig', 'apple', 'cherry', 'banana', 'strawberry']
>>> sorted(fruits)
#安装ascii排序
['apple', 'banana', 'cherry', 'fig', 'strawberry']
根据列表单词反过来拼写
>>> def reverse(word):
... return word[::-1]
...
直接调用
>>> print(fruit)
['strawberry', 'fig', 'apple', 'cherry', 'banana']
>>> sorted(fruit,key=reverse)
['banana', 'apple', 'fig', 'strawberry', 'cherry']
python3 里面的map和filter还是内置函数
例如:阶乘
>>> list(map(fact,range(6)))
[1, 1, 2, 6, 24, 120]