python内置函数

内置函数

一、内置函数

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

1.1 掌握

  1. bytes()

    解码字符。

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

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

    print(chr(65))
    
    print(ord('A'))
    #输出:
    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')
    l=['a','b','c']
    for index,i in enumerate(l):
        print(index,i)
    #输出:
    0 a
    1 b
    2 c
  5. eval()把字符串翻译成数据类型。

    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()
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值