Python Basics
Python is an interpreted language similar to that used in MATLAB. It has a very clean syntax, and is widely regarded as the easiest programming language to learn. It is also one of the most popular and widely used -- see the 2019 IEEE programming language survey. Python supports the principal programming language constructs, including: procedural, object-oriented, and functional programming. Python also runs on nearly all hardware capable of supporting high-level programming languages, and all hardware capable of running an operating system. Code written in Python is therefore highly portable.
Python code runs at speeds comparable to MATLAB -- both being interpreted languages -- and, like MATLAB, also offers the ability to access a JIT (just in time compiler) to speed up intensive computations, and may be compiled to an executable object as well using Cython or Nuitka.
Python was originally developed by Guido van Rossum, and has developed into two major branches: Python 2.x.x and Python 3.x.x. Eventually, the two major branches will merge into one. Currently, most operating systems, the Mac OS, and Linux in particular, ship with Python 2.x.x. as the default, and most legacy Python libraries and applications were developed in the 2.x.x dialect, but most of what I use is also available in the Python 3.x.x branch, so I am now mostly using this branch.
An extensive collection of Python tutorials and examples is available on the WWW, and by far the best way to learn Python, or any other programming language for that matter, is to use it.
Installation on Windows
Unlike Linux and the Mac OS, Windows doesn't come with Python pre-installed. Moreover, Python was developed on the Unix and Linux platforms, so many of it's dependencies originate on these platforms as well. This situation created significant challenges for the end user. Fortunately, dedicated work of the last several years has addressed these difficulties to the extent that now it's trivial to get Python up and running on Windows by using one of the "full-stack" options discussed below.
If you want access to the latest versions of all of the thousands of Python modules available from https://www.python.org/, while a bit more challenging to implement than the full-stack options, with a little care, it's relatively easy to do these days. Here's how I installed Python along with my favorite modules on a Windows 7 machine:
You should then be good to go!
What if you've messed up somewhere, like I did, and need to fix things manually?
I had to create a PYTHONPATH environmental variable by right-click opening Computer from the Start menu, and selecting Properties, and then Advanced tab. In the Advanced tab, select Environmental Variables, and create the new PYTHONPATH variable under System Variables as decribed at the link given in 2. above. My PYTHONPATH variable contains: C:\Program Files\Python36\python;C:\Program Files\Lib;C:\Program Files\Python36\DLLs;C:\Program Files\Python36\Lib\lib-tk;C:\Program Files\Python36\include;C:\Program Files\Python36\libs;C:\Program Files\Python36\Scripts;C:\Program Files\Python36\tcl;C:\Program Files\Python36\Tools;
As I mentioned earlier, PIP couldn't find the compiler needed to build parts of scipy and also numpy, so I Googled a bit and found this website: \url{http://www.manejandodatos.es/2013/10/error-installing-new-python-packages-windows-7/, which directed me to Christoph Gohlke's "Unofficial Windows Binaries for Python Extension Packages" website: \http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy-cluster. I installed Christoph's pre-compiled binaries for numpy (the larger numpy-1.13.1+mkl-cp36-cp36m-win\_amd64.whl or else it won't work) and scipy, and then also installed his version of matplotlib as well for good measure, but I don't think this was necessary. These are wheel files (.whl) which PIP can install without invoking a compiler.
Once I did the above, I had no trouble installing additional modules such as numba. And my example python codes using matplotlib with LaTeX typesetting worked perfectly!
What IDE should I use?
Once you've got PIP up to date, you can install Spyder: pip install spyder. Spyder is a very nice IDE which incorporates the IPython command interface which behaves much like MATLAB. However, before Spyder will work, you have to install pyqt5: pip install pyqt5, which is a python wrapper for the ubiquitously used QT C++ graphics and widgits library. Spyder requires pyqt5 to function correctly. Without pyqt5, entering spyder on the command line in the cmd app will try to launch Spyder, but fail after a second or two with no error messages generated. I installed pyqt5 after installing Spyder, but it would probably be better to install it first.
Two Things to Note During This Process
Python Links
In the following, the term 'stack' refers to a collection of applications, such as an editor, package manager, debugger, and source control interface, and also a set of Python function libraries. The main open source repository of Python codes is maintained by https://www.python.org/ which provides a command-line based package manager called PIP to provide install and uninstall functionality. Other organizations, such as Enthought and Continuum Analytics provide their own packages. Enthought provides proprietary packages of their own as well.
The Enthought Canopy Python stack (Free for students and faculty and seems to work well on both Windows and Mac) Enthought no longer supports Canopy. See: https://support.enthought.com/hc/en-us/articles/360038600051-Canopy-GUI-end-of-life-transition-to-the-Enthought-Deployment-Manager-EDM-and-Visual-Studio-Code
The Continuum Analytics Anaconda Python stack is free and open source to all.
The Matplotlib Plotting Library is included in both the Canopy and Anaconda stacks. Check out the Gallary. The SciPy Scientific Library contains a comprehensive library of numerical algorithms with functionality similar to that found in MATLAB. The entire SciPy library is provided in both the Canopy and Anaconda stacks.
Continuum Analytics provides the Anaconda Python Stack (Links to an external site.). This stack comes with a package manager (Conda), and Spyder IDE, but no debugger (last time I checked). Continuum Analytics was co-founded by Travis Oliphant who was the primary developer of NumPy and SciPy. I was primarily using the Anaconda stack on my MacBook Pro, but have recently switched to my own Python stack assembled using Home Brew. I also use Canopy on a Windows 7 machine. On my Linux machines, I use the standard python.org libraries and PIP package manager. My current opinion is that the jury is still out regarding which approach is best. Update: I've been using the python.org approach on my Mac for a few years now, and am pretty comfortable with it.
My latest advance is to finally incorporate one of the Python virtual environment systems; in my case, I've chosen to use pyenv. Virtual environment systems enable you to switch between any number of Python versions on the same machine – globally. In other words, you can switch between the 3.x.x. and 2.x.x branches and any older versions of each at will from the command line. How this works is explained on the GitHub repository, but the main concept to be aware of is that the entire Python stack for a given version will be installed from python.org in your home directory. You can also install a Python stack into any subdirectory you like, and limit its scope to that directory and those under it. This is helpful for application developers who plan on distributing their Python app. In this way, developers can specify particular versions of any Python module included.
One hickup using pyenv I discovered recently was that matplotlib, while working perfectly within the Spyder IDE, failed when the containing Python code was executed on the command line. The error complained about requiring a Python 'framework' to be installed. I found the fix quickly through a Google search here.
Additional Resources
Python Tutorials by Mokhtar Ebrahim