# IPython log file 1 x = 2 x get_ipython().magic(u'logstart') get_ipython().magic(u'pinfo str') 1 1.5 1. 1 + 3 * 2 3/2 3/2. "hello" "hello\" there" 'hello" there' "\nhello" "\thello" """hello there""" 'hello len(r"\n") r"\n" u"foobar" "asdf" + "qwer" "-+=" * 10 x = "foobar" x[0] = 'a' "foobar" + 1 int(0) str(1) "foobar" + str(1) 0 "a string" [1, 2] float(1) list() x = 1 y = 1 y = x id(x) id(y) x = 5 y = 5 id(x) id(y) x = "foo" y = "foo" id(x) id(y) del(y) y x del(x) id(1) True False true True or False True and False not False 1 or 0 bool(1) bool(0) 1 or 0 "value1" if 0 else "value2" x x = 4 if x < 10: print "x was less than 10" bool("asdf") bool("False") bool("") answer = "yes" if answer: print "true" 1 == 0 True == 0 True == 1 True == 2 int(True) True is 1 id(True) id(1) True is True False is False 1 is True message = "this is section" section = 1 message + str(section) message + section bool("bar") bool("") bool(-1) x = 1 z = 0 x and z bool(x and z) x and z x or z 0 or 100 x = 0 or 100 x x = 0 or 100 or 200 x 0 == False z = 5 if z == True: print "yes" from datetime import datetime help(datetime) datetime.now() help(datetime.now) 5 % 2 hours = 11 "Good morning. it is " + str(hours) + " hours" "Good morning. It is %.2f hours" % hours hours "Good morning. It is %s hours" % hours datime.now() datetime.now() datetime.now().hour [1, 3] list() [] list("asdf") x = [1, "asdf", True] x x.append(3) x y = x y x id(x) id(y) x[0] = 10 y len("asdf") len(x) x while x: print x.pop() x x = range(10, 20) x x[0] get_ipython().magic(u'pinfo x.pop') i = 0 while i < len(x): print x[i] / 2 i += 1 for value in x: print value / 1 for value in x: print value / 2 help(x) x x.insert(0, 'a') x x.insert(100, 'a') x x[100] x[100] = 1 get_ipython().magic(u'pinfo x.remove') x x.remove('a') x x x[0] x[len(x) - 1] x[-1] x[-3] x x[0:3] x[0:] x[:] y = x[:] id(y) id(x) y = list(x) x x[1:] x[1:-1] x[::2] x x[1::2] x x[4] = ['a','b','c'] x x[4] x[4][2] del(x[4]) x x.extend(['a','b','c']) x get_ipython().magic(u'pinfo x.extend') x[3:4] = [1, 2, 3] x x[::-1] [1,2,3] + ['zb', 'qwer'] get_ipython().magic(u'pinfo range') help(range) range(0, 4) for i in range(10): print "hello" for i in range(10): print "hello" , i for i in range(0, 10): print "hello" , i x x.count(1) get_ipython().magic(u'pinfo x.count') x.index(1) x.index(100) 1 in x if x.count(1) > 0: print "yes 1 is in x" if 0 in x: print "yes 0 is in x" "i" in "Simeon" x = "a" x[0] x = "simeon franklin" x[0::2] x[-1] x = [1, 2, 3] y = (1, 2, 3) x y type(x) type(y) y.y y 1 in y y[0] y[0:-1] y[0] = 10 x = 1 y = 2 x, y = y, x (x, y) = (y, x) x y (x, y) = (y, x) x y get_ipython().magic(u'pinfo enumerate') x = range(10, 40, 6) x for val in enumerate(x): print val for (i, val) in enumerate(x): print "%s value is %s" % (i, val) "%s and %s" % 1, 2 "%s and %s" % (1, 2) "%s and %s" % [1, 2] "%s and %s" % (1, 2) 1, 2 ("") () ("") (,) tuple() 1, tuple('asdf') x for k,v in enumerate(x, 1): print k, v for k,v in enumerate(x, 100): print k, v import class1 reload(class1) import class1 class1 class1.fname class1.classmates help(class1) reload(class1) help(class1) dct = {} dict() dct['key'] = 1 dct dct['key'] = 2 dct dct[1] = 10 dct dct[4.5] = 10 dct dct[[1, 2] = 'foo' dct[[1, 2]] = 'foo' dct[(45, 20)] dct[(45, 20)] = 1 dct d = {'foo': 1, 'foo2': 4} d d['foo'] dict(foo=1, foo2=4) d d.has_key('foo') 'foo' in d get_ipython().magic(u'pinfo d.setdefault') d d.setdefault('x', 10) d d['z'] d.get('z') get_ipython().magic(u'pinfo d.get') d.get('x', 11) d[z] = d.get('z', 10) + 1 d d d[z] = d.get('z', 10) + 1 d d['z'] = d.get('z', 10) + 1 d z d[z] d d d.keys() l = ['z', 'y'] for key in l: if key in d: print "Yup: %s" % key get_ipython().magic(u'pinfo d.items') d.items() d.keys() d.values() d.items() get_ipython().magic(u'pinfo dict') dict([('a', 1), ('b', 2)]) d.items() get_ipython().magic(u'pinfo dict') dict(one=1, two=2) dict(3=3) for something in d: print something d get_ipython().magic(u'pinfo d.items') get_ipython().magic(u'pinfo d.iteritems') get_ipython().magic(u'pinfo d.viewitems') get_ipython().magic(u'pinfo d.clear') d d.clear() d get_ipython().magic(u'pinfo d.copy') get_ipython().magic(u'pinfo d.fromkeys') dict.fromkeys([1,2,3]) get_ipython().magic(u'pinfo d.pop') d.popitem get_ipython().magic(u'pinfo d.popitem') get_ipython().magic(u'pinfo d.update') get_ipython().magic(u'pinfo d.update') d d = {'foo': 1, 'bar': 2} overrides = {'foo': 4} d.update(overrides) d get_ipython().magic(u'pinfo input') get_ipython().magic(u'pinfo raw_input') fname = raw_input("Please type a last name: ") fname dict = {'foo': 1} dict list = [1,2,3] {} __builtin__ dir(__builtin__) dict = __builtin__.dict dict() list = __builtin__.list for = 1 import keyword keyword.kwlist import classmates4 '{1} {0}' % ('a', 'b', 'c') '{1} {0}' % ('a', 'b')) '{1} {0}' % ('a', 'b') '{1} {0}'.format('a', 'b') "%(fname)s %(lname)s" % {'fname': 'bob', 'lname': 'wills', 'role': 'hang gliding legend'} get_ipython().magic(u'pinfo str.format') set() set([1,2,3]) set([1,2,3, 1]) x = set([1,2,3]) y = set([3, 4, 5]) x.pop> get_ipython().magic(u'pinfo x.pop') x.add(0) x x.add(4) x get_ipython().magic(u'pinfo x.remove') x.remove(4) x.remove(5) x x y x & y x.intersection(y) x | y x ^ y x get_ipython().magic(u'pinfo x.intersection') x.intersection_update get_ipython().magic(u'pinfo x.intersection_update') get_ipython().magic(u'pinfo x.update') lst = range(10 ) lst.append(3) lst.append(4) lst lst = list(set(lst)) lst x ^ y x y x.difference(y) x x.symmetric_difference(y) import collections get_ipython().magic(u'pinfo collections') tup = (1, 2) x = tup[0[ ] x = tup[0] y = tup[1] (x, y) = tup x y x, y = tup x None d(x=None) dict(x=None) d = dict(x=None) d.get('x') d.get('x', -1) d['python'] = 2 d python = "ten" d[python] == d['python'] python x = d get_ipython().magic(u'pinfo list') astring = "Simeon" lst = list(astring) lst lst[0] = "s" lst str(lst) len(str(lst)) get_ipython().magic(u'pinfo str.join') lst "".join(lst) ",".join(lst) csv = ",".join(lst) csv list(csv) get_ipython().magic(u'pinfo str.split') csv csv.split(",") d d.values() d['fname'] = "simeon" d['lname'] = 'franklin' d['role'] = 'h2' d del(d['python']) d d.keys() d.values() lst2 = [] for val in d.values(): if val: lst2.append(val) ",".join(lst2) get_ipython().magic(u'pinfo filter') filter(None, d.values()) get_ipython().magic(u'pinfo list') list((1,2)) list("asdf") list(d)) list(d) d lst = range(10, 20) lst for num in lst: if num % 2 == 0 and num % 10 == 0: print num for num in lst: if not (num % 2 == 0 and num % 10 == 0): continue print num get_ipython().magic(u'hist ') for num in lst: if not (num % 2 == 0 and num % 10 == 0): break print num lst def foo(x, y): pass def foo(x, y): return x + y foo(4, 5) foo("asdf", "bar") x = foo("asdf", "bar") type(x) repr(x) x print d d.__repr__() repr(x) get_ipython().magic(u'hist ') def add(x, y): return x + y add(4, 3) x = 10 y = 12 add(2, 4) add() add(1) add(1, 2, 3) def sub(x, y=0): return x - y sub(3) get_ipython().magic(u'pinfo sum') get_ipython().magic(u'pinfo any') get_ipython().magic(u'pinfo all') sum([1,2,3]) get_ipython().magic(u'hist ') sub(x) sub(10) sub(10, 11) sub(10, y=11) sub(x=10, y=11) sub(y=11, x=10) def somefoo(data, a=None, b=None, c=None): returns sum(data) def somefoo(data, a=None, b=None, c=None): return sum(data) somefoo([1,2], c=-1) somefoo([1,2])) somefoo([1,2]) def somefoo(*args): print args somefoo() somefoo(3) somefoo(3, 4) somefoo(3, 4, 23) somefoo(3, 4, 23, 12) def sub(x, y): return x - y sub(4, 5) lst = range(5, 7) lst sub(lst[0], lst[1]) sub(*lst) lst.append(10) sub(*lst) sub(lst[0], lst[1], lst[2]) def sub(x, y=0): return x - y sub(4) def sumargs(*args): return sum(args) sum([1, 2, 3]) sumargs(1, 2, 3) sumargs(1, 2, 3, 34e) sumargs(1, 2, 3, 34) def sumargs(x, *args): print x print args sumargs() sumargs(1) sumargs(1, 3) sumargs(1, 3, 3) sumargs(1, 3, 3, 12) import classmates5