python built-in functions


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()

abs(x)

返回一个数的绝对值。参数可以是一个整型或者浮点型。如果参数是一个复数的话,则返回magnitue

magnitude的计算规则如下: 实数的magnitude就是该实数的正平方根,如2的magnitude就是2,-3的magnitude就是3 复数的magnitude是该复数与共轭复数的乘积的正平方根,比如z=3-2j,则magnitude为(3-2j)*(3+2j)的正平方根,也就是9+4=13的正平方根;
示例:

In [1]: abs(7)
Out[1]: 7

In [2]: abs(-7.7)
Out[2]: 7.7

In [3]: abs(complex(7,7))
Out[3]: 9.899494936611665

all(iterable)

参数为可迭代对象,如果对象的所有元素为真或者对象为空,则返回True,等价于:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

示例:

In [6]: all([1,2,3])
Out[6]: True

In [7]: all(())
Out[7]: True

In [10]: all([1,2,3,None])
Out[10]: False

any(iterable)

参数为可迭代对象,如果对象的任一元素为真,则返回True,如果对象为空,则返回False,等价于:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

示例:

In [11]: any([True,False])
Out[11]: True

In [12]: any({})
Out[12]: False

ascii

bin

bool

bytearray

bytes

callable

chr

classmethod

compile

complex

delattr

dict

dir

divmod

enumerate

eval

exec

filter

float

format

frozenset

getattr

globals

hasattr

hash

help

hex

id(object)

返回一个对象的‘标识’,在一个对象的生命周期中,保持唯一不变。两个不处在同一生命周期的对象调用id()也许会返回一样的值

CPython实现的细节:返回值是对象在内存中的地址

>>> s1 = 'sss'
>>> s2 = 'sss'
>>> s3 = 's*s'
>>> s4 = 's*s'
>>> id(s1)
4351970960
>>> id(s2)
4351970960
>>> id(s3)
4351970640
>>> id(s4)
4351970680

input

int

isinstance

issubclass

iter

len

list

locals

max

abs

memoryview

min

next

object

oct

ord

pow

property

range

repr

reversed

round

set

setattr

slice

sorted

staticmethod

str

sum

super

tuple

type

vars

zip

__import__


文章作者: keepwonder
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 keepwonder !
  目录