Here are some key differences between Python 2 and Python 3:

 

 

Division: In Python 2, integer division always results in an integer. In Python 3, integer division results in a float if the result is not an integer.

print(7 / 5 )
print(-7 / 5)	 
''' 
Output in Python 2.x 
1 
-2 

Output in Python 3.x : 
1.4 
-1.4 
'''

 

Annotations: Python 3 supports type annotations, which can help with code clarity and maintainability. Python 2 does not support type annotations.

 

Exceptions: In Python 2, exceptions are enclosed in notations. In Python 3, exceptions are enclosed in parentheses.

 

 

Storage of Strings: In Python 2, strings are stored as ASCII by default, while in Python 3, strings are stored as Unicode by default. This means that in Python 3, you can use non-ASCII characters in your strings without having to explicitly encode them. 

print(type('default string ')) 
print(type(u'string with b ')) 
''' 
Output in Python 2.x (Unicode and str are different) 
<type 'str'> 
<type 'unicode'> 

Output in Python 3.x (Unicode and str are same) 
<class 'str'> 
<class 'str'> 
'''

 

 

Print statement: In Python 2, the print statement is used to print output to the console. In Python 3, the print function is used instead. The print function is more flexible than the print statement, and it allows you to print multiple values on the same line, for example. 

 

Range function: In Python 2, the xrange() function is used to generate a sequence of numbers. In Python 3, the range() function is used instead. The range() function is more efficient than the xrange() function, and it can be used to generate infinite sequences. 

 

 

 

'ML Engineering > python' 카테고리의 다른 글

03. Binary Tree DFS Techniques  (0) 2024.08.06
02. Sliding Window Technique  (0) 2024.08.06
01. Two Pointers Technique  (0) 2024.08.06
Differences between subarrays, substrings, subsequences, and subsets  (0) 2024.08.06
Python - Data types  (0) 2024.04.05

+ Recent posts