Follow the instructions below to get the Anaconda distribution of Python installed on your computer. Even if you already have a version of Python installed on your machine, we encourage you to go through this installation process. If you already specifically have Anaconda installed, we encourage you to update all of the Python packages (you may need to look up how to do this).
Go to the Anaconda Download webpage: https://www.anaconda.com/download/ Then click the "Download" button under "Your Data Science Toolkit" to jump to the bottom of the page.
Select the appropriate operating system (Windows | macOS | Linux) for your computer (it may auto-detect the correct operating system).
Download the Python 3.8 version (64 bit recommended).
Follow the online documentation to install Python for your specific operating system: https://docs.anaconda.com/anaconda/install/
Open the command line program on your computer.
On Windows, type CMD in the run box in the Start menu.
On Mac, type “terminal” in the spotlight search and run the “Terminal” application
On Linux, open up the “Console” application
Type “jupyter notebook” in the command line and hit enter.
If everything goes correctly, a browser window should open up with the Jupyter interface running.
Both the lectures and laboratories will be offered via Jupyter notebooks. This allows us to intersperse comments, links and documentation (in Markdown cells) with executable code written in Python. You can interact with these notebooks either:
by launching on your own machine (jupyter notebook)
by launching on Google Colab: https://colab.research.google.com/
(for MSU personnel only) by launching OnDemand in the HPCC-ICER: https://ondemand.hpcc.msu.edu/
(for MSU personnel only) by launching JupyterHub in the EGR servers: https://jupyterhub.egr.msu.edu/
A great thing about the Python language is that we can make use of a whole world of packages for numerical analysis, data visualization, statistics and machine learning. There are also a number of great packages for machine learning, data analysis, performing and analyzing molecular dynamics simulations, quantum simulations, and even visualize structures directly in Jupyter notebooks! This truly lets us stand on the shoulders of giants.
Say we want to solve the linear equation:
Ax=B
where A and B are known matrices and x is unknown. We can do this by importing a particular package from scipy:
from scipy.linalg import solve
Note that if you see a message like: ModuleNotFoundError: No module named 'scipy' then this indicates scipy has not been installed or is not accessible to this Python kernel.
To install a new package you should use a package manager such as pip or conda using the command line. To make these new packages accessible you should shutdown and restart the server running the Jupyter notebook.
However, there is a shortcut: in Jupyter notebooks (and on Google Colab) you can run command line commands without leaving the notebook:
[ ]:
# no need to run this if you already have scipy
!pip install scipy
Back to our linear equation example. Now that we imported the solve function, what if we forget how to format the inputs? Or we want to know which options are available when we call it? This information is in the documentation for this function, also known as the "doc string". Documentation is of course available on the scipy webpage (found here), but again there is a shortcut:
solve?
def my_function(a):
"""This function returns the square of a number."""
return a**2
my_function?
What if we want to see the source code? Well you could look at the bottom of the docstring to find the location of the file on your computer, open the file in a text editor, and search for the corresponding function name. But as you probably guessed, there is an easier way! Just add another question mark:
solve??
Scrolling down past the call signature and the docstring shows the source code of this function.