Python, 2.x와 3의 차이
※ 2.x는 2.5나 2.6버전을 의미함
-. print가 함수형태로 변경됨
python 2.x
>>> print "welcome to","python" welcome to python >>> |
python 3
>>> print "welcome to","python" welcome to python >>> |
또한 입력인자로 구분자(sep), 끝라인(end), 출력(file)을 지정해 줄수 있다.
>>> print("welcome to","python",sep="~",end="!",file=sys.stderr) welcome to~python! >>> |
이와 유사하게 입출력관련해서 raw_input이 input으로 변경되고, as,with예약어가 추가되었으며, 새로운 문자열 포맷팅을 제공함
-. long형이 없어지고 ing형으로 통일됨
python 2.x
>>> type(2**31) <type 'long'> >>> sys.maxint 2147483647 >>> type(sys.maxint) <type 'int'> >>> type(sys.maxint+1) <type 'long'> |
python 3
>>> type(2**31) <class 'int'> >>> type(2**40) <class 'int'> |
위와 같이 2.x에서는 2의 31제곱이 long형이였는데, 3에서는 2의 31제곱은 물론 2의 40제곱도 int형인것을 알수 있음
2.x에서는 sys.maxint이하의 값은 int로 처리되고 그 이상의 값은 long으로 처리되었는데 3에서부터는 모두 int로 처리됨
-. int / int의 결과는 float로 처리됨
python 2.x
>>> 3/2 1 >>> 3.0/2.0 1.5 |
python 3
>>> 3/2 1.5 |
python 2.x에서는 int / int = int로만 출력이 되었지만 python3에서는 int / int = float로 출력이 된다.
-. String, Unicode체계가 바뀌었음
python 2.x
>>> type('가') <type 'str'> >>> type('가'.decode('utf-8')) <type 'unicode'> >>> type(u'가') <type 'unicode'> |
python 3
>>> type(u'가') SyntaxError: invalid syntax >>> type('가') <class 'str'> >>> type('가'.encode('cp949')) <class 'bytes'> |
위에서 볼수 있듯이 python 2.x에서는 string과 unicode로 구분되어있으며,
python 3에서는 string과 bytes로 구분이 됨
python 2.x에서는 일반스트링이 인코딩이 있는 문자열이었고 유니코드가 따로 있었는데
python 3에서는 유니코드를 따로 지정하지 않고 일반 스트링이 기존의 유티코드와 통일하며, 인코딩이 있는 문자열은 bytes로 표현됨
참조 : 빠르게 활용하는 파이썬3 프로그래밍