Python tutorials > Core Python Fundamentals > Basics and Syntax > What are Python 2 vs 3 differences?
What are Python 2 vs 3 differences?
Python 2 and Python 3 are distinct versions of the Python programming language. While Python 2 was widely used for many years, Python 3 introduced significant changes aimed at improving the language's clarity, consistency, and efficiency. Understanding the key differences between these versions is crucial for developers working with legacy code or starting new Python projects.
Print Statement vs. Print Function
In Python 2, print
is treated as a statement, whereas in Python 3, it's a function. This seemingly small change has broader implications for how output is handled. The function syntax in Python 3 is more consistent with other function calls in the language and allows for more flexible output formatting using keyword arguments such as sep
and end
.
Python 2:
print "Hello, world!"
Python 3:
print("Hello, world!")
Unicode Handling
Python 2 has separate types for strings (str
) and Unicode strings (unicode
). By default, strings are ASCII encoded. Unicode literals are denoted with a u
prefix. You often need to explicitly declare the encoding at the top of the file using a comment like # -*- coding: utf-8 -*-
to handle non-ASCII characters correctly.
In Python 3, all strings are Unicode by default. The str
type represents Unicode text, and a separate bytes
type is used for sequences of bytes. This greatly simplifies handling international characters and eliminates many common encoding-related errors.
Python 2:
# -*- coding: utf-8 -*-
s = u'你好'
print type(s)
Python 3:
s = '你好'
print(type(s))
Integer Division
In Python 2, dividing two integers results in integer division (truncating the decimal part). To get floating-point division, at least one of the operands needs to be a float. In Python 3, /
always performs true division (returning a float), while //
performs floor division (returning an integer).
Python 2:
print 5 / 2 # Output: 2
print 5.0 / 2 # Output: 2.5
Python 3:
print(5 / 2) # Output: 2.5
print(5 // 2) # Output: 2
xrange
vs. range
In Python 2, range
returns a list, while xrange
returns an iterator (similar to a generator) that generates numbers on demand. xrange
is more memory-efficient for large ranges. In Python 3, range
behaves like xrange
in Python 2, and xrange
is no longer available.
Python 2:
for i in xrange(5):
print i
Python 3:
for i in range(5):
print(i)
Error Handling (except
Syntax)
The syntax for catching exceptions has changed. In Python 2, you use except Exception, e:
, while in Python 3, you use except Exception as e:
. The as
syntax is considered more readable and consistent with other language constructs.
Python 2:
try:
# Code that might raise an exception
pass
except Exception, e:
print "Error: ", e
Python 3:
try:
# Code that might raise an exception
pass
except Exception as e:
print("Error: ", e)
next()
Function and Iterators
In Python 2, iterators have a next()
method. In Python 3, the next()
function is a built-in function that takes an iterator as an argument. This change provides a more consistent and general way to retrieve the next item from an iterator.
Python 2:
my_iterator = iter([1, 2, 3])
print my_iterator.next()
Python 3:
my_iterator = iter([1, 2, 3])
print(next(my_iterator))
input()
vs. raw_input()
In Python 2, raw_input()
reads a line from input and returns it as a string, while input()
tries to evaluate the input as a Python expression. This can be dangerous if the user enters malicious code. In Python 3, input()
behaves like raw_input()
in Python 2, returning the input as a string. To evaluate input as a Python expression, you can use eval(input())
, but it should be done with caution due to security risks.
Python 2:
name = raw_input("Enter your name: ")
print "Hello, " + name
number = input("Enter a number: ") # Be careful, evaluates the input as Python code
print number
Python 3:
name = input("Enter your name: ")
print("Hello, " + name)
Concepts Behind the Snippets
The core concept behind these differences is the evolution of Python towards a more consistent, readable, and efficient language. Unicode support is greatly improved in Python 3, leading to fewer encoding-related errors. The print
function provides more flexible output formatting. The change in integer division makes the language's behavior more predictable. The removal of xrange
and the modification of range
simplifies iteration. Standardizing the except
syntax and the next()
function promotes code consistency. The handling of user input is made safer by making input()
behave like Python 2's raw_input()
.
Real-Life Use Case Section
Imagine you're migrating a large Python 2 codebase that handles user data including names and addresses from different countries. Python 3's default Unicode support would significantly reduce the risk of encoding errors and make it easier to process data containing characters from various languages. Consider a data analysis project involving large datasets. The memory efficiency of Python 3's range()
function (which behaves like Python 2's xrange()
) would be beneficial when iterating over large ranges of numbers.
Best Practices
If you're starting a new project, always use Python 3. It's the actively supported version of the language and offers many advantages over Python 2. When migrating from Python 2 to Python 3, use the 2to3
tool to automate much of the conversion process. Thoroughly test your code after the conversion to ensure that it behaves as expected. Pay close attention to Unicode handling, integer division, and exception handling. If you must maintain a Python 2 codebase, familiarize yourself with the key differences to avoid common pitfalls.
Interview Tip
When discussing Python 2 vs. 3 differences in an interview, demonstrate a clear understanding of the major changes. Focus on the rationale behind these changes and how they improve the language. Be prepared to provide specific examples, such as Unicode handling and integer division. Highlight the importance of migrating to Python 3 and the benefits it offers.
When to Use Them
Use Python 3 for all new projects. Only use Python 2 if you're maintaining legacy code that hasn't been migrated to Python 3. Before choosing Python 2 for new development, carefully consider the long-term maintenance costs and the benefits of using a modern language.
Memory Footprint
Python 3's range()
(which is equivalent to Python 2's xrange()
) generally has a smaller memory footprint than Python 2's range()
, especially when dealing with large ranges, because it generates numbers on demand instead of creating a large list in memory. Overall memory usage between the versions can vary and depends heavily on the code being executed. Certain libraries and modules might have optimizations or inefficiencies unique to either version. The switch to unicode strings by default can also increase memory usage in some cases compared to Python 2's ASCII strings, but decreases the amount of errors, and the convenience often outweight the downsides.
Alternatives
There are no direct alternatives to Python 2 or Python 3 within the Python ecosystem. However, if you are looking for cross-compatibility, tools like future
and six
libraries can help write code that works on both Python 2 and Python 3. These libraries provide compatibility layers and helper functions to bridge the gaps between the two versions. For entirely different alternatives to Python, consider languages like Ruby, Go, or JavaScript (Node.js), each with its own strengths and weaknesses.
Pros
Python 3:
Python 2:
Cons
Python 3:
Python 2:
FAQ
-
Is Python 2 still used?
While Python 2 reached its end-of-life in 2020, some legacy systems still rely on it. However, it's strongly recommended to migrate to Python 3 for security and maintainability reasons.
-
How can I convert Python 2 code to Python 3?
You can use the
2to3
tool, which is included in the Python distribution, to automate much of the conversion process. However, manual review and testing are still necessary to ensure that the code behaves as expected. -
What is the biggest difference between Python 2 and Python 3?
One of the biggest differences is the way they handle Unicode. Python 3's default Unicode support simplifies handling international characters and eliminates many common encoding-related errors that were prevalent in Python 2.