1. 内置函数

什么是内置函数? 就是python给你提供的,拿来直接⽤的函数,比如print,input等等,截⽌到python版本3.6.2 python⼀共提供了68个内置函数。他们就是python直接提供给我们的,有
⼀些我们已经⽤过了,有⼀些还没有⽤过,还有⼀些需要学完了⾯向对象才能继续学习的,今天我们就认识⼀下python的内置函数。
python基础(15):内置函数(一)-LMLPHP

作⽤域相关:
迭代器相关:
字符串类型代码的执⾏:
print(eval("2+2")) #
n = 8
print(eval("2+n")) #
def func():
  print(666)
eval("func()") #
exec("""
for i in range(10):
   print(i)
""")
exec("""
def func():
   print("我是周杰伦")
func()
""")
'''
参数说明:
1. resource 要执⾏的代码, 动态代码⽚段
2. ⽂件名, 代码存放的⽂件名, 当传⼊了第⼀个参数的时候, 这个参数给空就可以了
3. 模式, 取值有3个,
1. exec: ⼀般放⼀些流程语句的时候
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()。
输入和输出相关:
内存相关: 
⽂件操作相关: 
模块相关:
帮助: 
调⽤相关: 
查看内置属性: 
基础数据类型相关:  
  数字相关: 
  进制转换: 
  数学运算: 
和数据结构相关: 
  列表和元组: 
st = "⼤家好, 我是麻花藤"
s = slice(1, 5, 2)
print(st[s])
  字符串相关: 
str():将数据转化成字符串
format():与具体数据相关, ⽤于计算各种⼩数, 精算等
# 字符串
print(format('test', '<20')) # 左对⻬
print(format('test', '>20')) # 右对⻬
print(format('test', '^20')) # 居中
# 数值
print(format(3, 'b')) # ⼆进制
print(format(97, 'c')) # 转换成unicode字符
print(format(11, 'd')) # ⼗进制
print(format(11, 'o')) # ⼋进制
print(format(11, 'x')) # ⼗六进制(⼩写字⺟)
print(format(11, 'X')) # ⼗六进制(⼤写字⺟)
print(format(11, 'n')) # 和d⼀样
print(format(11)) # 和d⼀样
# 浮点数
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')) # ⼩数点计数法.
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)
ret = bytearray('alex',encoding='utf-8')
print(ret[0])
print(ret)
# 查看bytes字节在内存中的情况
s = memoryview("麻花藤".encode("utf-8"))
print(s)
# 找到对应字符的编码位置
print(ord('a'))
print(ord('中'))
# 找到对应编码位置的字符
print(chr(97))
print(chr(20013))
# 在ascii中就返回这个值. 如果不在就返回\u...
print(ascii('a'))
print(ascii('好'))
# repr 就是原封不动的输出, 引号和转义字符都不起作⽤
print(repr('⼤家好,\n \t我叫周杰伦'))
print('⼤家好我叫周杰伦')

# %r 原封不动的写出来
name = 'taibai'
print('我叫%r' % name)
  数据集合:
  其他相关: 
lst = ["alex", "wusir", "taibai"]
for index, el in enumerate(lst):
  print(str(index)+"==>"+el)
print(all([1,2,True,0]))
print(any([1,'',0]))
l1 = [1,2,3,]
l2 = ['a','b','c',5]
l3 = ('*','**',(1,2,3))
for i in zip(l1,l2,l3):
  print(i)
04-19 23:54
查看更多