内置函数

  我们一起来看看python里的内置函数。什么是内置函数?就是Python给你提供的,拿来直接用的函数,比如print,input等等。截止到python版本3.6.2,现在python一共为我们提供了68个内置函数。它们就是python提供给你直接可以拿来使用的所有函数。这些函数有些我们已经用过了,有些我们还没用到过,还有一些是被封印了,必须等我们学了新知识才能解开封印的。那今天我们就一起来认识一下python的内置函数。这么多函数,我们该从何学起呢?

 

     内置函数    
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()  filter()  issubclass()  pow()  super() 
bytes() float()  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()  

1.1作用域相关

locals :函数会以字典的类型返回当前位置的全部局部变量。

globals:函数以字典的类型返回全部全局变量。

a = 1
b = 2
print(locals())
print(globals())
# 这两个一样,因为是在全局执行的。

##########################

def func(argv):
    c = 2
    print(locals())
    print(globals())
func(3)

#这两个不一样,locals() {'argv': 3, 'c': 2}
#globals() {'__doc__': None, '__builtins__': <module 'builtins' (built-in)>, '__cached__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000024409148978>, '__spec__': None, '__file__': 'D:/lnh.python/.../内置函数.py', 'func': <function func at 0x0000024408CF90D0>, '__name__': '__main__', '__package__': None}
代码示例

1.2其他相关

1.2.1 字符串类型代码的执行 eval,exec,complie

  eval:执行字符串类型的代码,并返回最终结果。

eval('2 + 2')  # 4


n=81
eval("n + 4")  # 85


eval('print(666)')  # 666
View Code

  exec:执行字符串类型的代码。

s = '''
for i in [1,2,3]:
    print(i)
'''
exec(s)
View Code

相关文章:

  • 2021-12-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-13
  • 2021-08-16
  • 2021-05-23
  • 2022-03-07
相关资源
相似解决方案