python----内置函数

一、内置函数

  内置函数是python解释器在运行之初就加载到当前工程中的函数,我们可以直接调用,前面我们用到的len()、range、min()、max()、input()、等都是内置函数。下面介绍我们之前没用到或者不常见的内置函数。以下是python中的内置函数。

  ['abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

1.1作用域相关内置函数(globals()与locals())

  globals():获取的是全部全局变量的值并以字典的形式返回

  locals():获取当前作用域的全部局部变量的值并以字典的形式返回

def fun1():
    a = 1
    print(locals())  #{'a':1}
    print(globals())  
fun1()

 1.2字符串相关

  eval:执行字符串内的代码并返回结果,通常用来计算字符串计算代码:

s1 = '1+2+3'
print(eval(s1),type(eval(s1)))  #6 <class 'int'>

  exec:执行字符串代码,无返回值:

s1 = '''
for i in range(10):
print(i)
'''
print(exec(s1))

猜你喜欢

转载自www.cnblogs.com/Kingfan1993/p/9510121.html
今日推荐