zoukankan      html  css  js  c++  java
  • python中的内置函数

    abs(number):取绝对值
    1 re=abs(-342)
    2 print(re)
    3 #输出:342
    all(iterable):判断可迭代的对象的元素是否都是真,如果是返回True  否则返回false  ;  0,none,空 都是假
    li=[12,43,23,]
    re=all(li)
    print(re)
    #输出:True
    
    li=[12,43,23,0]
    re=all(li)
    print(re)
    #输出:False

    bin(number):将number转换成二进制的数并返回
    re=bin(256)
    print(re)
    #输出:0b100000000
    #0b表示是二进制数

    oct(number):返回一个八进制表示的字符串

    n=oct(9)
    print(n,type(n))
    #输出:0o11 <class 'str'>

    chr(i):根据i返回相应的ASCII码对应的字符

    re=chr(97)
    print(re)
    #输出:a

    ord(c):根据字符c相应的integer

    n=ord("b")
    print(n)
    #输出:98

    dir(p_object=None):不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息

    divmod(x, y): 返回元组((x-x%y)/y, x%y)

    re=divmod(100,3)
    print(re)
    #输出:(33, 1)

    eval(source, globals=None, locals=None): 将字符串str当成有效的表达式来求值并返回计算结果。globals可选。必须是dictionary    

    s='[11,22,33,44]'
    print(s,type(s))
    li=eval(s)
    print(li,type(li))
    #输出:[11,22,33,44] <class 'str'>
          [11, 22, 33, 44] <class 'list'>

    max(*args, key=None):获取一个可迭代对象中的最大值

    li=[11,22,3,4,55]
    print(max(li))
    #输出:55

    min(*args, key=None):获取一个可迭代对象的最小值

    li=[11,22,3,4,55]
    print(min(li))
    #输出:3

    pow(x, y, z=None) : 有2个参数 返回 x**y  ;  有3个参数  返回(x**y) % z

    n=pow(2,4)
    print(n)
    #输出:16
    
    
    n=pow(2,4,3)
    print(n)
    #输出:1

    round(number, ndigits=None):  对number四舍五入  ndigits是保留小数点后几位

    n=round(200.4352,2)
    print(n)
    #输出:200.44

    sorted(iterable, key=None, reverse=False):  对可迭代对象的元素进行排序 并返回一个列表

    li=[2,3,1,72,23]
    re=sorted(li)
    print(re)
    #输出:[1, 2, 3, 23, 72]

    sum(iterable, start=None):  求和
    li=[1,2,3,4]
    n=sum(li)
    print(n)
    #输出:10
    
    
    


    
    
  • 相关阅读:
    Codeforces Round #325 (Div. 2) F:(meet in the middle)
    Educational Codeforces Round 3:E (MST+树链剖分+RMQ)
    Educational Codeforces Round 3:D. Gadgets for dollars and pounds(二分答案+贪心)
    CodeForce 484B:(最大余数)
    CodeForce 540C:(DFS)
    HDU 1010:(DFS)
    Poj1741-Tree(树分治)
    uva10245-The Closest Pair Problem(平面上的点分治)
    hdu1561-The more, The Better(树形dp)
    hdu2196-Computer(树形dp)
  • 原文地址:https://www.cnblogs.com/wangbinbin/p/7092378.html
Copyright © 2011-2022 走看看