Python 3
Moving to Python 3
Since the Python 3 series was first released in 2008, the core team has consistently produced releases that have moved the language forward. If you work for an organization that has built their applications in Python 2.7 (or older), you need to carefully evaluate the differences between the Python 2 series and the Python 3 series.
In order to assist with the migration, many large organizations run Python 2.7 in parallel. Since you can safely install both ActivePython 2.7 and ActivePython 3 side-by-side on your computer, you can experiment with Python 3 while still using Python 2 for your current code.
Download ActivePython to help with your Python migration project. ActivePython is available for the Python 3.4, 3.5 and 3.6 for Windows, Linux and macOS.
Python 3 Release Highlights
Users are moving to Python 3 because of the significant improvements in the language. These include integer division (which is much simpler and stronger) as well as asynchronous support, better exception handling, Unicode support, and the ability to mix tabs and spaces. Python 3 also gives you function annotations and range memory objects which are significantly more memory efficient.
Some of the highlights of the changes are listed below or get a more detailed overview of what’s new in Python 3.x:
“PRINT” is a function
The print
statement has been replaced with a print()
function, with keyword arguments to replace most of the special syntax of the old print statement.
Old: print "The answer is", 2*2
New: print("The answer is", 2*2)
Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)
Unicode Changes
How Python handles text vs. data has changed considerably. All text is Unicode (represented by the str
type), all binary data (e.g. 8-bit strings, encoded Unicode) is held in the new bytes
type.
Syntax Changes
Python 3 supports function argument and return value annotations. Functions can declare keyword-only arguments. Extended iterable unpacking allows code like:
a, b, *rest = some_sequence
*rest, last = some_sequence
Other syntax changes:
d = {k: v for k, v in stuff} # dictionary comprehensions
s = {"peter", "paul", "mary"} # set literals
mode = 0o755 # octal literals
mask = 0b01001111 # binary literals
data = b"marker" # bytes literals
Much More
There are many more changes in Python 3. Resources for Python 3:
- What’s New in Python 3.6
- What’s New in Python 3.5
- What’s New in Python 3.4
- What’s New in Python 3.3
- What’s New in Python 3.2
- What’s New in Python 3.1
- What’s New in Python 3.0
- Full Python 3.2 documentation
- Python 3000 development document (PEP 3000)
Porting to Python 3
The currently suggested path for developing for both Python 2 and 3 is to maintain code for Python 2.6/2.7 and use:
- The
python -3
command line option in Python 2.6 / 2.7 to enable warnings about Python 3 porting issues. - The
2to3
source-to-source translation tool included in Python 2.6/ 2.7 and Python 3 distributions.
Developers worldwide rely on ActivePython’s completeness and ease-of-use, while corporate users protect their infrastructure and stay competitive with quality-assured ActivePython business solutions.