内置函数 练习


print( all([1,-5,3]) )
print( any([]) )
a= ascii([1,2,"开外挂开外挂"])
print(type(a),[a])
print(bin(2555))
print(bool(0))
a = bytes('abcde',encoding='utf-8')
print(a.capitalize(),a)
b = bytearray('abcde',encoding='utf-8')#可修改字符串
print(b[0])
b[1]=100
print(b)
print(callable([]))#可调用与否,后边可加括号的为可调用
print(chr(87))
print(ord('w'))
code = 'for i in range(10):print(i)'
c = compile(code,'','exec')
exec(c)
code = '1+3/2*6'
c1 = compile(code,'','eval')
print(c1)
print(eval(c1))
a={}
print(dir(a))#dir,显示类的所有方法
print(divmod(5,2))
#eval(),将字符串转为字典
#exec()将字符串转为可执行代码
def sayhi(n):
print(n)
for i in range(n):
print(i)
sayhi(3)
#匿名函数
(lambda n:print(n))(5)
calc =lambda n:print(n)
calc(6)
res=filter(lambda n:n>5,range(10))
for i in res:
print(i)
res2 = map(lambda n:n*n,range(10))#[i*i for i in range(10)]
for i in res2:
print(i)
import functools
res3 = functools.reduce(lambda x,y:x+y,range(10))
print(res3)
a =frozenset ([1,4,33,4,5,6,7,5,8])#不可变列表

def test():
local_var =333
print(locals())
test()
print(globals())#返回当前程序所有的变量,以key-value形式表现
print(globals().get('local_var'))

print(hex(255))#转成十六进制
print(oct(9))#转成八进制
print(pow(2,5))
print(type(repr(123)),repr(123))#转成字符串对象
print(round(1.3364,2))#保留几们小数
d = range(20)
print(d)
print(d[slice(1,5)])
a = {6:2,8:0,1:4,-5:6,99:11,4:22}
print(sorted(a.items()))#排完序后变成列表
print(sorted(a.items(),key=lambda x:x[1]))#按value排序
a = [1,2,3,4,5,6]
b =[ 'a','b','c','d','e']
print(zip(a,b))
for i in zip(a,b):
print(i)
#__import__('decorator4')#引用字符串方式名





True
False
<class 'str'> ["[1, 2, '\\u5f00\\u5916\\u6302\\u5f00\\u5916\\u6302']"]
0b100111111011
False
b'Abcde' b'abcde'
97
bytearray(b'adcde')
False
W
119
0
1
2
3
4
5
6
7
8
9
<code object <module> at 0x0000000002152930, file "", line 1>
10.0
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
(2, 1)
3
0
1
2
5
6
6
7
8
9
0
1
4
9
16
25
36
49
64
81
45
{'local_var': 333}
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000000001D566D8>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'F:/python/2018/day4/内置方法.py', '__cached__': None, 'a': frozenset({1, 33, 4, 5, 6, 7, 8}), 'b': bytearray(b'adcde'), 'code': '1+3/2*6', 'c': <code object <module> at 0x0000000002152780, file "", line 1>, 'i': 81, 'c1': <code object <module> at 0x0000000002152930, file "", line 1>, 'sayhi': <function sayhi at 0x0000000001D0C1E0>, 'calc': <function <lambda> at 0x00000000023E02F0>, 'res': <filter object at 0x0000000002515978>, 'res2': <map object at 0x00000000025159B0>, 'functools': <module 'functools' from 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\lib\\functools.py'>, 'res3': 45, 'test': <function test at 0x00000000023E0840>}
None
0xff
0o11
32
<class 'str'> 123
1.34
range(0, 20)
range(1, 5)
[(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]
[(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)]
<zip object at 0x000000000240BF88>
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')
(5, 'e')

posted @ 2018-11-09 11:14  rongye  阅读(204)  评论(0编辑  收藏  举报