二进制数? [英] binary numbers?

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

问题描述

我使用Python外壳弄清楚如何在Python打印命令的作品。结果
当我键入​​


  

    

      

打印01结果
      1结果
      打印010结果
      8结果
      打印0100结果
      64结果
      打印030结果
      24


    
  

这是怎么回事?难道仅仅是基础2?为什么一在第二位置打印8?难道不应该是2,如果是二进制?


解决方案

启动一批以零标记为在Python 2进制这已经被认为是混乱的,令人惊讶的也不一致,如以0x开始将迎来它为十六进制。因此,在Python 3,从0开始是无效的,你用0°开始得到八进制。您也可以开始使用0B将其标记为二进制文件。

 >>> 10
10
>>>为0x10
16
>>> 0o10
8
>>> 0b10
2
>>> 010
  文件<&标准输入GT;,1号线
    010
      ^
语法错误:无效令牌

0X,0°和0B也适用在Python 2.6和Python 2.7。

I am using the python shell to figure out how the print command works in python.
When I type in

print 01
1
print 010
8
print 0100
64
print 030
24

What's going on here? Is it just base 2? Why does the "one" in the second position print as 8? Shouldn't it be 2 if it's binary?

解决方案

Starting a number with a zero marks it as octal in Python 2. This has been recognized as confusing, surprising and also inconsistent, as starting with 0x will mark it as hexadecimal. Therefore, in Python 3, starting with 0 is invalid, and you get octal by starting with 0o. You can also start with 0b to mark it as binary.

>>> 10
10
>>> 0x10
16
>>> 0o10
8
>>> 0b10
2
>>> 010
  File "<stdin>", line 1
    010
      ^
SyntaxError: invalid token

0x, 0o and 0b also works in Python 2.6 and Python 2.7.

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

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