Day30.内置函数
1.内置函数_1到21
print('1.绝对值:', abs(-1)) print('2.遍历可迭代对象都为True,结果为True:', all([1, 'aaa', 2]), all([])) print('3.遍历可迭代对象都为False,结果为False:', any([None]), any([])) print('4.bin将十进制转为二进制:', bin(11)) print('5.oct将十进制转为八进制:', oct(11)) print('6.hex将十进制转为十六进制:', hex(11)) print('7.bool判断布尔值数据类型:', bool('')) def func(): pass class Foo: pass print('8.callable判断对象是否能被调用:', callable(func), callable(Foo)) print('9.chr将数字转成字符:', chr(65)) print('10.ord将字符转成数字:', ord('A')) class Foo: pass obj = Foo() print('11.dir查看对象下面有哪些属性_掌握:', dir(obj)) print('12.divmod计算商和余数_掌握:', divmod(10, 3)) for index, data in enumerate(['a','b','c']): print('13.enumerate枚举获取索引和对应的值:', index, data) res1 = eval('1+2') res2 = eval('{"a":1, "b":2}') print('14.eval执行字符串中的表达式:', res1, type(res1), res2, type(res2)) print('15.frozenset不可变集合:', frozenset({1,2,3})) class Foo: pass obj = Foo() print('16.isinstance判断obj是否是Foo的实例:', isinstance(obj, Foo), isinstance([1], list)) print('17.pow一个数的次方再除以一个值的余数:', pow(10, 2, 3)) # 等同于 10 ** 2 % 3 print('18.round四舍五入:', round(1.5), round(1.4)) l1 = ['a', 'b', 'c', 'd'] l2 = ['aaa', 'bbb', 'ccc', 'ddd', 444] s = slice(1, 4, 2) print('19.slice实现切片操作:', l1[s], l2[s]) # 等同于l1[1:4:2] l2[1:4:2] v1 = 'hello' v2 = [11,22,33,44,55,66] res = dict(zip(v1,v2)) print('20.zip实现两个可迭代对象一一对应_掌握:', res) # import 'time' # todo import后面跟字符串模块名导入报错, 需要使用__import__('time'),可以正常用导入字符串模块名 x = __import__('time') x.sleep(3)