python內置函數

內置函數

1、內置函數

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

1.1 掌握

  1. bytes()函數

    解碼字符。學習

    res = '你好'.encode('utf8')
    print(res)
    #輸出:
    b'\xe4\xbd\xa0\xe5\xa5\xbd'
  2. chr()/ord()測試

    chr()參考ASCII碼錶將數字轉成對應字符,ord()將字符轉換成對應的數字。ui

    print(chr(65))
    
    print(ord('A'))
    #輸出:
    A
    65
  3. divmod()分欄翻譯

    print(divmod(10,3))
    #輸出:
    (3,1)
  4. enumerate()帶有索引的迭代code

    l=['a','b','c']
    for i in enumerate(l):
        print(i)
    #輸出:
    (0, 'a')
    (1, 'b')
    (2, 'c')
    l=['a','b','c']
    for index,i in enumerate(l):
        print(index,i)
    #輸出:
    0 a
    1 b
    2 c
  5. eval()把字符串翻譯成數據類型。htm

    l="['a','b','c']"
    print(l)
    print(type(l))
    print(eval(l))
    print(type(eval(l)))
    #輸出:
    ['a','b','c']
    <class 'str'>
    ['a', 'b', 'c']
    <class 'list'>
  6. hash()是否可哈希。對象

    print(hash(1))
    #輸出:
    1

    1.2瞭解

    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', '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()查看全局名字,查看局部名字

      def func():
          a = 1
          print(locals())
      func()
      #輸出:
      {'a': 1}
      def func():
          a = 1
          print(globals())
      func()
      #輸出:
      {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001AD03175B08>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'F:/python學習/測試/測試2.py', '__cached__': None, 'func': <function func at 0x000001AD04E67048>}
    8. pow()平方

      print(pow(2,4))
      print(pow(3,2,2))# (3**2)%2
      #輸出:
      
      16
      1
    9. round()四捨五入

      print(round(3.56))
      #輸出:
      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())
      #輸出:
      1565786439.943154

1.3面向對象知識點

  1. classmethod
  2. staticmethod
  3. property
  4. delattr
  5. hasattr
  6. getattr
  7. setattr
  8. isinstance()
  9. issubclass()
  10. object()
  11. super()
相關文章
相關標籤/搜索