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...
a = 1
a + 1
b = 2.1
b + 1
a + b
type(a + b)
c = 1.5 + 0.5j # complex numbers
print c.real
print c.imag
d = 3 > 4
print d
type(d)
s = "Hello everyone"
type(s)
a = "Hello "
b = "World"
print a + b
a = 1
print a
print type(a)
b = float(a)
print b
print type(b)
s = "1.23"
print s
print type(s)
f = float(s)
print f
print type(f)
l = ['red', 'blue', 'green', 'black', 'white']
len(l)
l
print l[0]
print l[1]
print l[2]
print l[-1] # last element
print l[-2]
l[0] = 'orange'
print l
print l[2:5]
print l[2:-1]
print l[1:6:2]
l[::-1]
ll = [5, 22.9, 14.8+1j, 'hello', [1,2,3]]
ll
print ll[0]
print ll[1]
print ll[2]
print ll[3]
print ll[4]
d = {'name': 'Jonathan', 'id': 223984, 'location': 'USA'}
d.keys()
d.values()
d['name']
d['id']
d['id'] = 1234
d['id']
t = ('red', 'blue', 'green')
t[0]
t[1:3]
t[1] = 'orange'
a = 10
if a == 10:
print "a is 10"
a = 10
if a > 10:
print "a is larger than 10"
else:
print "a is less than 10... or maybe equal too"
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"
for i in range(10):
print i
for color in ['red', 'blue', 'orange']:
print "My favorite color is", color
def func():
print "Hello world"
func()
def func2(name):
print "Hello", name
func2("Jonathan")
def times2(x):
return x * 2
y = times2(2)
print y
def times_something(x, y=2):
print x*y
times_something(3)
times_something(3, 3)
class Car(object):
engine = 'V4' # class attribute
def start(self): # class method
print "Starting the car with a", self.engine, "engine"
mycar = Car()
type(mycar)
mycar.engine
mycar.start()
mycar.engine = 'V6'
mycar.engine
mycar.start()
import numpy as np
a = np.array([0, 1, 2, 3, 4, 5, 6, 7])
a
a.shape
a.ndim
a.dtype
a[0::2]
a[a>3]
a * 2 + 100
a.mean()
b = np.arange(12).reshape(3,4)
b.shape
b
b[1,2]
b[0:2, ::-1]
import scipy
print scipy.__doc__
%pylab inline
plot([1,2,3])
a = np.random.rand(30, 30)
imshow(a)
colorbar()