zoukankan      html  css  js  c++  java
  • 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),compl
    ex('2+3j')返回(2+3j)
    pow()# 内置函数,乘方。如果有第三个参数,则表示乘方的结果对第三个参数取余,如w(2,3)
    返回8,pow(2,3,4)返回0
    round(1)//1.0#浮点数  

    功能判断

    callable("变量") #函数是否可调用。注意:变量要定义过
    isinstance(x,[11,22,33])#判断X是不是有列表或者整型等,如果是,返回True,不是返回False
    cmp('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 random
    temp = ""
    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 += c
    print(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()返回某数据类型等等

    附:常用重要函数

    abs(),all(),any(),bin(),bool(),bytes(),chr(),dict()dir(),divmod(),enumerate(),eval(),filter(),float(),gloabls(),help(),hex(),id(),input(),int(),isinstance(),len(),list(),locals(),map(),max(),min(),oct(),open(),ord(),pow(),print(),range(),round(),set(),type(),sorted(),str(),sum(),tuple()

     

      

      

      

  • 相关阅读:
    The Balance POJ 2142 扩展欧几里得
    扩展欧几里得定理总结
    Crashing Robots POJ 2632 简单模拟
    POJ 1328 Radar Installation 贪心算法
    The Pilots Brothers' refrigerator DFS+枚举
    HDU RSA 扩展欧几里得
    HDU A/B 扩展欧几里得
    ACM 数学
    E
    BZOJ 3223: Tyvj 1729 文艺平衡树
  • 原文地址:https://www.cnblogs.com/flash55/p/5839012.html
Copyright © 2011-2022 走看看