首页 > 代码库 > python之常用内置函数

python之常用内置函数

python内置函数,可以通过python的帮助文档 Build-in Functions,在终端交互下可以通过命令查看

>>> dir("__builtins__")[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘,_eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewarg,__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__epr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subcasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘, ‘count‘, ‘encode‘, ‘endswith‘,expandtabs‘, ‘find‘, ‘format‘, ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isecimal‘, ‘isdigit‘, ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘issace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘, ‘prtition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstri‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translat‘, ‘upper‘, ‘zfill‘]

python内置函数可以分为大致几类

数字运算

abs(-1) #取绝对值或者复数的模max([1,2,3])、min([1,2,3]) #最大最小值len(‘abc‘)、len([1,2,3])、len((1,2,3))#序列长度divmod(5,2)//(2,1)#内置函数,把...转换成复数,如complex(‘2‘)返回(2+0j),complex(‘2+3j‘)返回(2+3j)pow()# 内置函数,乘方。如果有第三个参数,则表示乘方的结果对第三个参数取余,如w(2,3)返回8,pow(2,3,4)返回0round(1)//1.0#浮点数  

功能判断

callable("变量") #函数是否可调用。注意:变量要定义过isinstance(x,[11,22,33])#判断X是不是有列表或者整型等,如果是,返回True,不是返回Falsecmp(‘hello‘,‘hello‘)#比较(x)range([start,] stop[, step])# 快速生成序列

类型转换

long(x)float(x) #把。。转换成浮点数complex(x) //复数str(x)#转换成字符串list(x)#转换成字符串tuple(x) //元组进制件的相互转换 r = hex(10)#十六进制 r = oct(10)#八进制 r= bin(10)#二进制 r= int(10)#十进制 i= int("11",base=10)#进制间的相互转换base后跟 2/8/10/16 print(i)chr(x)//返回x对应的字符,如chr(65)返回‘A‘ord(x)//返回字符对应的ASC码数字编号,如ord(‘A‘)返回65#最常用的例子就是生随机验证码import randomtemp = ""for i in range(4):    num = random.randrange(0,4)    if num == 3 or num == 1:        li = random.randrange(0,10)        temp = temp + str(li)    else:        rad = random.randrange(65,91)        c = chr(rad)        temp += cprint(temp)

序列处理

len():序列长度max():序列中最大值min():最小值reduce():归并filter():过滤序列,具体用法如下def f1(x):    # if x > 22:    #     return True    # else:    #     return False    return x >22# ret = filter(f1,[11,22,33,44])ret = filter(lambda x: x > 22, [11,22,33,44])for i in ret:    print(i)map():并行遍历,可接受一个function类型的参数,同filter()函数zip():并行遍历,用法具体如下f1 = ["a","b","c"]f2 = [11,22,33]set = zip(f1,f2)for i in set:    print(i)

type()返回某数据类型等等

 

 

  

  

  

python之常用内置函数