Python(파이썬) - 2.x vs 3.x 차이

CODEDRAGON Development/Python

반응형

   

파이썬 버전

  • 주요버전은 2.x와 3.x 버전이 있으며
  • 3.x버전은 2008에 처음으로 나왔습니다.

   

   

   

long 자료형이 없어지고 int로 단일화

2.x

>>>type(2**31)

<type 'long'>

>>>sys.maxint

2147483647

maxint 이하는 int 이상이면 long타입이 됨

3.x

>>>type(2**31)

<class 'int'>

>>>type(2**40)

<class 'int'>

  

maxint 값도 모두 int로 처리됨

   

   

   

int / int의 결과값이 float 자료형으로 처리

2.x

>>>3 / 2

1

3.x

>>>3 / 2

1.5

>>>type(3/2)

<class 'float'>

   

   

   

String, Unicode체계 변경

2.x

>>>type('가')

<type 'str'>

>>>type(u'가')

<type 'unicode'>

일반스트링이 인코딩이 있는 문자열이고

유니코드가 따로 존재했습니다.

3.x

>>>type('가')

<type 'str'>

>>>type('가'.encode('cp949'))

<type 'bytes'>

일반스트링이 유니코드와 동일하고

인코딩이 있는 문자열은 bytes로 표현됩니다.

   

   

   

print가 함수로 변경 (주의)

파이썬 3 버전은 print함수로 변경되어 출력할 문자열을 괄호안에 표시해 주어야 합니다.

2.x

>>> print "Hellow World"

Hello World

3.x

>>> print ("Hello World")

Hello World

>>> print "Hellow World"

SyntaxError: invalid syntax

>>>

   

   

   

사용자 입력한 값을 가져오는 함수

raw_input()이 없어지고 input()를 지원

2.x

raw_input()

3.x에서 없어짐

함수 호출시 에러메시지 발생

>>> str = raw_input()

Traceback (most recent call last):

File "<pyshell#55>", line 1, in <module>

str = raw_input()

NameError: name 'raw_input' is not defined

>>>

3.x

input()

>>> str = input();

python

>>> print(str)

python

  

  

   

   

   

3.x은 2.x과 하위호완성을 지원하지 않습니다.

   

반응형

'Development > Python' 카테고리의 다른 글

개발환경구축 - Python 인터프리터 설정  (0) 2015.08.06
개발환경구축 - PyDev설치  (0) 2015.07.31
문자(String)  (0) 2015.07.22
실행 - PyCharm  (0) 2015.07.16
설치 - PyCharm  (0) 2015.07.10