• 技术文章 >Python技术 >Python基础教程

    python int返回的方法探究

    小妮浅浅小妮浅浅2021-07-01 09:47:44原创2352

    1、int额外的方法:

    int.bit_length()

    返回二进制表示整数所需的位数,不包括符号位和前面的零:

    >>> n = -37
    >>> bin(n)
    '-0b100101'
    >>> n.bit_length()
    6

    2、返回表示整数的字节组。

    (1024).to_bytes(2, byteorder='big')
    b'\x04\x00'
    (1024).to_bytes(10, byteorder='big')
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    (-1024).to_bytes(10, byteorder='big', signed=True)
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    x = 1000
    x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')
    b'\xe8\x03'

    以上就是python int返回的方法探究,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python int
    品易云
    上一篇:python逻辑值检测如何实现 下一篇:python indent如何打印JSON数据

    相关文章推荐

    • python tkinter制作日历界面的简单步骤• python tkinter如何绑定事件?• python tkinter布局界面如何实现?• python tkinter中的Grid布局怎么用?• python tkinter中Login按钮怎么用?• 如何用print调试python3代码?• python里random.randint函数如何使用?• Python –如何将int转换为String• Python int()使用小结• python中分辨int和float的差别• python中的int函数如何使用?• Python中int与bytes相互转换• python中的int是什么意思• python int是什么意思

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网