如何在Python你前preSS二进制文字? [英] How do you express binary literals in Python?

查看:145
本文介绍了如何在Python你前preSS二进制文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么EX preSS整数作为与Python文字的二进制数?

How do you express an integer as a binary number with Python literals?

我很容易能够找到十六进制的答案是:

I was easily able to find the answer for hex:

    >>> 0x12AF
    4783
    >>> 0x100
    256

和八:

    >>> 01267
    695
    >>> 0100
    64

如何使用文字来恩preSS二进制Python中?

答案摘要

Summary of Answers


  • 2.5的Python和更早版本:可以使用前preSS二进制 INT('01010101111',2),但不与文字

  • Python 2.5的和更早的:没有的没有办法的能恩preSS二进制文字

  • 的Python 2.6测试版:你可以做像这样: 0b1100111 0B1100111

  • 的Python 2.6测试版:还将允许 0o27 0O27 (第二个字符是字母O)重新present八进制。

  • 的Python 3.0测试版:同2.6,但将不再允许老 027 语法八进制

  • Python 2.5 and earlier: can express binary using int('01010101111',2) but not with a literal.
  • Python 2.5 and earlier: there is no way to express binary literals.
  • Python 2.6 beta: You can do like so: 0b1100111 or 0B1100111.
  • Python 2.6 beta: will also allow 0o27 or 0O27 (second character is the letter O) to represent an octal.
  • Python 3.0 beta: Same as 2.6, but will no longer allow the older 027 syntax for octals.

推荐答案

有关引用—的将来的Python的可能性:

与Python 2.6开始,你可以使用$ P $ EX preSS二进制文字PFIX 0B 0B

For reference—future Python possibilities:
Starting with Python 2.6 you can express binary literals using the prefix 0b or 0B:

>>> 0b101111
47

您还可以使用新的函数来得到一个数字的二进制重新presentation:

You can also use the new bin function to get the binary representation of a number:

>>> bin(173)
'0b10101101'

文件的开发版本:<一href=\"http://docs.python.org/dev/whatsnew/2.6.html#pep-3127-integer-literal-support-and-syntax\">What's新的Python 2.6

这篇关于如何在Python你前preSS二进制文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆