# This is bad #from numpy import * import itertools #itertools. import numpy as np #np.array( blank_list = [] # how to create a blank list colors = ['red', 'green', 'blue', 'yellow'] # list print colors # IF YOU SEE THIS for i in range(len(colors)): # for loop print colors[i] # DO THIS INSTEAD for color in colors: print color # If you see this kind of thing in your code for index in range(len(colors)): print index, '-->', colors[index] # Do this instead for index, color in enumerate(colors): print index, '-->', color #add list comprehensions dave = [x**2 for x in xrange(5)] print dave blank_dict = {} # how to create a blank... dictionary dictionary = {'Spider Man':'blue', 'Dave':'green', 'Jonathan':'red' } print dictionary print "Keys: " for key in dictionary: print key print "Values: " for value in dictionary.itervalues(): print value # dictionaries are unordered! for key, value in dictionary.items(): print key, '-->', value dictionary_two = {key:dictionary[key]*2 for key in dictionary} dictionary_two.update({"abbie":"purple"}) print dictionary print dictionary_two # Read/write using the with with open('text.ascii', 'w') as filehandle: filehandle.write('hello!') # http://stackoverflow.com/questions/3394835/args-and-kwargs def print_everything(*args): """Takes a list of arguments and prints an enumerated list. """ for count, thing in enumerate(args): print '{0}. {1}'.format(count, thing) print_everything('apple', 'banana', 'cabbage') # Similarly, **kwargs allows you to handle named arguments that you have not defined in advance: def table_things(title_string, **kwargs): """Takes a title and a dictionary of elements to print as a table elements.""" print title_string print "-" * len(title_string) for name, value in kwargs.items(): print '{0} = {1}'.format(name, value) table_things("Classifying stuff with Dave", apple = 'fruit', cabbage = 'vegetable') names = ['David', 'Jonathan', 'Fred', 'Bob', 'Steve', 'Spider Man'] statuses = ['cool', 'lame', 'lame', 'lame', 'lame', 'cool', ] cooldict = {name:status for name, status in itertools.izip(names, statuses)} cooldict table_things("Cool factor", **cooldict) # double splat! import pylab as pl pl.rcParams['figure.figsize'] = 8, 6 # plotsize N_points = 100 # 10, 100 x = np.linspace(0, 2*np.pi, N_points) f = np.sin(x) pl.plot(x, f, label="sin") # color, label pl.plot(x, np.cos(x), label="cos", color='red') # color, label pl.legend() pl.ylabel("sin(x)") pl.xlabel("radians") pl.savefig("amazing-1.pdf")