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

    内置函数

    一、内置函数

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

    55内置函数-内置函数.jpg?x-oss-process=style/watermark

    1.1 掌握

    1. bytes
    2. chr/ord
    3. divmod
    4. enumerate
    5. eval
    6. 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 了解

    1. abs
    2. all
    3. any
    4. bin/oct/hex
    5. dir
    6. frozenset
    7. gloabals/locals
    8. pow
    9. round
    10. slice
    11. sum
    12. 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 面向对象知识点

    1. classmethod
    2. staticmethod
    3. property
    4. delattr
    5. hasattr
    6. getattr
    7. setattr
    8. isinstance()
    9. issubclass()
    10. object()
    11. super()
    我把月亮戳到天上 天就是我的 我把脚踩入地里 地就是我的 我亲吻你 你就是我的
  • 相关阅读:
    Rust语言学习笔记(11)
    Rust语言学习笔记(10)
    Rust语言学习笔记(9)
    Rust语言学习笔记(8)
    趣味编程:静夜思(Rust版)
    Python sorted()
    Python 魔法方法
    Python filter()
    Python的map和reduce
    Python函数的参数
  • 原文地址:https://www.cnblogs.com/zhulipeng-1998/p/12863729.html
Copyright © 2011-2022 走看看