%pylab inline 10+5 10-157 4/3 (50-4)*10/5 4**4 4**4.0 7+0.000000000000001 7+0.0000000000000001 0.1+0.2 from numpy import * exp(2.34) sqrt(5) sinc(0.5) radius=5 area=pi*radius**2 area x=10 x=(x**2+25)/10 x weight _freq = 8 Oscillator_Energy = 10 _freq*Oscillator_Energy speed_of_light = 2.9979*10**8 spring_constant = sqrt(2/5) 'Hello Class' "Hello Class" "How was Hwajung's birthday party?" a = "I like " # There is a blank space at the end of this string. b = "chicken and HOF" a+b temp=23 text="The temperature right now is" print(text,temp) shopping_list=['eggs', 'bread', 'milk', 'bananas'] shopping_list[2] shopping_list[0] shopping_list[-1] shopping_list[-2] len(shopping_list) shopping_list.append('apples') shopping_list shopping_list.remove('bread') shopping_list various_things=[1, "hello", -1.234, [-1, -2, -3]] various_things various_things[0] various_things[-1] various_things[3][1] items=['four calling birds', 'three french hens', 'two turtle doves', 'a partridge in a pear tree'] for thing in items: print(thing) for variable in items: print(variable) for variable in items: print(variable) for variable in items: print("My true love gave to me", variable) shopping_list=['eggs', 'bread', 'milk', 'bananas', 'apples'] shopping_list[0] shopping_list[0:3] shopping_list[-2:] shopping_list[0::2] a=5 ; b=8 a>b c=0 c<=0,c>=0 a=5;b=6 a==b,a!=b True==1,False==0 a=-1;b=4;c=10;d=11 a=[4,5,7] [4,5,6]<=[4,5,7] 'today'=='Today' today='friday' if today=='friday': print('We have class today :(') # this is a code block else: print('No class today :)') # this is also a code block today='thursday' if today=='friday': print('We have class today :(') elif today=='thursday': print('Our assignment is due today :(') else: print('No class today :)') n=0 while n<=10: #evaluate code block until n>10 print('The current value of n is:',n) n=n+1 #increase the value of n by 1 for n in [1,2,3,4,5,6,7,8,9,10]: if remainder(n,2)==0: print(n,'is even') else: print(n,'is odd') for n in range(1,11): if remainder(n,2)==0: print(n,'is even') else: print(n,'is odd') for n in range(0,11,2): print(n) n = 10 fib = [0,1] for i in range(2,n): fib.append(fib[i-1]+fib[i-2]) print(fib) n = 2 fib = [0,1] while n<10: fib.append(fib[n-1]+fib[n-2]) n = n+1 print(fib) # This is an example script for the P461 class # Here we will calculate the series expansion # for sin(x) up to an arbitrary order N. # # Paul Nation, 02/03/2014 from numpy import * from scipy.misc import factorial N = 5 # The order of the series expansion x = pi/4. # The point at which we want to evaluate sine ans = 0.0 for k in range(N+1): ans = ans+(-1)**k*x**(1+2*k)/factorial(1+2*k) print("Series approximation:",ans) print("Error:",sin(x)-ans) from numpy import * from scipy.misc import factorial N=5 # The order of the series expansion x=pi/4. # The point at which we want to evaluate sine def sine_series(x,N): ans=0.0 for k in range(N+1): ans=ans+(-1)**k*x**(1+2*k)/factorial(1+2*k) return ans result = sine_series(x,N) print("Series approximation:",result) print("Error:",sin(x)-result) def function_name(arg1,arg2): "Block of code to run" "..." return result from numpy.random import random from pylab import * N=100 # Number of points to generate def random_coordinates(N): x_coords=[] y_coords=[] for n in range(N): xnew,ynew=random(2) x_coords.append(xnew) y_coords.append(ynew) return x_coords,y_coords xc,yc=random_coordinates(N) plot(xc,yc,'ro',markersize=8) show() from numpy.random import random from pylab import * N = 20 # Number of points to generate def random_coordinates(N): x_coords = [] y_coords = [] for n in range(N): xnew,ynew = random(2) x_coords.append(xnew) y_coords.append(ynew) return x_coords,y_coords def dist2d(x1,y1,x2,y2): return sqrt((x1-x2)**2+(y1-y2)**2) def max_dist(xc,yc): max_dist = 0.0 num_points = len(xc) for ii in range(num_points): for jj in range(num_points): dist = dist2d(xc[ii],yc[ii],xc[jj],yc[jj]) if dist > max_dist: max_dist = dist xvals = [xc[ii],xc[jj]] yvals = [yc[ii],yc[jj]] return max_dist, xvals, yvals xc,yc = random_coordinates(N) max_dist,pnt1,pnt2 = max_dist(xc,yc) plot(xc,yc,'ro',markersize=8) plot(pnt1,pnt2,'b-',lw=2) show() def max_dist(xc,yc): """ Finds the maximum distance between any two points in a collection of 2D points. The points corresponding to this distance are also returned. Parameters ---------- xc : list List of x-coordinates yc : list List of y-coordinates Returns ------- max_dist : float Maximum distance xvals : list x-coodinates of two points yvals : list y-coordinates of two points """ max_dist=0.0 #initialize max_dist num_points=len(xc) #number of points in collection for ii in range(num_points): for jj in range(num_points): dist=dist2d(xc[ii],yc[ii],xc[jj],yc[jj]) if dist>max_dist: max_dist=dist xvals=[xc[ii],xc[jj]] yvals=[yc[ii],yc[jj]] return max_dist, xvals, yvals from IPython.core.display import HTML def css_styling(): styles = open("styles/style.css", "r").read() return HTML(styles) css_styling()