内置函数

 

exec()执行程序  eval()评价

print(exec('1+2+3+4')) # 没有返回值
print(eval('1+2+3+4'))  #有返回值
exec('print(123)')
eval('print(123)')

 

hash()

print(hash('name.asg'))

 

callable() 检测对象是不是函数

print(callable('i'))
print(callable(input))

 

sep=' ' 自动插入
print(1,2,3,4,5,6,sep='|')  #1|2|3|4|5|6

 

filter()过滤器  map()  zip()

 

求偶数
def odd_numbered(h):
    return h % 2!=0

ret =filter(odd_numbered,[1,4,2,6,5,9])
for i in ret:
    print(i)

求字符串
def odd_numbered(h):
    return type(h) ==str

ret =filter(odd_numbered,[1,'naem',4,2,'number',6,5,9])
for i in ret:
    print(i)

求字符串和偶数
def odd_numbered(h):
    return type(h) ==str or h % 2==0

ret =filter(odd_numbered,[1,'naem',4,2,'number',6,5,9])
for i in ret:
    print(i)

变成字典1
ret=zip((('a'),('b')),(('c'),('d')))
def func(tup):
    return {tup[0]:tup[1]}
ret =map(func,ret)
print(list(ret))

变成字典2
ret=zip((('a'),('b')),(('c'),('d')))
ret=map(lambda tup:{tup[0]:tup[1]},ret)
print(list(ret))

 

 

 

 

bin() oct() hex() 进制转换

print(bin(10)) # 二进制  
print(oct(10)) #八进制
print(hex(10)) #十六进制

结果
0b1010
0o12
0xa

 

ads()   divmod()   round()   pow()  sum()数学运算

 

print(abs(1.2))  #计算绝对值
print(divmod(20,3))  #返回(除,余)
print(round(55))     # 小数精确
print(pow(4,5))      #幂运算
k=[1,2,3]
print(sum(k))        #求和

 

format()指定输出位置

print(format('list'))
print(format('list','>40'))
print(format('list','^40'))

结果
list
                                    list
                  list 

 

encoding编码转换

print(bytes('你好明天',encoding='GBK'))
print(bytes('你好明天',encoding='UTF-8'))
print(bytes('你好明天',encoding='UTF-32'))

 

posted on 2018-09-21 15:50  飘零花落  阅读(73)  评论(0)    收藏  举报