Introduction to Python for Planetary Sciences



Sebastian Walter

Planetologie-Seminar

July 9, 2020

#### Python overview - Interpreted, high-level general-purpose programming language - First released in 1991 - Emphasises code readability and simplicity by use of significant **whitespace** - Multiple programming paradigms: structured, **object-oriented** and functional - Comprehensive standard library, large collection of additional libraries* - Open and freely available https://www.python.org/ Note: The large amount of existing libraries is the main reason for its recent success in science. Plenty of modules available so you don't need to implement the methods yourself. And a major benefit is its free availablilty, so when you learn a programming language for example for your studies, you will be able to use it afterwards. So if you plan to start learning a programming language I strongly recommend to use a freely available one, and Python is a good candidate. Under this address you will find a download and after installation, you start the Python terminal

Syntax


  • No brackets to delimit blocks

  • Instead, whitespace indentation
  • (visual structure represents semantic structure)

>>> for x in range(0, 3):
>>>   x
0
1
2

Objects

  • In Python everything is an object

  • Objects can contain data and methods
  • >>> myList = [1,2,3]
    >>> myList.append(4)
    >>> myList
    [1, 2, 3, 4]
    

Indexing and slicing

  • Index operator []
>>> myList = ['a', 'b', 'c', 'd', 'e']

>>> myList[0]

'a'
  • A slice is a subset of list elements
>>> myList[1:3]

['b','c']
#### Important libraries / Modules - SciPy "ecosystem" - SciPy library - maths, science, engineering - Numpy - n-dimensional arrays and computing tools - Matplotlib - visualisation - IPython / Jupyter - RasterIO (GDAL)

Installation

Conda package and environment manager

(1) Download and install miniconda for your operating system https://docs.conda.io/en/latest/miniconda.html (Python 3.X)

(2) Start a conda terminal and install modules:

  • create a stand alone environment
  • conda create -n tutorial
  • activate the environment:
  • conda activate tutorial
  • install modules needed for this tutorial:
  • conda install -c conda-forge jupyter matplotlib scipy rasterio
  • start Jupyter:
  • jupyter notebook

Example code

Link to Spectrum plotting Gist

Link to Image processing Gist