Intro to Python for scientific work

method-skill
python
computing
Author

Dan Sandiford

Published

April 20, 2026

Open in Colab

Why Python for earthquake science?

  • Reproducible workflows
  • Fast numerical work
  • Access to ObsPy / ML tools
  • Simple plotting

Basic data types

integer = 3
floating = 3.14
text = "earthquake"
boolean = True

print(type(integer))
print(type(floating))
print(type(text))
print(type(boolean))

Containers

my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
my_dict = {"station": "AU.MEL", "magnitude": 3.2}

print(my_dict["station"])

Control flow

magnitude = 2.7

if magnitude >= 3:
    print("Locate event")
else:
    print("Probably noise")

Loops

stations = ["MEL", "OTW", "GIP"]

for s in stations:
    print("Processing", s)

NumPy

import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

x[:5]

Pandas

import pandas as pd

df = pd.DataFrame({
    "station": ["MEL", "OTW", "GIP"],
    "magnitude": [2.1, 3.0, 1.8]
})

df

Matplotlib

import matplotlib.pyplot as plt

plt.plot(x, y)
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.title("Synthetic waveform")
plt.show()

A tiny scientific task

magnitudes = np.array([1.2, 2.4, 3.1, 2.8])

large = magnitudes[magnitudes >= 2.5]

large

Discussion

  • Where will you use Python next?
  • What should the next session cover?