from IPython.display import Image Image("IPy_header.png") %run talktools # Create a [list] days_of_the_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', ] days_of_the_week # Simple for-loop for day in days_of_the_week: print day # Double for-loop for day in days_of_the_week: for letter in day: print letter, for day in days_of_the_week: for letter in day: print letter.lower(), letters = [letter for day in days_of_the_week for letter in day] letters = [letter for day in days_of_the_week for letter in day] print letters sorted_letters = sorted([x.lower() for x in letters]) print sorted_letters unique_sorted_letters = sorted(set(sorted_letters)) print "There are", len(unique_sorted_letters), "unique letters in the days of the week." print "They are:", ''.join(unique_sorted_letters) def first_three(input_string): """Takes an input string and returns the first 3 characters.""" return input_string[:3] [first_three(day) for day in days_of_the_week] def last_N(input_string, number=2): """Takes an input string and returns the last N characters.""" return input_string[-number:] [last_N(day, 153) for day in days_of_the_week if len(day) > 6] from math import pi print [str(round(pi, i)) for i in xrange(2, 9)] list_of_lists = [[i, round(pi, i)] for i in xrange(2, 9)] print list_of_lists # Let this be a warning to you! # If you see python code like the following in your work: for x in range(len(list_of_lists)): print "Decimals:", list_of_lists[x][0], print "expression:", list_of_lists[x][1] # Change it to look more like this: for decimal, rounded_pi in list_of_lists: print "Decimals:", decimal, "expression:", rounded_pi print list_of_lists import numpy as np import matplotlib.pyplot as plt # The following line is an ipython notebook trick %matplotlib inline plt.rcParams['figure.figsize'] = 12, 8 # plotsize x = np.arange(10000) print "x -> ", x # notice the smart printing print "x[:] -> ", x[:] print "x[0] -> ", x[0] # first element print "x[0:5] -> ", x[0:5] # first 5 elements print "x[-1] -> ", x[-1] # last element # A bit more complicated slicing print x[-5:] # last five elements print x[-5:-2] # print x[-5:-1] # last 4 elements (not final value) # Single physical cloud with following physical parameters def GaussFunc(x, amplitude, centroid, sigma): """Takes an array, and calculates a Gaussian with the following parameters. """ return amplitude * np.exp(-0.5 * ((x - centroid) / sigma)**2) feature_centroid = 5315.3 feature_amplitude = 2.3 feature_sigma = 1.5 wavelength = np.linspace(5305., 5330., 120) tau = GaussFunc(wavelength, feature_amplitude, feature_centroid, feature_sigma) flux = np.exp(-tau) sigma = 0.05 noise = np.random.randn(len(flux)) * sigma observed = flux + noise error = np.ones_like(wavelength) * sigma plt.plot(wavelength, flux) plt.plot(wavelength, observed) np.savetxt("example.txt", np.transpose((wavelength, observed, flux, error, noise))) # np.random.randn? %less example.txt wave = [] observed_flux = [] error = [] for line in open("example.txt", 'r'): wave.append(line[0]) observed_flux.append(line[1]) error.append(line[3]) wave[:10] # Whoops! wave = [] observed_flux = [] error = [] for line in open("example.txt", 'r'): line = line.split() wave.append(line[0]) observed_flux.append(line[1]) error.append(line[3]) wave[:10] # Dang! still strings! # If you see yourself doing this kind of thing... wave = [] observed_flux = [] error = [] for line in open("example.txt", 'r'): line = line.split() wave.append(float(line[0])) observed_flux.append(float(line[1])) error.append(float(line[3])) wave = np.array(wave) observed_flux = np.array(observed_flux) error = np.array(error) # Do this instead wave, observed_flux, error = np.loadtxt("example.txt", usecols=(0, 1, 3), unpack=True) # Run this cell %load http://matplotlib.org/mpl_examples/pylab_examples/polar_legend.py # And it loads the code here, ready to run (obviously without this comment). #!/usr/bin/env python import numpy as np from matplotlib.pyplot import figure, show, rc # radar green, solid grid lines rc('grid', color='#316931', linewidth=1, linestyle='-') rc('xtick', labelsize=15) rc('ytick', labelsize=15) # force square figure and square axes looks better for polar, IMO fig = figure(figsize=(8,8)) ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c') r = np.arange(0, 3.0, 0.01) theta = 2*np.pi*r ax.plot(theta, r, color='#ee8d18', lw=3, label='a line') ax.plot(0.5*theta, r, color='blue', ls='--', lw=3, label='another line') ax.legend() show() Image("../../../../Screenshots/Screenshot 2014-07-18 14.51.30.png")