![image]()
Day17
异常捕获补充(了解)
![image]()
for循环本质
![image]()
迭代取值与索引取值对比
![image]()
生成器对象
生成器就是自定义迭代器
![image]()
自定义range功能
![image]()
yield传值
![image]()
yield与return对比
![image]()
生成器表达式
![image]()
练习
![image]()
常见内置函数
1.abs(123) # 绝对值
print(abs(-123))
print(abs(123))
2.all() any()
l = [11,22,33,0]
print(all(l)) # 所有的元素都为True结果才是True
print(any(l)) # 所有的元素只要有一个为True结果就为True
3.bin() oct() hex() # 进制数
print(bin(123))
4.bytes() str() # 编码解码
res = '金牌班 最牛逼'
res1 = bytes(res,'utf8')
print(res1)
res2 = str(res1,'utf8')
print(res2)
5.callable() # 是否可调用(能不能加括号调用)
s1 = 'jason'
def index():
pass
print(callable(s1),callable(index)) # False True
6.chr() ord()
print(chr(65)) # 根据ASCII码转数字找字符
print(ord('A')) # 65
7.complex() # 复数
print(complex(123)) # (123+0j)
8.dir() # 查看当前对象可以调用的名字
def index():
pass
print(dir(index))
print(index.__name__)
9.divmod() # 除是否有余数
print(divmod(101,10))
"""总数据100 每页10条 10页"""
"""总数据99 每页10条 10页"""
"""总数据101 每页10条 11页"""
num,more = divmod(233,10)
if more:
num += 1
print('总共需要%s页'%num)
10.eval() # 只能识别简单的语法 exec()可以识别复杂语法 都是将字符串里的数据内容加载并执行
res = """
你好啊
for i in range(10):
print(i)
"""
eval(res)
exec(res)
11.isinstance() # 判断是否属于某个数据类型
print(isinstance(123,float)) # False
print(isinstance(123,int)) # True
12.pow() # 平方
print(pow(4,3))
13.round() # 四舍五入(五舍六入)
print(round(4.8))
print(rounf(4.6))
print(round(8.5))
14.sum() # 求和
l = [11,22,33,44,55,66]
print(sum(l))