为什么“ print(int('100',2))”在python中给出4的输出?

你好,我试图解决一些代码,但我陷入其中,我不知道为什么 print(int('100',2))给出4的输出,请向我解释。

评论
  • 先生靠过来
    先生靠过来 回复

    The int(x,base) function converts a number from a specified base to a decimal integer. 100 in base 2 is 4 in base 10.

  • 对不起
    对不起 回复

    When you call int(), you are giving it two parameters: '100', and 2. The first parameter it takes is the number as a string, and the second is the base. So, you are telling it to convert the string '100' into an integer of base 2. 100 in base 2 is four, so you are getting this output. Hope this helps!

  • 无邪
    无邪 回复

    二进制系统中的100是1 * 2 ^ 2 * 0 * 2 ^ 1 + 0 = 4