"Python 버전 2와 3 차이"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 3명의 중간 판 16개는 보이지 않습니다)
2번째 줄: 2번째 줄:
;Python 2와 3의 차이
;Python 2와 3의 차이
;파이썬 버전 2와 3의 차이
;파이썬 버전 2와 3의 차이
* 모든 변수가 class로 다루어진다.
* 파이썬 3에서는 모든 변수가 [[객체]](object)로 처리됨
* 이외에 여러가지 내부적 변화가 있지만, 개발자 입장에서는 다음 4가지 정도를 알아두면 된다.
* 이외에 여러가지 내부적 변화가 있지만, 다음 4가지 정도를 알아두면 된다.
 
==int 나누기 결과가 float ★★★ ==
* 결과가 다르므로 특히 유의해야 함
{{소스헤더|Python 2}}
<syntaxhighlight lang='python'>
print( 1/2 )
print( type(1/2) )
# 0
# <type 'int'>
</syntaxhighlight>
 
{{소스헤더|Python 3}}
<syntaxhighlight lang='python'>
print( 1/2 )
print( type(1/2) )
# 0.5
# <class 'float'>
</syntaxhighlight>


==print문 괄호 필수 ★★==
==print문 괄호 필수 ★★==
;Python 2
{{소스헤더|Python 2}}
<source lang='python'>
<syntaxhighlight lang='python'>
print( 'hello' )
print( 'hello' )
# hello
# hello
print 'hello'
print 'hello'
# hello
# hello
</source>
</syntaxhighlight>


;Python 3
{{소스헤더|Python 3}}
<source lang='python'>
<syntaxhighlight lang='python'>
print( 'hello' )
print( 'hello' )
# hello
# hello
print 'hello'
print 'hello'
# Error! invalid syntax
# Error! invalid syntax
</source>
</syntaxhighlight>


==String과 Unicode 통일 ★==
==str과 unicode 통일 ★==
;Python 2
{{소스헤더|Python 2}}
<source lang='python'>
<syntaxhighlight lang='python'>
print( type('hello') )
print( type('hello') )
print( type(u'hello') )
print( type(u'hello') )
# <type 'str'>
# <type 'str'>
# <type 'unicode'>
# <type 'unicode'>
</source>
</syntaxhighlight>


;Python 3
{{소스헤더|Python 3}}
<source lang='python'>
<syntaxhighlight lang='python'>
print( type('hello') )
print( type('hello') )
print( type(u'hello') )
print( type(u'hello') )
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>
</source>
</syntaxhighlight>
 
:→ 모든 문자열은 유니코드인 str 클래스(의 인스턴스)이다.
==나누기 결과가 float==
;Python 2
<source lang='python'>
print( 1/2 )
print( type(1/2) )
# 0
# <type 'int'>
</source>
 
;Python 3
<source lang='python'>
print( 1/2 )
print( type(1/2) )
# 0.5
# <class 'float'>
</source>


==long → int로 통일==
==long → int로 통일==
;Python 2
{{소스헤더|Python 2}}
<source lang='python'>
<syntaxhighlight lang='python'>
print( 2**30 )
print( 2**30 )
print( type(2**30) )
print( type(2**30) )
67번째 줄: 69번째 줄:
# 1267650600228229401496703205376
# 1267650600228229401496703205376
# <type 'long'>
# <type 'long'>
</source>
</syntaxhighlight>


;Python 3
{{소스헤더|Python 3}}
<source lang='python'>
<syntaxhighlight lang='python'>
print( 2**30 )
print( 2**30 )
print( type(2**30) )
print( type(2**30) )
79번째 줄: 81번째 줄:
# 1267650600228229401496703205376
# 1267650600228229401496703205376
# <class 'int'>
# <class 'int'>
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[Python 소스코드 버전 변환 2to3.py]]
*[[Python]]
*[[Python]]
*[[2to3.py]]
 
== 참고 ==
* [http://python-future.org/compatible_idioms.html 파이썬 2와 파이썬 3에서 모두 호환되는 스크립트를 작성하는 팁]


[[분류: Python]]
[[분류: Python]]

2020년 11월 2일 (월) 02:31 기준 최신판

1 개요[ | ]

Python 2와 3의 차이
파이썬 버전 2와 3의 차이
  • 파이썬 3에서는 모든 변수가 객체(object)로 처리됨
  • 이외에 여러가지 내부적 변화가 있지만, 다음 4가지 정도를 알아두면 된다.

2 int 나누기 결과가 float ★★★[ | ]

  • 결과가 다르므로 특히 유의해야 함
Python 2
print( 1/2 )
print( type(1/2) )
# 0
# <type 'int'>
Python 3
print( 1/2 )
print( type(1/2) )
# 0.5
# <class 'float'>

3 print문 괄호 필수 ★★[ | ]

Python 2
print( 'hello' )
# hello
print 'hello'
# hello
Python 3
print( 'hello' )
# hello
print 'hello'
# Error! invalid syntax

4 str과 unicode 통일 ★[ | ]

Python 2
print( type('hello') )
print( type(u'hello') )
# <type 'str'>
# <type 'unicode'>
Python 3
print( type('hello') )
print( type(u'hello') )
# <class 'str'>
# <class 'str'>
→ 모든 문자열은 유니코드인 str 클래스(의 인스턴스)이다.

5 long → int로 통일[ | ]

Python 2
print( 2**30 )
print( type(2**30) )
print( 2**100 )
print( type(2**100) )
# 1073741824
# <type 'int'>
# 1267650600228229401496703205376
# <type 'long'>
Python 3
print( 2**30 )
print( type(2**30) )
print( 2**100 )
print( type(2**100) )
# 1073741824
# <class 'int'>
# 1267650600228229401496703205376
# <class 'int'>

6 같이 보기[ | ]

7 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}