Python is a high-level, interpreted, open-source programming language that emphasizes readability of source code. In this notebook some of the features of the language as well as key modules in the the Scientific Python ecosystem will be introduced.
Aside using the python shell, Python scrips, and IPython with the "Hello world" example.
In [1]:
a = 1 # integers
In [2]:
a + 1
Out[2]:
In [3]:
b = 2.1 # float
type(b)
Out[3]:
In [4]:
c = 1.5 + 0.5j # complex numbers
print c.real
print c.imag
type(c)
Out[4]:
In [5]:
d = 3 > 4 # booleans
print d
type(d)
Out[5]:
In [6]:
a = 1
b = 2.5
a + b
Out[6]:
In [7]:
# types can be cast
a = 1
print a
print float(a)
In [8]:
s = "Hello everyone" # strings
type(s)
Out[8]:
In [9]:
# lists
l = ['red', 'blue', 'green', 'black', 'white']
type(l)
Out[9]:
In [10]:
len(l)
Out[10]:
In [11]:
print l[0]
print l[1]
print l[2]
In [12]:
print l[-1] # last element
print l[-2]
In [13]:
print l[2:5]
In [14]:
print l[2:-1]
In [16]:
l
Out[16]:
In [15]:
print l[1:6:2]
In [17]:
l[::-1]
Out[17]:
In [18]:
l
Out[18]:
In [19]:
l[0] = 'orange'
print l
In [20]:
ll = [5, 22.9, 14.8+1j, 'hello', [1,2,3]]
In [21]:
ll
Out[21]:
In [22]:
print ll[0]
print ll[1]
print ll[2]
print ll[3]
print ll[4]
In [23]:
d = {'name': 'Jonathan', 'id': 223984, 'location': 'USA'}
In [24]:
d.keys()
Out[24]:
In [25]:
d.values()
Out[25]:
In [26]:
d['name']
Out[26]:
In [27]:
d['id']
Out[27]:
In [28]:
d['id'] = 1234
In [29]:
d['id']
Out[29]:
In [30]:
t = ('red', 'blue', 'green')
In [31]:
t[0]
Out[31]:
In [32]:
t[1:3]
Out[32]:
In [33]:
t[1] = 'orange'
In [34]:
a = 10
if a == 10:
print "a is 10"
# if, elif, else
In [38]:
a = 10
if a > 10:
print "a is larger than 10"
else:
print "a is less than 10... or maybe equal too"
In [41]:
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 [42]:
for i in range(10):
print i
In [43]:
for color in ['red', 'blue', 'orange']:
print "My favorite color is", color
In [44]:
# list comprehensions
[i*2 for i in range(10)]
Out[44]:
In [45]:
def func():
print "Hello world"
In [46]:
func()
In [47]:
def func2(name):
print "Hello", name
In [48]:
func2("Jonathan")
In [49]:
def times2(x):
return x * 2
In [50]:
y = times2(2)
print y
In [51]:
def times_something(x, y=2):
print x*y
In [52]:
times_something(3)
In [53]:
times_something(3, 3)
In [54]:
w = times_something
In [55]:
w(3, 3)
In [56]:
f_list = [times_something, times2]
In [57]:
f_list[1](9)
Out[57]:
In [58]:
class Car(object):
engine = 'V4' # class attribute
def start(self): # class method
print "Starting the car with a", self.engine, "engine"
In [ ]:
class SuperCar(Car):
In [59]:
mycar = Car()
In [60]:
type(mycar)
Out[60]:
In [61]:
mycar.engine
Out[61]:
In [64]:
mycar.start()
In [65]:
mycar.engine = 'V6'
In [66]:
mycar.engine
Out[66]:
In [67]:
mycar.start()
In [68]:
import numpy as np
In [76]:
a = np.array([0, 1, 2, 3, 4, 5, 6, 7])
In [71]:
a.shape
Out[71]:
In [72]:
a.ndim
Out[72]:
In [73]:
a.dtype
Out[73]:
In [78]:
a[0::2]
Out[78]:
In [80]:
a[a>3]
Out[80]:
In [83]:
a * 2 + 100
Out[83]:
In [86]:
a.
Out[86]:
In [87]:
b = np.arange(12).reshape(3,4)
In [88]:
b.shape
Out[88]:
In [ ]:
b
In [ ]:
b[1,2]
In [ ]:
b[0:2, ::-1]
In [92]:
%pylab inline
In [93]:
plot([1,2,3])
Out[93]:
In [94]:
a = np.random.rand(30, 30)
imshow(a)
colorbar()
Out[94]:
In [97]:
import matplotlib.pyplot as plt
In [ ]: