%pylab inline a=[1,2,3] a=a+1 for k in range(3): a[k]=a[k]+1 from numpy import * a=array([1,2,3,4,5,6]) print(a) a=array([2.0,4.0,8.0,16.0]) b=array([0,1+0j,1+1j,2-2j]) c=array(['a','b','c','d']) print(a) print(b) print(c) output=[n for n in range(10)] print(output) output=[] for n in range(10): output.append(n) print(output) array(output) array([n for n in range(10)]) array([2.0*k**0.563 for k in range(0,10,2)]) zeros(5) ones(10) arange(5) arange(0,10,2) linspace(0,10,20) #makes an array of 20 points linearly spaced from 0 to 10 linspace(-5,5,15) #15 points in range from -5 to 5 array([1,2,3.14]) # [int,int,float] -> [float,float,float] array([1.0,1+1j,'hello']) #array data is upcast to strings array([1,2,3,4,5],dtype=float) arange(2,10,2,dtype=complex) array([k for k in range(10)],dtype=str) a=array([1,2,3,4]) 5.0*a #This gets upcasted because 5.0 is a float 5*a**2-4 x=linspace(-pi,pi,10) sin(x) x=array([x**2 for x in range(4)]) sqrt(x) x=array([2*n+1 for n in range(10)]) sum(x) #sums up all elements in the array a=array([0,-1,2,-3,4]) print(a<0) a=arange(10) print((mod(a,2)!=0)) a=arange(20) a[3::3] a[3::3]=-1 print(a) a=arange(10) a[::-1] a=linspace(-10,10,20) print(a[a<=-5]) print(a[-8N numbers = [] for i in range(2,N+1): # This can be replaced by array numbers.append(i) # Run Seive of Eratosthenes algorithm marking nodes with -1 for j in range(N-1): if numbers[j]!=-1: p=numbers[j] for k in range(j+p,N-1,p): # This can be replaced by array numbers[k]=-1 # Collect all elements not -1 (these are the primes) primes = [] for i in range(N-1): # This can be replaced by array if numbers[i]!=-1: primes.append(numbers[i]) print(primes) N=20 # generate a list from 2->N numbers=arange(2,N+1) # replaced for-loop with call to arange # Run Seive of Eratosthenes algorithm # by marking nodes with -1 for j in range(N-1): if numbers[j]!=-1: p=numbers[j] numbers[j+p:N-1:p]=-1 # replaced for-loop by slicing array # Collect all elements not -1 (these are the primes) primes=numbers[numbers!=-1] # Used conditional statement to get elements !=-1 print(primes) from IPython.core.display import HTML def css_styling(): styles = open("styles/style.css", "r").read() return HTML(styles) css_styling()