By Ryan Wiles
Typically, mature programming languages, don’t tend to change too drastically from one version to the next. Allowing programs written for one version to continue working on newer versions with few if any changes. When changes are made to a programming language, it’s usually adding new features or making minor refinements to existing ones. Sometimes a more drastic decision is made to remove what are considered to be problematic aspects of the language. While this is done to improve the language overall, it comes at the cost of breaking backwards compatibility. The jump from Python 2 to Python 3 was one of these releases that broke backwards compatibility.
Python 3 is represents the future of the Python language. If you’re new to the language, that’s where you should focus your efforts. These tutorials are going to focus principally on Python 3, however there is still a lot of Python 2 code that hasn’t yet been ported to 3 yet. So, it is prudent to know what some of the changes between versions are and how to run Python 2 code without breaking your Python 3 environment. We’ll cover that later when we discuss sandboxes and virtual environments.
Python 2 | Python 3 |
---|---|
Two types of strings str and unicode |
All strings are unicode |
Two types of integers int and long |
All integers are called int , but have the range of a long |
Division returns int if operands are integers |
Division always returns float |
round() returns float |
round() returns int |
Unorderable types can be compared | Unorderable types raise TypeError if compared |
print was a statement |
print() is now a function |
range() , map() , filter() , zip() , dict.items() , dict.keys() , dict.values() return a list |
Now return an object for lazy evaluation |
xrange() returns an object for lazy evaluation |
Removed (replaced by range() ) |
input() ran eval() on the input |
input() returns input as a string |
raw_input() returns input as a string |
Removed (replaced by input() ) |
raise and except |
Changed argument structure |
iter.next() |
Replaced with by next(iterator) |
Comprehension loop variables leak to the global namespace | Comprehension loop variables don’t leak |
Source files default to ASCII encoding | Source files default to UTF-8 encoding |
lambdas supported tuple unpacking |
tuples are no longer unpacked |
There’s a fairly comprehensive list of all the differences at Porting Code to Python 3 with 2to3
Note: MacOS currently comes pre-installed with Python 2.7.10 located at
/usr/bin/python
. This is convenient if you want to quickly play around with Python. However, you’ll quickly find that you want to start installing additional packages or switch between Python 2 & 3 and it’s best not mess up the OSes bundled Python environment.Instead, we’re going to install Python using Homebrew. Homebrew will install Python 2/3 in
/usr/local/bin
. But since that directory is usually added to the PATH variable after/usr/bin
. This means that typing inpython
is still going to run the MacOS’s version of Python. To run Homebrew’s Python executables, we’ll typepython2
orpython3
instead.If you really want to have the shell run the Homebrew version when you type
python
, you would have to change your user’s PATH environment variable or set up an alias. I’m not going to go into that process, since we’re going to introduce a more flexible approach later on.
Similar to the apt and yum package managers on Linux. Homebrew is a command line package manager for MacOS allowing you to install and manage many popular UNIX/Linux software packages.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Full installation instructions for Homebrew are here: https://docs.brew.sh/Installation
On Windows: https://docs.python.org/3.3/using/windows.html
On Mac: https://docs.python.org/3.3/using/mac.html
brew install python # installs Python 3
brew install python@2 # installs Python 2
On Linux: https://docs.python.org/3.3/using/unix.html
$ which python2
/usr/local/bin/python2 # This is where HomeBrew places it
$ python2 --version
Python 2.7.15 # The 2 is the important part, the exact minor versions may vary
$ pip2 --version # Verify pip for python2 is installed
pip 18.0 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
$ which python3
/usr/local/bin/python3 # This is where HomeBrew places it
$ python3 --version
Python 3.7.0 # The 3 is the important part, the exact minor versions may vary
$ pip3 --version # Verify pip for python3 is installed
pip 18.0 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
$ python2 # to enter the Python 2 REPL shell
$ python3 # to enter the Python 3 REPL shell
© 2018 Ryan Wiles