其他内置函数
1.ord() 与chr()相反
2.pow()
print(pow(3,3)) # 相当于3**3 print(pow(3,3,2)) # 相当于3*3%2
运行结果:
27
1 Process finished with exit code 0
3.reversed() 颠倒顺序
4.round() 四舍五入
5.slice()
a = 'hello'
s1 = slice(3,5)
s2 = slice(1,4,2) # 2步长
print(a[s1])
print(a[s2])
运行结果:
lo
el Process finished with exit code 0
6.sorted() 比较之后排序
people = [
{'name':'a','age':123},
{'name':'b','age':46},
{'name':'c','age':12}
]
print(sorted(people,key=lambda dic:dic['age']))
运行结果:
[{'name': 'c', 'age': 12}, {'name': 'b', 'age': 46}, {'name': 'a', 'age': 123}] Process finished with exit code 0
7.sum()求和
8._import_() 可以导入字符串类型的模块。# import 调用.py格式的模块