Notes on Python

To follow along with the example below, make sure to install the Anaconda program and explore the use of Jupyter notebooks as seen on the Jupyter Web page. The Python scripting language provides services similar to MATLAB in performing data manipulation and visualization. Many people have used Python for this purpose for years. Python grew rapidly as a popular data manipulation language that processed inputs, internals, and outputs to scientific models, even allowing for models written in other languages to be wrapped by Python (see SWIG, for example) for easy data infusion and reporting purposes.

Python interprets statements one at a time where as many scientific models are written on sophisticated platforms that run code in other ways (parallel, as opposed to sequential, processing included).

Before the coordinated notebooking facilities, Python could be invoked in two popular ways (which will still work fine today on a machine where the Anaconda application suite of tools have been loaded. Before showing examples of invocation methods, note that the MATLAB script from the MATLAB page is available as a Python notebook here (to download it, right-mouse click on the link and choose Save Link As from the pop-up menu).

Using Jupyter Notebook

The Jupyter Python notebook service affords piecemeal development and running of Python code. Python services can be imported via the import ... as or from import statements that are included in the base Python language. This first example uses three very popular Python packages to do useful work in a notebook: NumPy, MatPlotLib, and SciPy. NumPy is the fundamental package for scientific computing with the Python language. Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. SciPy is a Python-based ecosystem of open-source software for mathematics, science, and engineering. As SciPy contains many sub-packages, it is common to ask for just one of the packages as in the matplotlib.pyplot example seen in section [1] of the notebook screenshot below (pyplot is a plotting package for plotting data with Python syntax).

The base Python language provides a useful print() function to print the value of any intermediate variable involved in a computation or visualization. Section [2] of the partial notebook screenshot below demonstrates the use of the print() function to print the state of a variable named nz, which is being managed as an integer variable.

In this class, you can use Python in addition to MATLAB by learning the rules of syntax for writing valid code, learning how to declare variables and initialize them with data, learning how to perform useful computation and visualization with those data, and learning which approaches provided by which packages are the best for an overall data analysis activity.



You run a Jupyter Python notebook by clicking on the Run button for each code step in the notebook. In the case of the NPS model, running in the notebook requires running nine steps (and so hitting the Run button nine times in succession). Breaking the code into chunks makes it more reusable and gives you time to evaluate intermediary steps while you are building your script for an analysis. When you run the sixth step, you should see plots similar to the plots shown on the MATLAB page:

Running Python commands interactively in a Terminal application

Operating systems provide applications for interacting with the control of a computer through typed commands. On the Mac OS (and most flavors of Linux), there is an application called Terminal (as seen below). Within the Windows OS, there is an application called Command (or cmd or Command Prompt). Launching these programs provides access to running the python command. The python command provides an intearctive Python terminal in which code statements written in Python can be run one at a time. Below shows a terminal session whereby I typed in everything that has been highlighted in yellow (the white text are feedback output provided by the Python language). When I finish by typing the plot.show() command, a plot window appears with the identical plot that is generated when using a Jupyter Python notebook in class.

Running a Python Script from text file

Alternatively, Python commands can be batched in text files to create sequences of commands that do something useful. As an example, the figure below shows the same commands used in the interactive terminal, but as saved as lines in a text document. Python anticipates Python scripts to have an .py extention. Since I saved these commands in a file I named plot_1.py, I can then run all of them in sequence via a single python command:

python plot_1.py

The Python interpreter can run those commands and provide me the same plot as when typing them into the interpreter one line at a time. On a very busy analysis (of water quality and quantity predictions for climate scenarios for a watershed river basin), I typically have tens or even hundreds of Python script files saved with a .py extension and a coordinated file naming scheme that helps me remember when each script is useful for analysis needs.
Python syntax varies slightly depending on the programmer who scripted the services in each available package, but the base Python language syntax aims to be consistent as managed by a Python language committee. Python is generally considered to have become so popular because of the writability and consistency provided over many years.

Try and gain an instinct to how Python code is written so you can rely on that instinct to remove clutter from those things you have to memorize or refer to often in reference documents online. If you organize all the code you write or study into a library of code, you'll be able to reuse syntax structures for similar analysis tasks.