IT TIP

파이썬에서 0으로 시작하는 숫자는 무엇을 의미합니까?

itqueen 2020. 12. 14. 21:28
반응형

파이썬에서 0으로 시작하는 숫자는 무엇을 의미합니까?


파이썬 앞에 0이있는 작은 정수를 입력하면 이상한 결과가 나타납니다. 왜 이런거야?

>>> 011
9
>>> 0100
64
>>> 027
23

참고 : Python 버전 2.7.3은 Python 3.0에서 테스트했으며 지금은 오류입니다. 따라서 버전별로 다릅니다.

편집 : 그들은 분명히 여전히 정수입니다.

>>> type(027)
`<type 'int'>`

8 진수 (8 진수)로 표현 된 숫자입니다. 몇 가지 예 :

Python 2 (이전 형식)

참고 : 이러한 양식은 Python 2.x에서만 작동합니다.

0111*(8**1) + 1*(8**0)= 9와 같고 ,

0100같은지 1*(8**2) + 0*(8**1) + 0*(8**0)= 64

0272*(8**1) + 7*(8**0)= 16 + 7 = 23 과 같습니다 .

Python 3 (새 형식)

Python 3에서는 8 진수 상수 (예 : 또는 등) 를 나타내는 0o대신을 사용해야합니다 . Python 2.x 버전> = 2.6은 새 형식과 이전 형식을 모두 지원합니다.00o110o27

0o111*(8**1) + 1*(8**0)= 9와 같고 ,

0o100같은지 1*(8**2) + 0*(8**1) + 0*(8**0)= 64

0o272*(8**1) + 7*(8**0)= 16 + 7 = 23 과 같습니다 .


Python 2 (및 몇 가지 다른 프로그래밍 언어)에서 이들은 8 진수를 나타냅니다 .

Python 3에서는 011더 이상 작동하지 않으며 0o11대신 사용 합니다.

에 대한 응답으로 : 그리고 그들은 일반 정수입니다. 그들은 단지 다른 방식으로 지정됩니다. 그리고 그것들은 파이썬에 의해 자동으로 내부 정수 표현으로 변환됩니다 (실제로는 base-2이므로 9둘 다 011내부적으로으로 변환됩니다 0b1001).


Python 버전 2와 3은 모두 선행 '0o'및 '0O'(대문자 o)로 작성된 8 진법을 이해하므로 Python 2.x에서도 if를 사용하는 습관을 가지십시오.

문자열의 숫자에는 선행 0 만 사용하십시오.

int ()를 사용하여 다른 기본 시스템의 정수를 변환 할 수 있습니다.

>>> int(0o20)

16

출력을 선행 0으로 표시하려면 다음 답변에 따라 정의하십시오. 선행 0이있는 숫자 표시

우편 번호로 작업 할 계획이 있다면 모든면에서 문자열로 취급하는 것이 가장 좋습니다.


8 진법의 숫자. 다른 접두사는 0x16 진수 및 0b이진용입니다.


8 진수 (밑수 8, 값 0-7)

oct () 함수를 사용하여 10 진수를 8 진수로 변환 할 수 있습니다 .

In [125]: for i in range(10):
   .....:     print '{:5} {:5}'.format(i, oct(i))
   .....:
    0 0
    1 01
    2 02
    3 03
    4 04
    5 05
    6 06
    7 07
    8 010
    9 011

적절한 밑수 (이 경우 8 )를 사용하여 int () 함수를 사용하여 8 진수 값을 정수로 변환합니다 .

int(str(17), 8)
Out[129]: 15

유사한 규칙 / 함수 세트가 hex () 함수를 사용하는 16 진수 ( 밑수 16)에 적용됩니다 .


아주 쉽습니다. 8 진수입니다.

http://en.wikipedia.org/wiki/Octal

Also there are numbers that are starting with 0x. They are hexadecimal numbers:

>>> 0x51
81

They are apparently octal (base 8) numbers, and the 0 is just an outdated prefix that Python 2 used to use.

In Python 3 you must write: 0o11 instead.

They are still integers but doing operations with them will give a result in regular base-10 form.


I have tried it out. I have learned a little bit. From Java I know this as a pitfall: A leading zero introduces an octal number. I have colleagues which didn't resp. don't know that after more than years with experience in Java.

Now I was interesting what the behaviour in Python is. I appreciate that change from Python 2 to Python 3. It is pitfall I have never understood why Java (a young language with focus on "beeing easy") could take over that stupid solution from C.

A leading zero as prefix can be accidentally typed (bad solution). 0x for a hexadecimal number is never accidentally typed (good solution). But 0 and o are to similar therefore I think 0o23 in Python is not a perfect solution. 0c23 (octal) would be a better solution in my opinion.


The latest Python specification for number literals is in PEP-3127 "Integer Literal Support and Syntax"

The syntax for 011 has been removed in Python 3.0. The syntax for 0o11 has been added in Python 2.6 and it is the only one supported after Python 3.0. There is no __future__ import in Python 2 that would disallow or atleast warn on number literals with leading zeros, so developers have to know what 011 means and that it should be avoided (and that it can actually be avoided).

참고URL : https://stackoverflow.com/questions/11620151/what-do-numbers-starting-with-0-mean-in-python

반응형