__author__ = 'coco'
'''
python内置函数
'''
# all() 全为真,才为真
print(all([0,-2,3])) # False
print(all([1,-2,3])) # True
# any() 任意为真,则为真
print(any([1,-6,3])) # True
print(any([0,'',1])) # True
# bin() 十进制转二进制
print(bin(8)) #0b1000
# bool() 判断真假
print(bool(0)) # False
print(bool(-1)) # True
# bytes() 字符串转为二进制字节形式,不可修改
a = bytes("abcde",encoding="utf-8")
print(a.capitalize(),a)
#bytearray() 改二进制字符串
b = bytearray("abcde",encoding="utf-8")
print(b[1])
b[1]=50
print(b)
#callable() 判断是否可调用
def sayhi():pass
print(callable(sayhi)) # True
# chr() 返回ascii对应表
print(chr(97)) # a
# ord() ascii返回数字
print(ord('d')) # 100
# compile() 底层用于把代码编译用的
# exec() 可以直接执行代码,哪怕是已经赋值给了字符串
code='''
for i in range(10):
print(i)
'''
exec(code)
# delattr(object,name) # 删除object对象名为name的属性
# dict() 生成默认字典
# dir() 查询方法
print(dir(dict()))
# enumerate() 给列表生成索引
# eval() 运行python表达式
x = 1
print(eval('x+1')) # 2
# 匿名函数
calc = lambda n:print(n)
print(calc(5))
# 还可以三元运算
calc2 = lambda n:3 if n<4 else n
print(calc2(5))
# filter() 过滤
res = filter(lambda n:n>5,range(10))
for i in res:
print(i)
#map() 一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回。
res = map(lambda n:n*n,range(10))
for i in res:
print(i)
#==>
[ lambda i:i*2 for i in range(10) ]
#reduce() python2.x是reduce,python3.x移动到了functools里
# reduce把一个函数作用在一个序列[x1, x2, x3, ...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算
import functools
res = functools.reduce(lambda x,y:x+y,range(10))
print(res) # 45
# frozenset 这样就变成了不可变
a = frozenset(set([1,2,4,5,6,4]))
#globals() 返回程序变量字典
#hash() 哈希、散列 (映射关系)
#help() 帮助
#hex() 把一个数字转为16进制
print(hex(255)) #'0xff'
# locales() 打印局部变量
#next() 取下一个值
#oct() 转8进制
print(oct(8))
#pow() 返回多少次幂
print(pow(2,5))
#reversed() 反转
# round()
print(round(1.31678,2))
# sorted() 排序
# 对字典排序-->列表
#sorted(a.items()) #按key
#sorted(a.items(),lambda x:x[1]) #按value
#zip() 融合
a = [1,2,3,4]
b = ['a','b','c','d']
for i in zip(a,b):
print(i)
# import 'decorator'
# __import__('decorator')