zoukankan      html  css  js  c++  java
  • 内置方法

    内置方法

    所有内置方法:https://docs.python.org/3/library/functions.html?highlight=built#ascii

    掌握

    1、bytes()

    解码字符

    str = '斯蒂芬和'
    res = str.encode('utf8')
    print(res)
    res1 = bytes(str,encoding='utf8')
    print(res1)
    '''
    b'xe6x96xafxe8x92x82xe8x8axacxe5x92x8c'
    b'xe6x96xafxe8x92x82xe8x8axacxe5x92x8c'
    '''
    

    2、chr()/old()

    chr()参考ASCII码表将数字转成对应字符;ord()将字符转换成对应的数字。

    print(chr(65))  # A
    
    print(ord('A'))  # 65
    

    3、divmod()

    python divmod() 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。

    print(divmod(7,2)) # (3,1)
    
    print(divmod(8,2)) # (4,0)
    
    divmod(1+2j,1+0.5j)  ((1+0j), 1.5j)
    

    4、enumerate()

    enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

    lis = ['nick','tank','dsb']
    for k,v in enumerate(lis,start=1):
        print(k,v)
    '''
    1 nick
    2 tank
    3 dsb
    '''
    

    5、eval()

    把字符串翻译成数据类型

    lis = '[1,2,3]'
    lis_eval(lis)
    print(lis_eval)
    # [1,2,3]
    

    6、hash()

    是否可哈希

    print(hash(1))  # 1
    

    了解

    abs:求绝对值

    print(abs(-5)) # 5
    

    all : 可迭代对象内元素全为真,则返回真

    any : 可迭代对象中有一个元素为真,则为真

    print(all([1, 2, 3, 0])) # False
    print(all([])) # True
     
    print(any([1, 2, 3, 0])) # True
    print(any([])) # False
    

    bin()/oct()/hex():二进制、八进制、十六进制转换。

    print(bin(17))   # 0b10001
    print(oct(17))   # 0o21
    print(hex(17))   # 0x11
    
    

    dir:列出该模块的所有功能

    import time
    print(dir(time))
    
    ['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'perf_counter', 'process_time', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname', 'tzset']
    
    

    frozenset:不可变集合,定义了便无法改变

    s = frozenset({1, 2, 3})
    print(s)  # frozenset({1, 2, 3})
    
    

    globals()/loacals():查看全局名字;查看局部名字。

    pow : 幂函数

    print(pow(3, 2, 3))  # (3**2)%3
    # 0
    
    

    round :四舍五入

    print(round(3.5))
    # 4
    
    

    sum : 求和

    print(sum(range(100))) # 4950
    
    

    _import_ : 通过字符串导入模块。

    m = __import__('time')
    print(m.time())
    # 1556607502.334777
    
    

    面向对象会用到的内置方法(先列举出来)

    1. classmethod
    2. staticmethod
    3. property
    4. delattr
    5. hasattr
    6. getattr
    7. setattr
    8. isinstance()
    9. issubclass()
    10. object()
    11. super()
  • 相关阅读:
    组合数取模的题……
    对组合数取模
    n!(n的阶乘)
    八、元素绑定
    七、Application类
    RC振荡电路
    运算放大器工作原理
    No
    合并查询结果
    连接查询
  • 原文地址:https://www.cnblogs.com/dadazunzhe/p/11352651.html
Copyright © 2011-2022 走看看