前言,python

今天老师讲到一个对称处理时, 我发现老师用range(-3, 4) 颠覆了我对range函数使用负数的思想. 但是我觉得老师的代码可以优化, 因为之前看官方文档时有个函数可以求绝对值, 我只有印象, 却不记得了.

if i < 0:
pre_space = -i
else:
pre_space = i

可是使用一个函数之后, 只用使用一行代码即可.

pre_space = abs(i)

内置函数
Python解释器内置了许多始终可用的函数和类型。它们按字母顺序列在此处。

内置功能
abs() delattr() hash() memoryview() set()
all() dict() help() min() setattr()
any() dir() hex() next() slice()
ascii() divmod() id() object() sorted()
bin() enumerate() input() oct() staticmethod()
bool() eval() int() open() str()
breakpoint() 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()

转载于:https://blog.51cto.com/sonlich/2156875

你可能感兴趣的:(前言,python)