python(13)——内置函数

input()
type()
len()
print()
enumerate()
list()
dict()
tuple()
set()
str()
int()
float()
bool()

max()
min()
print(round(3.1415926,2)) #取几位小数

a=[4,7,2,41,6]
print(sorted(a)) #排序   升序:由小到大
print(list(reversed(sorted(a)))) #reversed() 反转
print(sorted(a,reverse=True)) #排序  降序

print(ord('a')) #97 字母转换成ascii码表里面的值
print(chr(97))  #a ascii码表里面的值转换成字母

print(any([1,1,0]))  #True 如果这个list里有一个为真,就返回True
print(all([1,2,3,0]))  #False  list中所有值为真则返回真

print(dir(hashlib))    #打印传入对象/模块的可调用方法

# eval() #执行简单的python代码  只能执行简单的,定义数据类型和运算
f=open('a.txt').read()  # a.txt中的内容  {'username':'abc','password':12345}
print(type(f))  #<class 'str'>
res= eval(f)  
print(type(res)) #<class 'dict'>  eval()将字符串转成字典

# exec ()   #执行python代码
my_code='''
def my():
    print('hehe')
my()
'''
exec(my_code) #执行python代码  不安全

# map()  循环帮你调用函数,然后保存函数的返回值
def bl(i):   # 补零函数
  return  str(i).zfill(2)
l='123456'
res =list(map(bl,l)) #map返回的是一个生成器,用list()方法转成list, bl为函数名,l为一个可循环的对象
print(res)  #['01', '02', '03', '04', '05', '06']

# filter() #filter也是循环调用函数,如果这个函数返回的值是真,那么就保存l中的这个值
l='12345654'
def hh(i):
    if i>'3':
        return True
print(list(filter(hh,l))) #['4', '5', '6', '5', '4']  l中大于3的值
原文地址:https://www.cnblogs.com/HathawayLee/p/9794488.html