Question 1
from math import sin, cos
x = pi /4
1_val = sin^2( x ) + cos^2( x )
print 1_VAL
# Fix 1: pi needs to be imported from the math module before it can be used in the line below.
from math import sin, cos, pi
x = pi /4
# Fix 2: 1_val is not a valid variable name - it cannot start with a number
# Fix 3: raising something to a power is done using **, not ^
# Fix 4: when calling sin or cos, it's name must be followed by the arguments in brackets. If we want to represent $\sin^2(x)$
# in Python, we have to write it like $\sin(x)^2$, i.e. sin(x)**2.
val = sin(x)**2 + cos(x)**2
# Fix 5: variable names are case-sensitive in Python. val is not the same as VAL.
print val
1.0
Question 2
n = 101 # The number of values
start = -1.0
end = 1.0
dx = (end-start)/(n-1) # The distance between each value
values = [start + i*dx for i in range(n)]
print values
from numpy import linspace
values_numpy = linspace(start, end, n)
for v in values_numpy:
print "%.2f" % (v)
[-1.0, -0.98, -0.96, -0.94, -0.92, -0.9, -0.88, -0.86, -0.84, -0.8200000000000001, -0.8, -0.78, -0.76, -0.74, -0.72, -0.7, -0.6799999999999999, -0.6599999999999999, -0.64, -0.62, -0.6, -0.5800000000000001, -0.56, -0.54, -0.52, -0.5, -0.48, -0.45999999999999996, -0.43999999999999995, -0.42000000000000004, -0.4, -0.38, -0.36, -0.33999999999999997, -0.31999999999999995, -0.29999999999999993, -0.28, -0.26, -0.24, -0.21999999999999997, -0.19999999999999996, -0.17999999999999994, -0.16000000000000003, -0.14, -0.12, -0.09999999999999998, -0.07999999999999996, -0.05999999999999994, -0.040000000000000036, -0.020000000000000018, 0.0, 0.020000000000000018, 0.040000000000000036, 0.06000000000000005, 0.08000000000000007, 0.10000000000000009, 0.1200000000000001, 0.14000000000000012, 0.15999999999999992, 0.17999999999999994, 0.19999999999999996, 0.21999999999999997, 0.24, 0.26, 0.28, 0.30000000000000004, 0.32000000000000006, 0.3400000000000001, 0.3600000000000001, 0.3800000000000001, 0.40000000000000013, 0.41999999999999993, 0.43999999999999995, 0.45999999999999996, 0.48, 0.5, 0.52, 0.54, 0.56, 0.5800000000000001, 0.6000000000000001, 0.6200000000000001, 0.6400000000000001, 0.6600000000000001, 0.6799999999999999, 0.7, 0.72, 0.74, 0.76, 0.78, 0.8, 0.8200000000000001, 0.8400000000000001, 0.8600000000000001, 0.8800000000000001, 0.9000000000000001, 0.9199999999999999, 0.94, 0.96, 0.98, 1.0] -1.00 -0.98 -0.96 -0.94 -0.92 -0.90 -0.88 -0.86 -0.84 -0.82 -0.80 -0.78 -0.76 -0.74 -0.72 -0.70 -0.68 -0.66 -0.64 -0.62 -0.60 -0.58 -0.56 -0.54 -0.52 -0.50 -0.48 -0.46 -0.44 -0.42 -0.40 -0.38 -0.36 -0.34 -0.32 -0.30 -0.28 -0.26 -0.24 -0.22 -0.20 -0.18 -0.16 -0.14 -0.12 -0.10 -0.08 -0.06 -0.04 -0.02 0.00 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 0.22 0.24 0.26 0.28 0.30 0.32 0.34 0.36 0.38 0.40 0.42 0.44 0.46 0.48 0.50 0.52 0.54 0.56 0.58 0.60 0.62 0.64 0.66 0.68 0.70 0.72 0.74 0.76 0.78 0.80 0.82 0.84 0.86 0.88 0.90 0.92 0.94 0.96 0.98 1.00
Question 3
from numpy import *
from pylab import *
%pylab inline
def f(x, t):
return exp(-(x-3*t)**2)*sin(3*pi*(x-t))
n = 1000
xarray = linspace(-3, 10, n)
for t in [0, 1, 2]:
plot(xarray, f(xarray, t), label=r"$t$ = %.1f" % (t))
xlabel(r"$x$")
ylabel(r"$f(x,t)$")
legend(loc="best")
show()
Populating the interactive namespace from numpy and matplotlib
/usr/local/lib/python2.7/site-packages/IPython/core/magics/pylab.py:161: UserWarning: pylab import has clobbered these variables: ['linalg', 'draw_if_interactive', 'random', 'power', 'fft', 'info'] `%matplotlib` prevents importing * from pylab and numpy "\n`%matplotlib` prevents importing * from pylab and numpy"
Question 4
from numpy import *
f = open("seismic.dat", "r")
magnitude = []
for line in f:
s = line.split()
magnitude.append(float(s[-1])) # The last column is the "magnitude"
magnitude = array(magnitude)
f.close()
print magnitude
count = 0
for m in magnitude:
if m > 6.0:
count += 1
print "There are %d earthquakes above magnitude 6.0" % (count)
[ 6.6 6.1 5.2 5. 5.1 7.3 5.2 5.4 5.4 5.7 5.2 5.6 7.6 5.7 5.4 5.4 5.5 5.2 6.1 6.1 6.2 6. 5.2 5.6 5.1 5.5 5.2 5.2 6.7 5.5 6.5 5.6 6. 5.7 6.3 5.5 5.5 5.6 6.4 5.2 5.5 5.4 5.7 5.8 5.3 5.4 5.6 5.3 5.6 5.5 5.7 5.9 5.1 5.7 7. 6.4 5.6 5.2 6.1 6.7 5.9 5.4 5.9 5.6 5.8 6.1 5. 5.3 5.4 5.2 5.2 5.9 5.4 5.2 5.7 6.1 5.6 5.3 5.3 6.1 5.4 5.3 5.5 5.4 5.5 5.8 5.4 5.1 5.3 5.2 7.2 5.4 5.5 5. 5. 5.6 6.4 5.2 7.1 5.2 5.1 5.3 5.1 5.4 5.2 4.9 5.4 5. 5.1 5.4 5.1 5.2 5.3 5. 5.1 7.5 6.6 5.1 5.1 5.1 5.3 5.5 6.8 6.1 5. 5.3 5.3 6.6 5.6 5.9 5.2 5.4 5.3 5.1 5.1 5.1 4.9 7.8 5.4 5.1 5.8 5.2 5.1 5.5 5.3 5. 6.1 5. 5.2 5.1 5.3 5.6 5.3 5. 5.2 7. 4.8 4.9 5. 5. 5. 5.6 6.1 5.5 5.6 4.9 5. 5. 5.1 5.2 6. 4.9 5.2 5.6 5.5 4.8 5.2 5.7 5.4 5.8 4.9 5.2 4.9 6. 5.2 5.4 4.9 5.1 6.3 5. 5. 5.7 4.9 6.4 5.5 5.7 6.5 5.1 5. 6.6 5.6 5.9 5.9 5.4 5.2 5. 5.7] There are 34 earthquakes above magnitude 6.0
Question 5 The file human_evolution.txt holds information about various human species and their hight, weight, and brain volume.
f = open("human_evolution.txt", "r")
humans = {}
for line in f:
# Ignore lines that don't start with "H."
if(line[0:2] != "H."):
continue
# Get each column and strip out leading and trailing whitespace
name = line[0:21].strip()
lived = line[21:37].strip()
height = line[37:50].strip()
mass = line[50:62].strip()
brain_volume = line[62:].strip()
# Add to dictionary
humans[name] = [lived, height, mass, brain_volume]
f.close()
# Test
print humans["H. habilis"]
print "%25s %25s %25s %25s %25s" % ("Species", "Lived when (M. years)", "Adult height (m)", "Adult mass (kg)", "Brain volume (cm**3)")
for human in humans:
data = humans[human]
print "%25s %25s %25s %25s %25s" % (human, data[0], data[1], data[2], data[3])
['2.2 - 1.6', '1.0 - 1.5', '33 - 55', '660'] Species Lived when (M. years) Adult height (m) Adult mass (kg) Brain volume (cm**3) H. neanderthalensis 0.35 - 0.03 1.6 55 - 70 1200 - 1700 H. sapiens sapiens 0.2 - present 1.4 - 1.9 50 - 100 1000 - 1850 H. heidelbergensis 0.6 - 0.35 1.8 60 1100 - 1400 H. erectus 1.4 - 0.2 1.8 60 850 (early) - 1100 (late) H. floresiensis 0.10 - 0.012 1.0 25 400 H. ergaster 1.9 - 1.4 1.9 700 - 850 H. habilis 2.2 - 1.6 1.0 - 1.5 33 - 55 660