What you are looking at is a Jupyter Notebook, a web-based interactive computation enviroment well suited for creating and sharing examples of scientific Python computations.
One of the key features of Jupyter notebooks is that Python code can be written and executed in cells.
Click on the next cell and press shift+enter to execute the Python code.
In [131]:
# This is a Python comment
# the next line is a line of Python code
print("Hello World!")
Cells can also be used to create textual materials using the markup language Markdown.
Double click on this cell (or any others) to see the raw markdown which produces the nicely formatted text.
One of the reasons these Notebooks are so well suited for scientific Python work is that Juypter is well integrated into the Scientific Python ecosystem. For example, plots can be included in notebooks for visualizing data. Execute the next two cells (using ctrl+enter)
In [132]:
# These two lines turn on inline plotting
%matplotlib inline
import matplotlib.pyplot as plt
In [133]:
plt.plot([1,2,3])
Out[133]:
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 covered.
The notebook for this materials is available if you wish to follow along on your own computer, but we will be moving fast...
In [134]:
a = 1
In [135]:
a + 1
Out[135]:
In [136]:
b = 2.1
In [137]:
b + 1
Out[137]:
In [138]:
a + b
Out[138]:
In [139]:
type(a + b)
Out[139]:
In [140]:
c = 1.5 + 0.5j # complex numbers
In [141]:
print(c.real)
print(c.imag)
In [142]:
d = 3 > 4
In [143]:
print(d)
In [144]:
type(d)
Out[144]:
In [145]:
s = "Hello everyone"
type(s)
Out[145]:
In [146]:
a = "Hello "
b = "World"
print(a + b)
In [147]:
a = 1
print(a)
print(type(a))
In [148]:
b = float(a)
print(b)
print(type(b))
In [149]:
s = "1.23"
print(s)
print(type(s))
In [150]:
f = float(s)
print(f)
print(type(f))
In [151]:
l = ['red', 'blue', 'green', 'black', 'white']
In [152]:
len(l)
Out[152]:
In [153]:
l
Out[153]:
In [154]:
print(l[0])
print(l[1])
print(l[2])
In [155]:
print(l[-1]) # last element
print(l[-2])
In [156]:
l[0] = 'orange'
print(l)
In [157]:
print(l[2:5])
In [158]:
print(l[2:-1])
In [159]:
print(l[1:6:2])
In [160]:
l[::-1]
Out[160]:
In [161]:
ll = [5, 22.9, 14.8+1j, 'hello', [1,2,3]]
In [162]:
ll
Out[162]:
In [163]:
print(ll[0])
print(ll[1])
print(ll[2])
print(ll[3])
print(ll[4])
In [164]:
d = {'name': 'Bobby', 'id': 223984, 'location': 'USA'}
In [165]:
d.keys()
Out[165]:
In [166]:
d.values()
Out[166]:
In [167]:
d['name']
Out[167]:
In [168]:
d['id']
Out[168]:
In [169]:
d['id'] = 1234
In [170]:
d['id']
Out[170]:
In [171]:
t = ('red', 'blue', 'green')
In [172]:
t[0]
Out[172]:
In [173]:
t[1:3]
Out[173]:
In [176]:
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 [177]:
for i in range(10):
print(i)
In [179]:
def func():
print("Hello world")
In [180]:
func()
In [188]:
class Car(object):
engine = 'V4' # class attribute
def start(self): # class method
print("Starting the car with a", self.engine, "engine")
In [189]:
mycar = Car()
In [190]:
type(mycar)
Out[190]:
In [191]:
mycar.engine
Out[191]:
In [192]:
mycar.start()
In [193]:
mycar.engine = 'V6'
In [194]:
mycar.engine
Out[194]:
In [195]:
mycar.start()
In [196]:
import numpy as np
In [197]:
a = np.array([0, 1, 2, 3, 4, 5, 6, 7])
In [198]:
a
Out[198]:
In [199]:
a.shape
Out[199]:
In [200]:
a.ndim
Out[200]:
In [201]:
a.dtype
Out[201]:
In [202]:
a[0::2]
Out[202]:
In [203]:
a[a>3]
Out[203]:
In [204]:
a * 2 + 100
Out[204]:
In [205]:
a.mean()
Out[205]:
In [206]:
b = np.arange(12).reshape(3,4)
In [207]:
b.shape
Out[207]:
In [208]:
b
Out[208]:
In [209]:
b[1,2]
Out[209]:
In [210]:
b[0:2, ::-1]
Out[210]:
In [211]:
import scipy
In [213]:
print(scipy.__doc__)
In [214]:
%pylab inline
In [215]:
plot([1,2,3])
Out[215]:
In [216]:
a = np.random.rand(30, 30)
imshow(a)
colorbar()
Out[216]:
In [ ]: