python如何解释带前导零的数字 [英] How does python interpret numbers with leading zeroes

查看:88
本文介绍了python如何解释带前导零的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,我使用的是 Python 2.7当我在 python shell 上输入时:

I'm new with python, I'm using python 2.7 when I typed this on python shell:

print 01
print 010
print 0100
print 01000

它给出了这个结果

1
8
64
512

我试图理解它为什么会这样,但不幸的是我没有明白这一点.

I tried to understand why it gave that but unfortunately I didn't get the point.

推荐答案

如果一个数字以 0 开头,它会被解释为八进制或以 8 为基数.只要这样做:

If a number starts with 0, it is interpreted as octal, or base 8. Just do:

print 1
print 10
print 100
print 1000

您的问题将得到解决.

更多关于八进制:http://en.wikipedia.org/wiki/Octal

这是一种更容易理解八进制的方法:

Here is a way to understand octal easier:

八进制 1 是十进制(普通数)1

octal 1 is decimal (normal numbers) 1

八进制 2 : 十进制 2

octal 2 : decimal 2

...

八进制 7 : 十进制 7

octal 7 : decimal 7

八进制 10:十进制 8

octal 10: decimal 8

八进制 11:十进制 9

octal 11: decimal 9

八进制 12:十进制 10

octal 12: decimal 10

...

八进制 17:十进制 15

octal 17: decimal 15

八进制 20:十进制 16

octal 20: decimal 16

等等.八进制只使用从 0 到 7 的数字.

and so on. Octal just uses digits from 0 to 7.

希望这有帮助!

这篇关于python如何解释带前导零的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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