def fibonacci(N):
sequence = [0,1]
for i in range(N-1):
sequence.append(sequence[-2] + sequence[-1])
return sequence
fibonacci(10)
import matplotlib.pyplot as plt
import numpy as np
xlist=[1,2,5,9]
ylist = []
for x in xlist:
ylist.append(x**2)
plt.plot(xlist,ylist)
plt.show()
xarr = np.array([1,2,5,7,9])
yarr = xarr**2
plt.plot(xarr,yarr)
plt.show()
xlist = [1,2,5,7,9]
ylist = xlist**2
plt.plot(xlist,ylist)
plt.show()
xlist[3]
xarr[3]
mylist = [2,3.5,True,"Cat"]
mybadarr = np.array([2,3.5,True,"Cat",(2,5)])
mybadarr
numarr = np.array([1,3,4,5,7])
numarr[0] = 0.75
numarr
int(0.75)
floatarr = np.array([1,2,3,5],dtype=float)
floatarr
floatarr[0]= 0.75
print(floatarr)
[1,2,3]-[1,2,3]
np.array([1,2,3])-np.array([1,1,1])
np.array([1,2,3])+np.array([1,1,1])
np.array([1,2,3])*np.array([1,1,1])
np.array([1,2,3])**np.array([1,2,1])
np.array([1,2,3])-np.array([1,1])
x = np.array([-2,-1,0,1,2,3,4,5])
y = 0.25*x**4 +2/3*x**3-5*x**2+2*x+3
plt.plot(x,y)
plt.show()
xsmooth = np.arange(-5,5,0.5)
print(xsmooth)
xsmooth = np.arange(-5,5,0.01)
ysmooth = 0.25 *xsmooth**4 + 2/3*xsmooth**3-5*xsmooth**2+2*xsmooth+3
plt.plot(xsmooth,ysmooth)
plt.show()
np.arange(2,8,1)
np.zeros(4)
np.ones(5)
np.ones(6,dtype=int)
longarr=np.arange(0,100,2.5)
longarr[5]
longarr[5:10]
longarr[5:20:2]
longarr[:20:2]
longarr[10::2]
longarr[2:9:3] = 1
print(longarr)
longarr[1:10:3] = [100,200,300]
print(longarr)
arr = np.arange(1,10,1)
print(arr)
np.roll(arr,2)
np.roll(arr,-2)
TwoDArr = np.array([[1,2,3],
[4,5,6],
[1,1,2]])
print(TwoDArr)
TwoDArr[1]
TwoDArr = np.array([[1,2,3],
[4,5,6],
[1,1,2,7]])
TwoDArr = np.array([[1,2,3],
[4,5,6],
[1,1,2]])
TwoDArr[0,1]
BigArr = np.array([[1,2,3,4,5,6,7,8],
[9,1,2,3,4,5,2,5],
[2,2,2,2,2,2,2,2],
[3,4,3,4,3,4,3,4],])
BigArr[1,3::2]
BigArr[:2,3::2]
np.zeros((2,3))
np.zeros(2,3)
arr = np.arange(1,37,1)
arr.reshape((4,9))
BigArr.shape
BigArr
(nrow,ncol) = BigArr.shape
mysum = 0
for r in range(nrow):
for c in range(ncol):
mysum = mysum + BigArr[r,c]
BigArr.T
ListSqrs(n)
that creates a numpy array of the square numbers between 0 and $n^2$, do this in as few lines as possible.MyIdent(n)
that retrns a numpy array that looks like an $n\times n$ identity matrix.
$$ MyIdent(3)= \begin{bmatrix}
1& 0& 0\\
0& 1& 0\\
0& 0& 1
\end{bmatrix}$$SumEvenIdx(Arr)
that takes in a 2d array and returns the sum of the entries in the arrays whose indecies add up to an even number