## Python Basics¶

In [3]:
1 + 2 * 3

Out[3]:
7
In [4]:
2**3

Out[4]:
8
In [9]:
x = "Machine" + "Learning"
print x

MachineLearning

In [10]:
x.upper()

Out[10]:
'MACHINELEARNING'
In [11]:
x.lower()

Out[11]:
'machinelearning'
In [15]:
len(x)

Out[15]:
15
In [16]:
a = 2 + 3
b = a * 4
c = b - 7
L = [a,b,c]
print L

[5, 20, 13]

In [19]:
M = [10, 2, "a", 3.5]
N = L + M
print N

[5, 20, 13, 10, 2, 'a', 3.5]

In [34]:
len(N)

Out[34]:
7
In [20]:
N[2]

Out[20]:
13
In [21]:
N[:3]

Out[21]:
[5, 20, 13]
In [22]:
N[3:]

Out[22]:
[10, 2, 'a', 3.5]
In [23]:
N[3:6]

Out[23]:
[10, 2, 'a']
In [24]:
# This is what a comment looks like
fruits = ['apples','oranges','pears','bananas']
for fruit in fruits:
print fruit + ' for sale'

fruitPrices = {'apples': 2.00, 'oranges': 1.50, 'pears': 1.75}
for fruit, price in fruitPrices.items():
if price < 2.00:
print '%s cost %f a pound' % (fruit, price)
else:
print fruit + ' are too expensive!'

apples for sale
oranges for sale
pears for sale
bananas for sale
pears cost 1.750000 a pound
apples are too expensive!
oranges cost 1.500000 a pound

In [25]:
# Example of python's list comprehension construction:

nums = [1,2,3,4,5,6]
plusOneNums = [x+1 for x in nums]
print "plusOneNums =", plusOneNums
oddNums = [x for x in nums if x % 2 == 1]
print "oddNums = ", oddNums
oddNumsPlusOne = [x+1 for x in nums if x % 2 ==1]
print "oddNumsPlusOn = ", oddNumsPlusOne

plusOneNums = [2, 3, 4, 5, 6, 7]
oddNums =  [1, 3, 5]
oddNumsPlusOn =  [2, 4, 6]

In [26]:
# Dictionaries

studentIds = {'knuth': 42.0, 'turing': 56.0, 'nash': 92.0 }
studentIds['turing']

Out[26]:
56.0
In [27]:
studentIds['ada'] = 97.0
studentIds

Out[27]:
{'ada': 97.0, 'knuth': 42.0, 'nash': 92.0, 'turing': 56.0}
In [28]:
del studentIds['knuth']
studentIds

Out[28]:
{'ada': 97.0, 'nash': 92.0, 'turing': 56.0}
In [29]:
studentIds['knuth'] = [42.0,'forty-two']
studentIds

Out[29]:
{'ada': 97.0, 'knuth': [42.0, 'forty-two'], 'nash': 92.0, 'turing': 56.0}
In [30]:
studentIds.keys()

Out[30]:
['knuth', 'nash', 'ada', 'turing']
In [31]:
studentIds.values()

Out[31]:
[[42.0, 'forty-two'], 92.0, 97.0, 56.0]
In [32]:
studentIds.items()

Out[32]:
[('knuth', [42.0, 'forty-two']),
('nash', 92.0),
('turing', 56.0)]
In [33]:
len(studentIds)

Out[33]:
4

## Numpy¶

In [35]:
import numpy as np

In [38]:
# Creating a 1-d array

A = np.array([1,2,3,4,5])
A

Out[38]:
array([1, 2, 3, 4, 5])
In [39]:
# Creating a 1-d array from an existing Python list

L = [1, 2, 3, 4, 5, 6]

A = np.array(L)
A

Out[39]:
array([1, 2, 3, 4, 5, 6])
In [40]:
A / 5

Out[40]:
array([0, 0, 0, 0, 1, 1])
In [43]:
np.array([1.0,2,3,4,5]) / 5

Out[43]:
array([ 0.2,  0.4,  0.6,  0.8,  1. ])
In [48]:
M = np.array([2, 3, 4, 5, 6, 7])

P = L * M
P

Out[48]:
array([ 2,  6, 12, 20, 30, 42])
In [49]:
P.sum()

Out[49]:
112
In [50]:
P.mean()

Out[50]:
18.666666666666668
In [47]:
# Dot product of two vectors

np.dot(L, M)

Out[47]:
112
In [53]:
# Creating a 2-d array

X = np.array([[1,2,3],[4,5,6],[7,8,9]])
X

Out[53]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [54]:
# Transpose a 2-d array

X.T

Out[54]:
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
In [55]:
# Array slicing

X[1,2]

Out[55]:
6
In [56]:
X[:,1]

Out[56]:
array([2, 5, 8])
In [57]:
X[0,:]

Out[57]:
array([1, 2, 3])
In [61]:
X[1:,1:]

Out[61]:
array([[5, 6],
[8, 9]])
In [63]:
X[1:,0:2]

Out[63]:
array([[4, 5],
[7, 8]])
In [65]:
# Using arrays to index into other arrays (e.g., Boolean masks)

A = np.array([1, 2, 3, 4, 5, 6])

A > 3

Out[65]:
array([False, False, False,  True,  True,  True], dtype=bool)
In [67]:
A[A > 3]

Out[67]:
array([4, 5, 6])
In [68]:
sum(A[A > 3])

Out[68]:
15
In [71]:
Ind = (A > 1) & (A < 6)
print Ind
A[Ind]

[False  True  True  True  True False]

Out[71]:
array([2, 3, 4, 5])
In [ ]: