python 内置函数 进制转换

4.内置函数

  • 自定义函数

  • 内置函数

    • len

    • Open

    • id()

    • type()

    • range()

    • 输入输出

      • print()
      • input()
    • 强制转换

      • int()
      • float()
      • list()
      • tuple()
      • dict()
      • bool()
      • str()
      • set()
    • 数学计算

      • abs()

      • min()

      • sum()

      • divmod :两数相除求商和余数

        v=1001
        div,mod=divmod(1001,5)
        print(div,mod)
        
        • 练习 分页展示

          # 分页显示
          INFO_LIST = []
          for i in range(836):
              template = "第%s天,笨笨先僧 always be there with you" % i,
              # print(template)
              INFO_LIST.append(template)
          
          per_page_count = 10
          total_page, rem = divmod(836, per_page_count)
          if rem > 0:
              total_page = total_page + 1
          
          # 计算出总页数
          # 输入页数  显示
          while True:
              val = input("请输入页数:")
              
              val = int(val)
              if val>total_page_count or val<1
              start = (val - 1) * per_page_count
              end = val * per_page_count
              for ele in range(start, end):
                  print(INFO_LIST[ele])
          
          
      • 进制转换相关

        • bin():binary

        • oct(): 八进制(octal)

        • hex(): 十六进制 (hexadecimal)

        • 【八进制 二进制 十六进制 】之间不能相互转换 只能先转为十进制 才能转为其他进制

        • int():十进制

          v1="0b1101"
          result=int(v1,base=2)
          print(result)
          print(bin(result))#二进制
          
          v1="0o1101"
          print(int(v1,base=8))#八进制
          
          v1="0xa"
          print(int(v1,base=16))#十六进制
          
          
          
          

        练习:
        1.将ip中的数字转为二进制 后形成新的二进制 计算出新二进制的int值

        ip = "192.168.12.79"
        ip_list = ip.split(".")
        for i in range(len(ip_list)):
            ele = bin(int(ip_list[i]))#得到 "0b11000000 0b10101000 0b1100 0b1001111"
            ip_list[i] = ele[2:]#把0b切掉 得到后面的二进制01内容
            #ip_list[i]=ip_list[i].strip("0b")也可以去掉
            if len(ip_list[i]) < 8:#补全到八位
                str = "0" * (8 - len(ip_list[i])) + ip_list[i]
                ip_list[i] = str
        val = "".join(ip_list)
        print(int("0b"+val, base=2))
        
原文地址:https://www.cnblogs.com/koukouku/p/10692079.html