首页 / PYTHON / Python内置函数
Python内置函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python内置函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2628字,纯文字阅读大概需要4分钟。
内容图文
![Python内置函数](/upload/InfoBanner/zyjiaocheng/665/e0a4a46d0fd24d20852119c5a64c58ee.jpg)
abs() 绝对值
1 print(abs(-5)) #都变成正数
all() 判断
1 #一个为假,就为假 # 0 空 None Flase 2 print(all([1,'a',0]))#False 3 # 列表中所有元素的布尔值为真,最终结果才为真 4 print(all('')) 5 # 传给all的可迭代对象如果为空,最终结果为真
any() 判断
1 #但凡有一个为真,就为真 2 # print(any([0,'',None,1])) 3 print(any([])) 4 # 传给any的可迭代对象如果为空,最终结果为假
进制转换
1 print(bin(11)) #10-2 2 print(oct(11)) #10-8 3 print(hex(11)) #10-16
bool() 布尔值
1 print(bool(0))#0, None,空的布尔值为假
.encode() 转类型
1 res = '你好songhaiixng'.encode('utf-8') 2 print(res) #转成了bytes类型 3 4 res = bytes('你好songhaixing',encoding='utf-8') 5 print(res) #同上面一样
callable() 调用
1 def func(): 2 pass 3 print(callable(func)) 4 print(callable('ss'.strip))#判断 5 print(callable('dac'.split))#带括号的都可以调用
ASCLL表转化
1 print(chr(90))#按照表十进制转化成字符 2 print(ord('x'))#。。。。。。转化成数字
dir() 查看调用对象
1 print(dir('song'))#查看某个对象可以调用的方式
divmod() 除 余
1 print(divmod(10,3))#(3,1) 10除于3余1 2 print(divmod(10,2))#(5,0)
eval() 字符串内表达式 运算
1 #将字符内的表达式拿出来运行一下,并拿到改表达式的计算结果 2 res = eval('2**3') #(去掉字符串) 3 print(res) 4 res = eval('[1,2,3,4,]') 5 print(res)
1 with open('db.txt','r',encoding='utf-8')as f: 2 f = f.read() #{'name':'song','pwd':'123'} 3 res = eval(f) #把这个字典拿出来了 4 print(res,type(res)) 5 print(res['pwd'])
frozenset() 不可变集合 冻结集合
1 fset = frozenset({1,2,3}) 2 3 #可变 4 s = {1,2,3,4} 5 s.add(5) 6 print(s) #{1, 2, 3, 4, 5} 可变
globals() 全局作用域中的绑定关系
1 x = 1111111111111 2 print(globals())#查看全局作用域中名字于值得绑定关系 3 print(locals())
字典的key必须是不可变类型
1 dic = {[1,2,3]:'a'}
不可hash的类型list,dict,set, == 可变的类型
可hash得类型int,float,str,tuple, == 不可变的类型
1 hash([1,2,3,4,5,6,])#不可hash
help() 显示帮助信息
1 def func(): 2 """ 3 帮助信息 4 :return: 5 """ 6 pass 7 print(help(func)) #显示里面的帮助信息 8 print(help(max))
len() 长度
1 len({'x':1,'y':2})#调用 __len__ 2 print({'x':1,'y':2}.__len__)
iter() 迭代器
1 res = iter('song')#'song'.__iter__ 2 print(next(res))
pwo() 平方取余
1 print(pow(2,3,3))#2的3次方取余
slice() 切片
1 res = slice(1, 5, 2)#切片 2 3 l = [1, 2, 3, 4, 5, 6, 7, 8] 4 print(l[res]) #[2, 4] 5 6 t = (5, 14, 7, 8, 9, 5, 4, 2) 7 print(t[res]) #(14, 8)
sum() 求和
1 print(sum([1,2,3,4,5])) #15
vars()
zip() (拉链函数) 左右对应
1 left = 'hello' 2 # right = (1,2,3,) #[('h', 1), ('e', 2), ('l', 3)] 3 right = {'x':1,'e':2} #[('h', 'x'), ('e', 'e')] 4 res = zip(left,right) 5 print(list(res))
内容总结
以上是互联网集市为您收集整理的Python内置函数全部内容,希望文章能够帮你解决Python内置函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。