## Python Basics¶

In :
1 + 2 * 3

Out:
7
In :
2**3

Out:
8
In :
x = "Machine" + "Learning"
print x

MachineLearning

In :
x.upper()

Out:
'MACHINELEARNING'
In :
x.lower()

Out:
'machinelearning'
In :
len(x)

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

[5, 20, 13]

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

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

In :
len(N)

Out:
7
In :
N

Out:
13
In :
N[:3]

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

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

Out:
[10, 2, 'a']
In :
# 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 :
# 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 :
# Dictionaries

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

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

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

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

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

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

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

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

Out:
4

## Numpy¶

In :
import numpy as np

In :
# Creating a 1-d array

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

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

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

A = np.array(L)
A

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

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

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

P = L * M
P

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

Out:
112
In :
P.mean()

Out:
18.666666666666668
In :
# Dot product of two vectors

np.dot(L, M)

Out:
112
In :
# Creating a 2-d array

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

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

X.T

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

X[1,2]

Out:
6
In :
X[:,1]

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

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

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

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

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

A > 3

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

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

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

[False  True  True  True  True False]

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