• python_69_内置函数1


    #abs()取绝对值
    '''
    all(iterable)
    Return True if all elements of the iterable are true (or if the iterable is empty).
    '''
    print(all([0,9,-8,'a']))
    print(all([9,-8,'a']))
    '''
    any(iterable)
    Return True if any element of the iterable is true. If the iterable is empty, return False.
    '''
    print(any([0,9,-8,'a']))
    print(any([]))
    
    # ascii(object)
    # As repr(), return a string containing a printable representation of an object,
    # but escape the non-ASCII characters in the string returned by repr() using x, u or U escapes.
    a=ascii(['a',1,'汉子'])
    print(a,[a])
    print(type(a))#格式是字符串
    '''
    bin(x)
    Convert an integer number to a binary string prefixed with “0b”. 
    '''
    print(bin(3))
    #布尔bool
    print(bool(0))
    print(bool(1))
    print(bool([]))
    #byte
    a=bytes('abc',encoding='utf-8')
    print(a.capitalize(),a)#字符串不可以修改,二进制的字节格式也不可以修改,要想修改只能生成新的
    #bytearray字节的数组可以被修改
    b=bytearray('abc',encoding='utf-8')
    print(b[0],b[2])
    #b[0]='B'#错误。必须以字节形式修改
    b[0]=65
    b[1]=66
    b[2]=67
    print(b)
    '''
    callable(object)判断是否可以被调用
    '''
    print(callable([]))
    def diaoyong():pass
    print(callable(diaoyong))#函数和类都可调用
    # chr(i),i必须是数字,将数字转为ascll码字符
    print(chr(67))
    #ord与chr相反
    print(ord('C'))
    #compile()
    code1='for i in range(10):print(i)'
    exec(code1)#以下两行程序同等此行
    # c=compile(code1,'','exec')#exec将字符串编码可执行的程序
    # exec(c)
    #print(c),c是内存中的数据对象
    code2='1+3/2*6'
    print(eval(code2)) #以下两行程序同等此行
    '''
    c=compile(code2,'','eval')
    eval(c)#,适合字符串变字典,以及加减乘除类,不适合语句类,比如for循环,这样的用exec
    '''
    code3='''
    import time
    def consumer(name):
        print("%s 准备吃包子啦!" %name)
        while True:
           baozi = yield
           print("包子[%s]来了,被[%s]吃了!" %(baozi,name))
    c=consumer('猪小芳')
    c.__next__()
    b1='韭菜馅'
    '''
    exec(code3)#以下两行程序同等此行
    # py_obj=compile(code3,'err.log','exec')#编译过程中出的错会写到err.log中,写不写无所谓,不好使
    # exec(py_obj)
    #complex复数
    print(complex('1+2j')+complex('2+2j'))
    #dict字典
    print(dict())#生成字典
    #dir查看使用方法
    a={}
    b=()
    print(dir(a))
    print(dir(b))
    #divmod(a,b),return商和余数
    print(divmod(6,4))
    #匿名函数,用完就释放
    (lambda n:print(n*n))(5)#一种传递参数的方法
    calc=lambda n:print(n*n)
    calc(10)
    # calc2=lambda x:for i in range(x):print(i)#处理不了复杂的,可以处理三元运算这种简单的
    calc3=lambda n:3 if n<4 else n
    print(calc3(5))
    #lambda可与filter过滤器结合使用
    res=filter(lambda n:n>5,range(10))
    print(res)#迭代器
    for i in res:
        print(i,end='	')
    print('>>>>>>>>>分隔符1')
    #lambda可与map结合使用
    res=map(lambda n:n*2,range(10))#等价于[i*2 for i in range(10)],[lambda n:n*2 for i in range(10)]
    print(res)#迭代器
    for i in res:
        print(i,end='	')
    
    print('>>>>>>>>>分隔符1')
    import functools
    print(functools.reduce(lambda x,y:x*y,range(1,10)))#阶乘
    print(functools.reduce(lambda x,y:x+y,range(10)))#累加和
    #不可变集合,就和元组似的
    a=frozenset([1,4,333,212,33,33,12,4])
    print(a)
    #返回本文件程序的所有全局变量及值
    print(globals())
    '''
    hash(object)哈希:每个数据对应一个数字映射,便于查找
    Return the hash value of the object (if it has one).Hash values are integers.
    They are used to quickly compare dictionary keys during a dictionary lookup. 
    Numeric values that compare equal have the same hash value 
    (even if they are of different types, as is the case for 1 and 1.0).
    '''
    print(hash('熊羚羽'))
    print(hash('韩江桦'))
    print(hash('俞莎莎'))
    print(hash('熊羚羽'))#与第一个对应的映射是相同的
    
  • 相关阅读:
    python 枚举enum
    python lambda 三元表达式
    python修改类属性
    python获取引用对象的个数
    python 返回实例对象个数
    python __del__() 清空对象
    python面向对象:继承
    python面向对象:多态
    Docker容器和K8s添加Health Check
    mkfs.xfs: /dev/vdb appears to contain a partition table (dos)
  • 原文地址:https://www.cnblogs.com/tianqizhi/p/8402069.html
Copyright © 2020-2023  润新知