内置函数

内置函数

一、内置函数

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

一、内置函数

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

Built-in Functions
abs() delattr() hash() memoryview() set()
all() dict() help() min() setattr()
any() dir() hex() next() slice()
ascii() divmod() id() object() sorted()
bin() enumerate() input() oct() staticmethod()
bool() eval() int() open() str()
breakpoint() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()

二、掌握函数

  1. 解码字符

    # 解码字符
    
    res = "你好!".encode('utf8')
    print(res)
    
    res = bytes("你好卡沃伊!", encoding='utf8')
    print(res)
    

    b'\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x81'
    b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x8d\xa1\xe6\xb2\x83\xe4\xbc\x8a\xef\xbc\x81'

  2. chr()/ord()

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

    print(chr(97))
    print(ord('A'))
    

    a
    65

  3. divmod()商和余数

    res = divmod(10, 6)
    print(res)
    

    (1, 4)

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

    list_en = [i for i in range(10, 20)]
    
    for i, en in enumerate(list_en):
        print(f"索引{i} 元素{en}")
    
    

    索引: 0 元素: 10
    索引: 1 元素: 11
    索引: 2 元素: 12
    索引: 3 元素: 13
    索引: 4 元素: 14
    索引: 5 元素: 15
    索引: 6 元素: 16
    索引: 7 元素: 17
    索引: 8 元素: 18
    索引: 9 元素: 19

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

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

    [1, 2, 3]

  6. hash()是否可哈希。

    print(hash("abc"))  # 无论你放入什么字符串,永远返回一个固定长度的随机字符串
    

    -5268237097026950995

三、了解函数

  1. abs() 求绝对值。

    print(abs(-10))
    

    10

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

    all_lsit = [1, 2, 3]
    print(all(all_lsit))
    
    all_lsit2 = [1, 2, 0]
    print(all(all_lsit2))
    

    True
    False

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

    any_lsit = [1, 2, 3]
    print(any(any_lsit))
    
    any_lsit2 = [0]
    print(any(any_lsit2))
    

    True
    False

  4. bin()/oct()/hex()

    print(bin(4))
    print(oct(8))
    print(hex(10))
    

    0b100
    0o10
    0xa

  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', 'monotonic_ns', 'perf_counter', 'perf_counter_ns', 'process_time', 'process_time_ns', 'sleep', 'strftime', 'strptime', 'struct_time', 'thread_time', 'thread_time_ns', 'time', 'time_ns', 'timezone', 'tzname']

  6. frozenset() 不可变集合。

    s = frozenset({1, 2, 3}) # 不可添加值
    print(s)
    

    frozenset({1, 2, 3})

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

    print(globals())
    
    def func():
        a = 1
        print(locals())
    
    # func()
    
  8. pow()幂运算

    print(pow(2, 2))
    

    4

  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(101)))
    

    5050

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

    # 导入模块的另外一种方式
    m = __import__('time')
    print(m.time())
    

    1565770314.1605728

    1. classmethod
    2. staticmethod
    3. property
    4. delattr
    5. hasattr
    6. getattr
    7. setattr
    8. isinstance()
    9. issubclass()
    10. object()
    11. super()
posted @ 2019-08-14 16:24  RandySun  阅读(142)  评论(0编辑  收藏  举报