We'll start with some basics in the interpreter, covering strings and maths, then come back to this notebook.
Indexing, slicing, append()
, in
layers = [0.23, ] # Add some more
# Play with lists
# e.g. indexing with layers[4]
# slicing with layers[2:4]
uppers = layers[:-1]
lowers = layers# how shall we slice it?
rcs = []
for pair in zip(uppers, lowers):
rc = # Compute the contrast
# Now what?
rcs
Definition, inputs, side-effects, returning, scope, docstrings
def add(a, b):
return a + b
# Exercise
def compute_rc(layers):
"""
Computes reflection coefficients given
a list of layer impedances.
"""
# Put code here
# This should give you what you got before
compute_rc(layers)
Now we will put this function in a file and import into a new notebook.
<something>.py
in your current working directoryRun this:
layers = [0.23, 0.34, 0.45, 0.25, 0.23, 0.35]
compute_rc(layers)
Why didn't that work?
Change the name here to the name of your file:
import something
something.compute_rc(layers)
import numpy as np # Just like importing file
biglog = np.random.random(1000)
%timeit compute_rc(biglog)
We need to learn the basics of NumPy arrays.
layers + 1
layers = np.array(layers)
layers + 1
Etc.
# Exercise
def compute_rc_vector(layers):
# rewrite using NumPy arrays
%timeit compute_rc_vector(biglog)
Warning signs:
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(biglog)
fig = plt.figure(figsize=(15,2))
ax = fig.add_subplot(111)
ax.plot(biglog)
ax.set_title("big log")
plt.show()
url = "http://en.wikipedia.org/wiki/Jurassic"
Use View Source in your browser to figure out where the age range is on the page, and what it looks like.
Try to find the same string here:
import requests
r = requests.get(url)
r.text
import re
# Use regex to extract the age range
# Make a function to get the start and end data of any geologic period