写在前面
最近有小伙伴吐槽,Python内置库这么多,要不要全部都学,记不住啊,这也太难记了吧!
当时我就想,为了小伙伴的学习着想,加班我也要解决这个问题!
说搞就搞,今晚直接加班把所有内置库函数整理出来了。

兄弟们学习python,有时候不知道怎么学,从哪里开始学。掌握了基本的一些语法或者做了两个案例后,不知道下一步怎么走,不知道如何去学习更加高深的知识。 那么对于这些大兄弟们,我准备了大量的免费视频教程,PDF电子书籍,以及源代码! 还会有大佬解答!直接在文末名片自取即可~
内置库一览
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()
delattr() hash() memoryview() set()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() ?lter() issubclass() pow() super()
bytes() ?oat() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
这时候,就会有小伙伴吐槽了,这么多,你觉得我记得住?你是不是高看我了?
 既然如此,咱们换个思路,直接把68个库想成你女神的兴趣爱好,然后你有12个女神,你就是一代海王,现在记得住了吗?

我把它分成了如下12个大类
- 数据相关
- 数据结构相关
- 作用域相关
- 迭代器生成器相关
- 字符串类型代码的执行
- 输入输出
- 内存相关
- 文件操作相关
- 模块相关
- 帮助
- 查看内置属性
- 调用相关
1、数据
数学运算
- abs() 返回绝对值
- divmode() 返回商和余数
- round() 四舍五入
- pow(a, b) 求a的b次幂, 如果有三个参数. 则求完次幂后对第三个数取余
- sum() 求和
- min() 求最小值
- max() 求最大值
代码
print(abs(-4))
print(divmod(20,3))
print(round(12.34))
print(pow(11,3,2))
print(sum([11,22,33,44,55,66,77,88]))
print(min(25,13,99,42,6,10))
print(max(25,13,99,42,6,10))
效果展示

数据类型
- bool : 布尔型(True,False)
- int : 整型(整数)
- float : 浮点型(小数)
- complex : 复数
进制转换
- bin() :将转换成二进制
- otc() :将转换成八进制
- hex() :将转换成十六进制
代码
print(oct(8))
print(bin(8))
print(hex(8))
效果展示

2、数据结构
序列
列表和元组
- list() 将一个可迭代对象转换成列表
- tuple() 将一个可迭代对象转换成元组
代码
print(list((11,12,13,14,15,16)))
print(tuple([11,12,13,14,15,16]))
效果展示

切片和翻转
- reversed() 将一个序列翻转, 返回翻转序列的迭代器
- slice() 列表切片
代码
lst = "人生苦短我用Python"
res = reversed(lst)
print(list(res))
效果展示

lst = [10, 11, 12, 13, 14, 15, 16]
print(lst[2:6:1])
spl = slice(2, 6, 1)
print(lst[spl])
效果展示  字符串
str() 将数据转化成字符串
代码
res = 'Python666'
print(format(res, "^20"))
print(format(res, "^30"))
print(format(res, ">30"))
print(format(res, "<30"))
效果展示

print(format(6, 'd' ))
print(format(111, 'c' ))
print(format(11, 'd' ))
print(format(11, 'o' ))
print(format(11, 'x' ))
print(format(11, 'X' ))
print(format(2.341567289, '0.4f' ))
print(format(2.341567289, 'f' ))
print(format(2341567289, 'e' ))
print(format(2341567289, '0.4e' ))
效果

bytes() :把字符串转化成bytes类型
ast = bytes("今天下雨呢!!", encoding="utf-8")
print(ast)
bytearray()
rst = bytearray("alex" ,encoding ='utf-8')
print(rst[1])
print(rst)
rst[1] = 108
print(str(rst))
ord(): 输入字符找带字符编码的位置 chr() :输入位置数字找出对应的字符 ascii() :是ascii码中的返回该值 不是就返回u
print(ord('s'))
print(ord('国'))
print(chr(99))
for i in range(65536):
print(chr(i), end=" ")
输出结果
把65535的字符都打印出來  repr() 返回一个对象的string形式
res = "巳月\n今天撩了%d个 妹子" % 1
print(res)
print(repr(res))
数据集合
字典:dict 创建字典 集合:set 创建集合 frozenset() 创建一个冻结的集合, 注: 冻结的集合不能进行添加和删除操作。
内置函数
len() 返回对象中的元素的个数
sorted() 对可迭代对象进行排序操作 (lamda) 语法:sorted(Iterable, key=函数(排序规则), reverse=False)
Iterable: 可迭代对象
key: 排序规则(排序函数), 在sorted内部会将可迭代对象中的每一个元素传递给这个函数的参数。根据函数运算的结果进行排序
reverse: 是否是倒叙. True: 倒叙, False: 正序
代码演示
lst = [25,7,16,92,11,63,889,100,1]
lst.sort()
print(lst)
lat = sorted(lst)
print(lat)
ldt = sorted(lst,reverse=True)
print(ldt)
效果
 enumerate() :获取集合的枚举对象
lst = ['one','two','three','four','five']
for index, el in enumerate(lst,1):
print(index)
print(el)
结果
 all(): 迭代对象全部为True,才返回True any() :迭代对象一个为True,就返回True
print(all([11,'python666',True]))
print(any([99,False,'Nice',97,True]))
输出结果

zip() :将可迭代的对象作为参数, 把对象中对应的元素打包成一个元组, 返回列表。 注 如果各个迭代器的元素个数不一致, 则返回列表长度与最短的对象相同。
lst1 = [1, 2, 3, 4, 5, 6]
lst2 = ['异型', '小兵张嘎', '急速快递', '人生不可重来', '这是一道伪命题', '白马王子的一生']
lst3 = ['MA', 'CN', 'FR', 'ITA', 'KR', 'SE']
print(zip(lst1, lst1, lst3))
for sa in zip(lst1, lst2, lst3):
print(sa)
输出结果
 fiter() :过滤 (lamda) 语法:fiter(function. Iterable)
function: 用来筛选的函数。 ①在?lter中会自动的把iterable中的元素传递给function; ②然后根据function返回的True或者False来判断是否保留留此项数据 , Iterable: 可迭代对象。
def func(i):
return i % 2 == 1
lst = [11,22,33,44,55,66,77]
la = filter(func,lst)
print(la)
输出结果
 map() :根据提供的函数对指定序列做映射(lamda) 语法 : map(function, iterable) 注: 可以对可迭代对象中的每一个元素进行映射。 分别去执行 function
def f(i):
return i
lst = [11,12,13,14,15,16,17]
st = map(f, lst)
print(list(st))
输出结果

3、作用域相关
locals() :返回当前作用域中的名字 globals() :返回全局作用域中的名字
def func():
a = 10
print(locals())
print(globals())
print("打印全部内容")
func()
输出结果

4、迭代器生成器相关
range() :生成数据 next() :迭代器向下执行一次, 内部实际使?用了__ next__()方法返回迭代器的下一个项目 iter() :获取迭代器, 内部实际使用的是__ iter__()方法来获取迭代器
for i in range(25,-1,-3):
print(i)
输出结果 
lst = [11,12,13,14,15]
st = iter(lst)
print(st.__next__())
print(next(st))
print(next(st))

5、字符串类型代码的执行
eval() :执行字符串类型的代码. 并返回最终结果 exec() :执行字符串类型的代码 compile(): 将字符串类型的代码编码。代码对象能够通过exec语句来执行或者eval()进行求值
st1 = input("请输入a+b:")
print(eval(st1))
st2 = "for i in range(9): print(i)"
ast = exec(st2)
print(ast)
输出结果 
exec("""
def func():
print(" python666")
""" )
func()
输出结果

code1 = "for i in range(9): print(i)"
cst = compile(code1, "", mode="exec")
exec(cst)
输出结果 
code2 = "1+2+3+4"
cst2 = compile(code2, "", mode="eval")
print(eval(cst2))

code3 = "sexuality = input('请输入你的性别:')"
cst3 = compile(code3, "", mode="single")
exec(cst3)
print(sexuality)
输出结果

6、输入输出
print() : 打印输出 input() : 获取用户输出的内容
print("Hello", "python666", sep=",", end="★")
输出结果

7、内存相关
hash() : 获取到对象的哈希值(int, str, bool, tuple)。 hash算法:
hash表:用空间换的时间 比较耗费内存。
8、文件操作相关
open() : 用于打开一个文件, 创建一个文件句柄。
f = open('filepath',mode='w',encoding='utf-8')
f.read()
f.close()
9、模块相关
__ import__() : 用于动态加载类和函数
import os
path = input("你要导入的模块:")
__import__(path)
10、调用相关
callable() : 用于检查一个对象是否是可调用的。
如果返回True, object有可能调用失败; 但如果返回False. 那调用绝对不会成功。
sat = 11
print(callable(sat))
输出结果 
11、帮助
help() : 函数用于查看函数或模块用途的详细说明
print(help())
print(help(str))
输出结果

12、查看内置属性
dir() : 查看对象的内置属性,访问的是对象中的__dir__()方法。
print(dir())
print(dir(list))
print(dir(tuple))
输出结果 
写在最后
今天的分享,差不多就结束了,Python的内置库函数,算是给大家整理的明明白白的了。
当然,咱们也可以参照官方文档来学习都是可以的。
记不住的小伙伴,可以先收藏,再学习,毕竟一次性记住这么多,确实有点难为人~
 最后给大家分享一些Python教程,希望对大家有所帮助:
Python零基础入门全套教程
Python进阶全套教程
Python实战100例
春蚕到死丝方尽,人至期颐亦不休。一息尚存须努力,留作青年好范畴。

|