1 + 2 * 3
2**3
x = "Machine" + "Learning"
print(x)
x.upper()
x.lower()
len(x)
a = 2 + 3
b = a * 4
c = b - 7
L = [a,b,c]
print(L)
M = [10, 2, "a", 3.5]
N = L + M
print(N)
len(N)
N[2]
N[:3]
N[3:]
N[3:6]
# This is what a comment looks like
# Dictionaries contain name-value pairs
studentIds = {'knuth': 42.0, 'turing': 56.0, 'nash': 92.0 }
studentIds['turing']
studentIds['ada'] = 97.0
studentIds
del studentIds['knuth']
studentIds
studentIds['knuth'] = [42.0,'forty-two']
studentIds
studentIds.keys()
list(studentIds)
list(studentIds.values())
list(studentIds.items())
list(studentIds)
len(studentIds)
fruits = ['apples','oranges','pears','bananas']
for fruit in fruits:
print (fruit + ' for sale')
# Accessing dict element
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!')
# 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)
import numpy as np
# Creating a 1-d array
A = np.array([1,2,3,4,5])
A
# Creating a 1-d array from an existing Python list
L = [1, 2, 3, 4, 5, 6]
A = np.array(L)
A
A / 5
np.array([1.0,2,3,4,5]) / 5
M = np.array([2, 3, 4, 5, 6, 7])
P = L * M
P
P.sum()
P.mean()
# Dot product of two vectors
np.dot(L, M)
# Creating a 2-d array
A = np.array([[1,2,3],[4,5,6],[7,8,9],[8,7,6]])
A
A.shape
A[0,1]
A[2,1]
A[:,1]
A[0,:]
A[1:,1:]
A[1:3,1:]
A[1:,0:2]
A[-1,:]
# Transpose a 2-d array
print(A)
print()
print(A.T)
C = np.array([1,10,20])
print("A: \n", A)
print()
print("C: \n", C)
print()
print("A * C: \n", A * C)
print()
print("A dot C: \n", np.dot(A,C))
A = np.mat(A)
C = np.mat(C)
print(A * C)
print("C Transpose: \n", C.T)
print()
print("A * C: \n",A * C.T)
# Using arrays to index into other arrays (e.g., Boolean masks)
A = np.array([1, 2, 3, 4, 5, 6])
A > 3
A[A > 3]
sum(A[A > 3])
Ind = (A > 1) & (A < 6)
print(Ind)
A[Ind]