以上实例输出结果为:
Python中字典的内建函数用法是什么?
点击上方 "Python人工智能技术" 关注,星标或者置顶
22点24分准时推送 , 第一时间送达
后台回复“大礼包”,送你特别福利
编辑:乐乐 | 来自:pypypypy
上一篇:
正文
大家好 , 我是Pythn人工智能技术 。
内置函数就是Python给你提供的 , 拿来直接用的函数 , 比如print. , input等 。
截止到python版本3.6.2,python一共提供了68个内置函数,具体如下
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
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()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()
delattr() hash() memoryview() set()
本文将这68个内置函数综合整理为12大类 , 正在学习Python基础的读者一定不要错过,建议收藏学习!
和数字相关 1. 数据类型
bool : 布尔型(True,False)
int : 整型(整数)
float : 浮点型(小数)
complex : 复数
2. 进制转换
bin() 将给的参数转换成二进制
otc() 将给的参数转换成八进制
hex() 将给的参数转换成十六进制
print(bin(10)) # 二进制:0b1010
print(hex(10)) # 十六进制:0xa
print(oct(10)) # 八进制:0o12
3. 数学运算
abs() 返回绝对值
divmode() 返回商和余数
round() 四舍五入
pow(a, b) 求a的b次幂, 如果有三个参数. 则求完次幂后对第三个数取余
sum() 求和
min() 求最小值
max() 求最大值
print(abs(-2)) # 绝对值:2
print(divmod(20,3)) # 求商和余数:(6,2)
print(round(4.50)) # 五舍六入:4
print(round(4.51)) #5
print(pow(10,2,3)) # 如果给了第三个参数. 表示最后取余:1
print(sum([1,2,3,4,5,6,7,8,9,10])) # 求和:55
print(min(5,3,9,12,7,2)) #求最小值:2
print(max(7,3,15,9,4,13)) #求最大值:15
和数据结构相关 1. 序列
(1)列表和元组
list() 将一个可迭代对象转换成列表
tuple() 将一个可迭代对象转换成元组
print(list((1,2,3,4,5,6))) #[1, 2, 3, 4, 5, 6]
print(tuple([1,2,3,4,5,6])) #(1, 2, 3, 4, 5, 6)
(2)相关内置函数
reversed() 将一个序列翻转, 返回翻转序列的迭代器
slice() 列表的切片
lst = "你好啊"
it = reversed(lst) # 不会改变原列表. 返回一个迭代器, 设计上的一个规则
print(list(it)) #['啊', '好', '你']
lst = [1, 2, 3, 4, 5, 6, 7]
print(lst[1:3:1]) #[2,3]
s = slice(1, 3, 1) # 切片用的
print(lst[s]) #[2,3]
(3)字符串
str() 将数据转化成字符串
print(str(123)+'456') #123456
format() 与具体数据相关, 用于计算各种小数, 精算等.
s = "hello world!"
print(format(s, "^20")) #剧中
print(format(s, "20")) #左对齐
print(format(s, "20")) #右对齐
# hello world!
# hello world!
# hello world!
print(format(3, 'b' )) # 二进制:11
print(format(97, 'c' )) # 转换成unicode字符:a
print(format(11, 'd' )) # ?进制:11
print(format(11, 'o' )) # 八进制:13
print(format(11, 'x' )) # 十六进制(?写字母):b
print(format(11, 'X' )) # 十六进制(大写字母):B
print(format(11, 'n' )) # 和d?样:11
print(format(11)) # 和d?样:11
推荐阅读
- 肯德基抖音直播营销策略,抖音肯德基食货局
- erp系统委外加工所有流程,委外加工流程图
- 自媒体如何新起账号呢,自媒体账号注册流程
- 虎牙娜美美食主播直播,虎牙娜美封号多长时间
- idb怎么恢复mysql mysql ibd 恢复数据
- 苹果ios12.4.1屏蔽更新更新,ios1212屏蔽更新
- 郑州o2o小程序定制开发,郑州小程序制作流程及费用
- 便秘会带来什么害处视频,便秘的真实视频
- vb.net字体高度 vb字体大小调整代码