作者:zb
1、python内置方法:
abs()方法:取绝对值
all()方法:all(iterable),如果iterable中的所有元素都为空或True,则返回True,否则返回False
# all()方法 print(all([0, 1, -2])) print(all([1, 1, 2])) print(all([]))
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py False True True Process finished with exit code 0
any()方法:any(iterable),如果iterable中有一个为真,则为真,否则为假;如果iterable为空,则为假
# any() print(any([0, 1, -2])) print(any([1, 1, 2])) print(any([]))
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py True True False Process finished with exit code 0
ascii()方法:ascii(object),把一个内存数据对象编程一个可打印的字符串
# ascii() a = ascii([1, 2, "朱博"]) print(type(a), a) print(type(a), [a])
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py <class 'str'> [1, 2, '\u6731\u535a'] <class 'str'> ["[1, 2, '\\u6731\\u535a']"] Process finished with exit code 0
bin()方法:十进制转二进制
# bin() print(bin(2)) print(bin(3)) print(bin(4)) print(bin(5)) print(bin(6)) print(bin(8))
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 0b10 0b11 0b100 0b101 0b110 0b1000 Process finished with exit code 0
oct()方法:十进制转八进制
# oct()方法,逢八进一 print(oct(1)) print(oct(2)) print(oct(8)) print(oct(9)) print(oct(16)) print(oct(24))
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 0o1 0o2 0o10 0o11 0o20 0o30 Process finished with exit code 0
hex()方法:十进制转十六进制
# hex()方法 print(hex(10)) print(hex(16)) print(hex(17)) print(hex(32))
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 0xa 0x10 0x11 0x20 Process finished with exit code 0
bool()方法:判断真假
# bool() print(bool(0)) print(bool(1)) print(bool([]))
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py False True False Process finished with exit code 0
bytearray()方法:二进制数组,可以被修改的二进制数据类型,修改的时候需要按照二进制的ASCII格式进行修改;普通的bytes数据类型无法被修改
# bytes()类型无法被修改 a = bytes('abcde', encoding='utf-8') print(a.capitalize(), a) # bytearray()类型可以被直接修改 b = bytearray('abcde', encoding='utf-8') b[1] = 200 print(b)
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py b'Abcde' b'abcde' bytearray(b'a\xc8cde') Process finished with exit code 0
callable()方法:判断一个数据类型是否可以被调用的方法,只有后面加括号的数据类型才可以被调用,例如函数
# callable():是否可以被调用 a = callable([]) print(a) print(callable(type))
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py False True Process finished with exit code 0
chr()方法:将ascii数字转换成ascii字符
# chr()方法 print(chr(98))
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py b Process finished with exit code 0
ord()方法,将ascii字符转换成ascii数字
# ord()方法 print(ord('b'))
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 98 Process finished with exit code 0
exec()方法:将源代码编译成执行结果
code = """ def fib(max): n,a,b = 0,0,1 while n<max: yield b a,b = b,a+b n+=1 return "超过了最大循环次数" f = fib(10) # 写一个判断异常的代码 while True: try: x = next(f) print('f', x) except StopIteration as e: print("运行出错:", e.value) break """ exec(code)
D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py f 1 f 1 f 2 f 3 f 5 f 8 f 13 f 21 f 34 f 55 运行出错: 超过了最大循环次数 Process finished with exit code 0
divmod()方法:地板除
# divmod()方法,地板除 print(divmod(5, 2)) D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py (2, 1) Process finished with exit code 0
eval()方法:将字符串变成字典
filter()方法:在一组数据中过滤出你想要的数据
# filter方法: res = filter(lambda n: n > 5, range(10)) for i in res: print(i) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 6 7 8 9 Process finished with exit code 0
map()方法:类似于列表生成式,按照一定的规则生成数据
# # map()方法: res = map(lambda n: n * 2, range(10)) for i in res: print(i) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 0 2 4 6 8 10 12 14 16 18 Process finished with exit code 0
reduce方法:类似于map()方法
# # reduce方法 import functools # # 累加 res = functools.reduce(lambda x,y:x+y ,range(10)) print(res) # # 累乘 res = functools.reduce(lambda x,y:x*y,range(1,10)) print(res) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 45 362880 Process finished with exit code 0
frozenset()方法:集合本身是可以被修改的,该方法可以将集合变成不可变集合
# # frozenset()方法 a = frozenset([1,2,3,4]) print(a) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py frozenset({1, 2, 3, 4}) Process finished with exit code 0
globals()方法:以key:value的格式打印当前文件中所有的全局变量和变量值
# globals()方法 print(globals()) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00000179504F7C40>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:\\oldboy_py\\day4-20230828python内置方法\\1、内置方法.py', '__cached__': None, '__author__': '朱博'} Process finished with exit code 0
locals()方法:类似于globals()方法,打印局部变量
# locals()方法,只能在局部使用并且返回局部变量 def test(): local_var = 333 #print(local_var) print(locals()) print(globals().get('local_var')) test() print(globals().get('local_var')) print(locals().get('local_var')) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py {'local_var': 333} None None None Process finished with exit code 0
hash()方法:将字符串转换成数字的方法,算法中经常用到
# hash()方法 print(hash(123)) print(hash('zb')) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 123 4027742581229707217 Process finished with exit code 0
pow()方法:pow(a,b),计算a的b次方
# pow()方法,a的b次方 print(pow(3,4)) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 81 Process finished with exit code 0
repr()方法:将数据转换成字符串
# repr()方法 print(repr(123)) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 123 Process finished with exit code 0
round()方法:保留小数精度
# round()方法 print(round(1.44443333)) # 默认不保存 print(round(1.44443333,2)) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py 1 1.44 Process finished with exit code 0
slice()方法:切片,类似于列表切片
# slice()方法 d = range(20) print(d[slice(2,5)]) print(d[2:5]) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py range(2, 5) range(2, 5) Process finished with exit code 0
sorted()方法:把元素进行排序
# sorted()方法 a = {6:2,8:0,1:4,-5:6,99:11,4:22} # 默认按照key排序 print(sorted(a.items())) # 自定义按照value排序 print(sorted(a.items(), key=lambda x:x[1])) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py [(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)] [(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)] Process finished with exit code 0
zip()方法:拉链方法
# zip()方法,拉链 a = [1,2,3,4,5,6] b = ['a','b','c','d'] for i in zip(a,b): print(i) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/1、内置方法.py (1, 'a') (2, 'b') (3, 'c') (4, 'd') Process finished with exit code 0
__import__()方法:以字符串形式导入模块的方法
二、匿名函数
1、定义:
在函数不需要被重复调用的场景,可以使用lambda函数来处理,lambda函数使用后会立即被释放
2、应用:
匿名函数单独使用的场景很少,一般是和其他方法结合使用
# 常规的只调用一次的函数 def sayhi(n): print(n) sayhi(3) # 类似于上面的简单的函数,可以使用匿名函数来替代 (lambda n:print(n)) (3) calc = lambda n:print(n) calc(3) # 匿名函数只能使用一些简单的三元运算 a = lambda n:3 if n<4 else n print(a(5)) print(a(2)) # 执行结果 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day4-20230828python内置方法/2、匿名函数lambda.py 3 3 3 5 3 Process finished with exit code 0
标签:oldboy,内置,python,py,day14,print,方法 From: https://www.cnblogs.com/qq2751044056/p/17673904.html