CS156
Chris Pollett
Feb 8, 2012
my_list = ['YO', 1.2, 7, "HI", ["scary nested list", "watch out!"]] my_empty_list = [] # or list()
b = my_list[0] # b is now: 'YO' c = my_list[4][1] # c is now: 'watch out!' d = my_list[1:3] # d is [1.2, 7] e = my_list[2:] # e is [7, 'HI', ['scary nested list', 'watch out!']]
my_list.append("an end") #my_list now #['YO', 1.2, 7, 'HI', ['scary nested list', 'watch out!'], 'an end'] my_list.insert(2, 3) # my_list now #['YO', 1.2, 3, 7, 'HI', ['scary nested list', 'watch out!'], 'an end'] a = [1, 2, 3] + [4, 5] #a is [1, 2, 3, 4, 5]
import sys if(len(sys.argv)) != 2: #notice sys.argv is a list of command-line args and we found its length print "Please supply a filename" raise SystemExit(1) # throw an error and exit f = open(sys.argv[1]) #program name is argv[0] lines = f.readlines() # reads all lines into list one go f.close() #convert inputs to list of ints ivalues = [int(line) for line in lines] # print min and max print "The min is ", min(ivalues) print "The max is", max(ivalues)
a = ( 1, "hello", 3) b = ( some, where) c = "6 scared of 7", "as 7 8 9" #notice can omit paren's d = () # 0-tuple e = 'yo', #one tuple f = ('yo',) #same one tuple g = (d,) # g is ((),)
c = (4, 5) a, b = c
my_set = set([3, 9, 2, 6]) another_set = set("goodness") # set of unique chars print another_set # set(['e', 'd', 'g', 'o', 'n', 's']) if 'e' in another_set: print "it's in there"
a = my_set; b = another_set; c = a | b # union of sets c = a & b # intersection of sets c = a - b # difference of sets c = a ^ b # symmetric difference of sets another_set.add('y') # adds a single element to set another_set.update([6,7,8]) # add multiple elements my_set.remove(3) # removes the number 3 from my_set
person = { "name" : "bob", "age" : 27, "sex" : "Male" } empty_dict = {} #an empty dictionary # or use dict()
name= person["name"] person["age"] = 28 person["address"] = "somewhere" #this would add a key-value pair del person["age"] # removes key value associated with 'age'
if "name" in person: name = person["name"] else: name = "no one" #the above conditional can be shortened to: name = person.get("name", "no one")
keys = list(person) #keys is ['address', 'name', 'sex'] #could also do person.keys() #person.values() would get list of values #person.len() gives the number of keys in dictionary
while condition: statement1 statement2 ...
for n in [1,2,3,4,5,6,7,8,9]: print "2 to the %d is %d" % (n, 2**n)
for n in range(1,10): print "2 to the %d is %d" % (n, 2**n) # same as before
a = range(5) # can omit start to get a = 0, 1, 2, 3, 4 b = range(1,8) # b = 1, 2, 3, 4, 5, 6, 7 c = range(0, 13, 2) # c = 0, 2, 4, 6, 8, 10, 12 d = range(7, 2, -1) # d = 7, 6, 5, 4, 3
a = "Get rich quick" for b in a: print b c = ["now", "I", "know"] for d in c: print c person = { "name" : "bob", "age" : 27, "sex" : "Male" } for key in person: print key, person[key] f = open("my_file.txt") for line in f print line
def smaller_value( a, b): if a < b: return a # notice single line if can be same line else: return b
print smaller_value(8, 4)
def reverse_list(list): if list == []: return [] return reverse_list(list[1:]) + [list[0]] print reverse_list([1, 2, 3, 4]) # prints [4, 3, 2, 1] def divide(a,b): q = a // b r = a - q*b return (q, r) quotient, remainder = divide(2373, 16)
def expify(a, b=2): return b**a #if don't give a second argument b will be 2