本节主要内容:
内置函数:
内置函数就是python给你提供的.拿来直接用的函数,比如print,input等等.截止到python版本3.6.2 python一共提供了68个内置函数.他们就是python
直接提供给我们的.有一些我们已经用过了.有一些还没有用过.还有一些还没有用过.还有一些需要学完了面向对象才能继续学习的.今天我们就认识
一下python的内置函数.
https://www.processon.com/mindmap/5bdbfc4be4b0cc9e7ea65a49中的思维导图是关于内置函数的分类和使用方法的介绍
下面详细介绍一些之前没有用到过的内置函数:
一.字符串类型代码的执行:
eval() 执行字符串类型的代码.并返回最终结果
print(eval("2+2")) #4 n = 8 print(eval("2+n")) #10 def func(): print(666) eval("func()") #666
exec() 执行字符串类型的代码
exec(""" for i in range(10): print(i) """) exec(""" def func(): print("我是周杰伦") func() """)
compile()将字符串类型的代码编译,代码对象能够通过exec语句来执行或者eval()进行求值
""" 参数说明: 1.resource 要执行的代码,动态代码片段 2.文件名,代码存放的文件名,当传入了第一个参数的时候,这个参数给空就可以了 3.模式,取值有3个, 1.exce: 一般放一些流程语句的时候 2.eval:resource只存放一个求值表达式 3.single:resource存放的代码有交互的时候,mode应为single. """ code1 = "for i in range(10) : print(i)" c1 = compile(code1,"",mode="exec") exec(c1) code2 = "1+2+3" c2 = compile(code2,"",mode="eval") a = eval(c2) print(a) code3 = "name = input('请输入你的名字')" c3 = compile(code3,"",mode="single") exec(c3) print(name)
有返回值的字符串形式的代码用eval().没有返回值的字符串形式的代码用exec().一般很少用到compile()
二.slice() 列表的切片
st = "大家好,我是麻花藤" s = slice(1,5,2) print(st[s])
三.字符串相关:
format() 与具体数据相关,用于计算各种小数,精算等
字符串 print(format('test', '<20')) # 左对⻬ cener(20) print(format('test', '>20')) # 右对⻬ print(format('test', '^20')) # 居中 # 数值 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 # 浮点数 print(format(123456789, 'e')) # 科学计数法. 默认保留6位⼩数 print(format(123456789, '0.2e')) # 科学计数法. 保留2位⼩数(⼩写) print(format(123456789, '0.2E')) # 科学计数法. 保留2位⼩数(⼤写) print(format(1.23456789, 'f')) # ⼩数点计数法. 保留6位⼩数 print(format(1.23456789, '0.2f')) # ⼩数点计数法. 保留2位⼩数 print(format(1.23456789, '0.10f')) # ⼩数点计数法. 保留10位⼩数 print(format(1.23456789e+10000, 'F')) # ⼩数点计数法. INF 无穷
bytes()把字符串转化成bytes类型
s = "你好" bs = s.encode("utf-8") print(bs) s1 = bs.decode("utf-8") print(s1) bs = bytes(s,encoding="utf-8") #把字符串编码成utf-8 print(bs)
bytearray() 返回一个新字节数组,这个数组里的元素是可变的,并且每个元素的值的范围是[0,256)
ret = bytearray("alex",encoding="utf-8") print(ret[0]) print(ret)
memoryview() 查看bytes在内存中的情况
s = memoryview("麻花藤".encode("utf-8")) # 查看bytes字节在内存中的情况 print(s)
repr() 返回一个对象的官方表示形式
# repr 输出一个字符串的官方表示形式
print(repr("大家好, 我叫周杰伦"))
print("大家好我叫周杰伦")
# 结果 :
# '大家好, 我叫周杰伦'
# 大家好我叫周杰伦
# %r %r用的就是repr
name = "taibai"
print("我叫%r" %name)
# 结果:我叫'taibai'
enumerate() 获取集合的枚举对象
lst = ["alex","wusir","taibai"] for index,el in enumerate(lst): print(str(index)+"==>"+el) # 结果: # 0==>alex # 1==>wusir # 2==>taibai
all() 可迭代对象中全部是True, 结果才是True
any() 可迭代对象中有一个是True, 结果就是True
print(all([1,2,True,0])) #False print(any([1,"",0]))#True
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表.如果各个
迭代器的元素个数不一致,则返回列表长度与最短的对象相同.
l1 = [1,2,3] l2 = ["a","b","c",5] l3 = ("*","**",(1,2,3)) for i in zip(l1,l2,l3): print(i) # 结果: # (1, 'a', '*') # (2, 'b', '**') # (3, 'c', (1, 2, 3))
.