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

    内置函数

    一、内置函数

    更多内置函数:<https://docs.python.org/3/library/functions.html?highlight=built#ascii" >

    1.1 掌握

    • bytes
    • chr/ord
    • divmod
    • enumerate
    • eval
    • hash

    1.bytes()
    解码字符。

    res = '你好'.encode('utf8')
    print(res)
    
    b'xe4xbdxa0xe5xa5xbd'
    
    res = bytes('你好', encoding='utf8')
    print(res)
    
    b'xe4xbdxa0xe5xa5xbd'
    

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

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

    3.divmod()
    分栏。

    print(divmod(10, 3))
    
    (3, 1)
    

    4.enumerate()
    带有索引的迭代。

    l = ['a', 'b', 'c']
    for i in enumerate(l):
        print(i)
    
    (0, 'a')
    (1, 'b')
    (2, 'c')
    

    5.eval()
    把字符串翻译成数据类型。

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

    6.hash()
    是否可哈希。

    print(hash(1))
    
    1
    

    1.2 了解

    • abs
    • all
    • any
    • bin/oct/hex
    • dir
    • frozenset
    • gloabals/locals
    • pow
    • round
    • slice
    • sum
    • __import__

    1.abs()
    求绝对值。

    print(abs(-13))  # 求绝对值
    
    13
    

    2.all()
    可迭代对象内元素全为真,则返回真。

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

    3.any()
    可迭代对象中有一元素为真,则为真。

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

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

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

    5.dir()
    列举出所有time的功能。

    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']
    

    6.frozenset()
    不可变集合。

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

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

    # print(globals())
    def func():
        a = 1
    #     print(globals())
        print(locals())
    
    
    func()
    
    {'a': 1}
    

    8.pow()

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

    9.round()

    print(round(3.5))
    
    4
    

    10.slice()

    lis = ['a', 'b', 'c']
    s = slice(1, 4, 1)
    print(lis[s])  # print(lis[1:4:1])
    
    ['b', 'c']
    

    11.sum()

    print(sum(range(100)))
    
    4950
    

    12.__import__()
    通过字符串导入模块。

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

    1.3 面向对象知识点

    • classmethod
    • staticmethod
    • property
    • delattr
    • hasattr
    • getattr
    • setattr
    • isinstance()
    • issubclass()
    • object()
    • super()
  • 相关阅读:
    python的多线程
    python的socket解析
    python的os.system函数的应用
    自动化测试的4种模型
    测试中的一些常见名词解析
    mysql存储过程详解
    mysql时间加减函数
    十周课程总结
    实验&报告7
    实验& 报告7
  • 原文地址:https://www.cnblogs.com/Dr-wei/p/11871625.html
Copyright © 2011-2022 走看看