This presentation will give a brief into to some key features of Python and the Scientific Python ecosystem to help those not familar with the language with the remainder of the class. This is in no way a comprehensive introduction to either topic. Excellent tutorials on Python and Scientific Python can be found online.
We will be using IPython for this class which is a package which allows Python code to be run inside a browser. This is in no way the only way to run python, the Python/IPython shell, scripts and various IDEs can also be used but will not be coverted.
The notebook for this materials is available if you wish to follow along on your own computer, but we will be moving fast...
In [ ]:
a = 1
In [ ]:
a + 1
In [ ]:
b = 2.1
In [ ]:
b + 1
In [ ]:
a + b
In [ ]:
type(a + b)
In [ ]:
c = 1.5 + 0.5j # complex numbers
In [ ]:
print c.real
print c.imag
In [ ]:
d = 3 > 4
In [ ]:
print d
In [ ]:
type(d)
In [ ]:
s = "Hello everyone"
type(s)
In [ ]:
a = "Hello "
b = "World"
print a + b
In [ ]:
a = 1
print a
print type(a)
In [ ]:
b = float(a)
print b
print type(b)
In [ ]:
s = "1.23"
print s
print type(s)
In [ ]:
f = float(s)
print f
print type(f)
In [ ]:
l = ['red', 'blue', 'green', 'black', 'white']
In [ ]:
len(l)
In [ ]:
l
In [ ]:
print l[0]
print l[1]
print l[2]
In [ ]:
print l[-1] # last element
print l[-2]
In [ ]:
l[0] = 'orange'
print l
In [ ]:
print l[2:5]
In [ ]:
print l[2:-1]
In [ ]:
print l[1:6:2]
In [ ]:
l[::-1]
In [ ]:
ll = [5, 22.9, 14.8+1j, 'hello', [1,2,3]]
In [ ]:
ll
In [ ]:
print ll[0]
print ll[1]
print ll[2]
print ll[3]
print ll[4]
In [ ]:
d = {'name': 'Jonathan', 'id': 223984, 'location': 'USA'}
In [ ]:
d.keys()
In [ ]:
d.values()
In [ ]:
d['name']
In [ ]:
d['id']
In [ ]:
d['id'] = 1234
In [ ]:
d['id']
In [ ]:
t = ('red', 'blue', 'green')
In [ ]:
t[0]
In [ ]:
t[1:3]
In [ ]:
t[1] = 'orange'
In [ ]:
a = 10
if a == 10:
print "a is 10"
In [ ]:
a = 10
if a > 10:
print "a is larger than 10"
else:
print "a is less than 10... or maybe equal too"
In [ ]:
a = 4
if a > 10:
print "a is larger than 10"
elif a < 10:
print "a is less than 10"
else:
print "a is equal to 10"
In [ ]:
for i in range(10):
print i
In [ ]:
for color in ['red', 'blue', 'orange']:
print "My favorite color is", color
In [ ]:
def func():
print "Hello world"
In [ ]:
func()
In [ ]:
def func2(name):
print "Hello", name
In [ ]:
func2("Jonathan")
In [ ]:
def times2(x):
return x * 2
In [ ]:
y = times2(2)
print y
In [ ]:
def times_something(x, y=2):
print x*y
In [ ]:
times_something(3)
In [ ]:
times_something(3, 3)
In [ ]:
class Car(object):
engine = 'V4' # class attribute
def start(self): # class method
print "Starting the car with a", self.engine, "engine"
In [ ]:
mycar = Car()
In [ ]:
type(mycar)
In [ ]:
mycar.engine
In [ ]:
mycar.start()
In [ ]:
mycar.engine = 'V6'
In [ ]:
mycar.engine
In [ ]:
mycar.start()
In [ ]:
import numpy as np
In [ ]:
a = np.array([0, 1, 2, 3, 4, 5, 6, 7])
In [ ]:
a
In [ ]:
a.shape
In [ ]:
a.ndim
In [ ]:
a.dtype
In [ ]:
a[0::2]
In [ ]:
a[a>3]
In [ ]:
a * 2 + 100
In [ ]:
a.mean()
In [ ]:
b = np.arange(12).reshape(3,4)
In [ ]:
b.shape
In [ ]:
b
In [ ]:
b[1,2]
In [ ]:
b[0:2, ::-1]
In [ ]:
import scipy
In [ ]:
print scipy.__doc__
In [ ]:
%pylab inline
In [ ]:
plot([1,2,3])
In [ ]:
a = np.random.rand(30, 30)
imshow(a)
colorbar()