Skip to content
bradfordbenson edited this page Dec 28, 2022 · 7 revisions

Python

Introduction

Python is an interpreted, object oriented, high-level programming language. An interpreted programming language is different from a compiled language in a number of ways, but in practice it means that you need to define all of the classes and functions at the top of the code, before you use them. In compiled languages some things can appear out of order, because the compiler reads through the whole source code before making the compiled program. Interpreted languages are read line by line by the interpreter, so things cannot be out of order in the code.

Calling python

There are a number of ways of working with python, in particular:

interactively on the command line (shell mode) with python programs / scripts through jupyter notebooks through Google Colab notebooks

Shell mode

In shell mode, you simply type the command python or python3 into a terminal, which will print some details about the version of python you have installed, the date, and the commands for how to get help, credits, and licensing information. The terminal will show >>>, which means you can now type in python commands and the interpreter will execute those commands.

Python programs / script

The second way to use python is in script mode. You write a text file containing all of the python commands you want python to execute, in the order that you want them done. After you save this file, which is called a script, you type python into a terminal with your script filename as the first argument, and the python interpreter reads the script and executes it line by line. An example is the rdj2aau.py script that you can use to convert a list of RA, Dec, JD to azimuth, altitude, and UT date. In this case it takes two additional filenames for the input and output files:

python rdj2aau.py in.txt out.txt

Jupyter notebooks

Jupyter notebooks are a great way to keep code organized and documented. Jupyter can be used with several kernels, including python. Please see here for further instructions specific to this class.

Google Collab

Google Collab lets you run python code (in notebook format) on Google's computers. This means that there is no need for you to install python on your own computer. It also makes it easy to share your notebook with others through Google Drive and/or github, and thus to work collaboratively on code. Our experience shows that if you need a particular python package for your project, it is much easier to ``install'' it on Google Colab than your own computer or the Computing Lab.

Python tutorials for this class

General python tutorial Data file for python tutorial: test_data.txt

Python notebook on reading FITS files, numpy, plotting histograms. Fits files: 00000025.BIAS.FIT , 00000026.BIAS.FIT

Installing python on your laptop

Python is available for all common operating systems. The easiest way to install it is through anaconda. Anaconda includes all the packages needed for this lab, in particular numpy and astropy. Anaconda does take up significant disk space, so instead you could install miniconda along with the packages you need.

Other python resources

Excellent tutorials and resources are available elsewhere