内置函数
以上这些都属于内置函数
1、max()
print(max(range(1,28))) #输出结果:27
2、min()
print(min(range(28))) #输出结果:0
3、sum()
print(sum(range(1,101))) #输出结果:5050
4、dir() # 输出msg具有的方法
msg = 'hello' print(dir(msg))
运行结果:
D:\work_soft\Miniconda3\python.exe F:/python-besttest/day05/aaa.py ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] Process finished with exit code 0
5、sorted()排序
res = sorted([2,3,1,2,3],reverse=True) #加上reverse就是降序排列了 print(res)
6、eval()
如果转json的话,尽量不用eval(),容易报错。转json的话,还是尽量用json模块
res = eval('[1,2,3,4,5]')#执行python代码,只能执行简单的python代码,1+1 a=1 print(res)
例子:
# f = open('a.json',encoding='utf-8') # result = eval(f.read()) #用来动态执行python代码的,简单的 # print(result)
7、exec()的用法
f = open('code',encoding='utf-8') code = f.read() exec(code)# 执行python代码的
例子:
s=''' 111112222333 ''' exec(s)#用来动态执行python代码的
8、枚举
stus = ['yyy','hhh','xxx','www'] for i in range(len(stus)): print(i,stus[i])
枚举:
stus = ['yyy','hhh','xxx','www'] for index,s in enumerate(stus): print(index,s)
9、多个list压缩到一起
stus = ['yyy','hhh','xxx','www'] sex = ['男','女','女','男'] age = [1,2] for name,se,ag in zip(stus,sex,age): #多个list压缩到一起 print(name,se,ag)
10、all()
print(all([1, 2, 3, 4])) # 判断可迭代的对象里面的值是否都为真,运行结果:True
print(all([1,2,3,4,False]))#判断可迭代的对象里面的值是否都为真,运行结果:False
11、any()
print(any([0, 1, 2, 3, 4])) # 判断可迭代的对象里面的值是否有一个为真,运行结果:True
12、round()
print(round(11.111, 2)) # 取几位小数,运行结果:11.11
13、
print(filter(lambda x: x > 5, [12, 3, 12, 2, 1, 2, 35])) # 把后面的迭代对象根据前面的方法筛选,运行结果:<filter object at 0x00000194A8F63F70>
14、
print(map(lambda x: x > 5, [1, 2, 3, 4, 5, 6]))
运行结果:<map object at 0x000001739AE73F70>
15、bin()
print(bin(10)) # 十进制转二进制
运行结果:0b1010
16、chr()
print(chr(100)) # 打印数字对应的ascii
运行结果:d
17、ord()
print(ord('b')) # 打印字符串对应的ascii码
运行结果:98
18、dir()
print(dir(1)) # 打印传入对象的可调用方法
运行结果:
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
19、globals()
print(globals()) # 返回程序内所有的变量,返回的是一个字典
运行结果:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001ED27955820>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:\\py-tmz-master\\tmz-code-master\\0day-lx\\01.py', '__cached__': None}
20、locals()
print(locals()) # 返回局部变量
运行结果:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000023015245820>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:\\py-tmz-master\\tmz-code-master\\0day-lx\\01.py', '__cached__': None}
21、hex()
print(hex(111)) # 数字转成16进制
运行结果:0x6f
22、oct()
print(oct(111)) # 把数字转换成8进制
运行结果:0o157
23、filter #过滤
map #
不用它俩也没问题,用它俩就是会节省代码
def oushu(number): if number%2==0: return True l = range(1,11) l2 = [] for i in l: if oushu(i): l2.append(i) result = list(filter(oushu,l)) # 这一行代码和上面五行代码是一模一样的效果 result2 = list(map(oushu,l)) print(l2) print(result) print(result2) result3 = list(map(str,range(1,101))) # print(result3)