In [1]:
locals()


Out[1]:
{'In': ['', u'locals()'],
 'Out': {},
 '_': '',
 '__': '',
 '___': '',
 '__builtin__': <module '__builtin__' (built-in)>,
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': 'Automatically created module for IPython interactive environment',
 '__name__': '__main__',
 '_dh': [u'/Users/vyin/WorkDocs/work/python-bootcamp'],
 '_i': u'',
 '_i1': u'locals()',
 '_ih': ['', u'locals()'],
 '_ii': u'',
 '_iii': u'',
 '_oh': {},
 '_sh': <module 'IPython.core.shadowns' from '/Users/vyin/anaconda/lib/python2.7/site-packages/IPython/core/shadowns.pyc'>,
 'exit': <IPython.core.autocall.ZMQExitAutocall at 0x10911b390>,
 'get_ipython': <bound method ZMQInteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x1090ba350>>,
 'quit': <IPython.core.autocall.ZMQExitAutocall at 0x10911b390>}

In [13]:
def f():
    myvar = 'hello'
    print locals()

f()


{'myvar': 'hello'}

In [30]:
def my_dec(f):
    def new_hello():
        print "DECORATOR START"
        f()
        print "DECORATOR END"

    return new_hello

@my_dec
def hello():
    print 'Original hello!'

hello()


DECORATOR START
Original hello!
DECORATOR END

In [31]:
hello()


DECORATOR START
Original hello!
DECORATOR END

In [68]:
def simple_gen(start, stop):
    for i in range(start, stop):
        yield -i

In [70]:
g = simple_gen(5, 9)

In [75]:
next(g)


---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-75-5f315c5de15b> in <module>()
----> 1 next(g)

StopIteration: 

In [46]:
type(g)


Out[46]:
generator

In [63]:
h = simple_gen()

In [67]:
next(h)


Out[67]:
-2

In [76]:
import collections

In [83]:
x = ['a', 'b', 'c', 'b', 'b']
y = collections.Counter(x)

In [87]:
y.keys()


Out[87]:
['a', 'c', 'b']

In [91]:
y.most_common(1)


Out[91]:
[('b', 3)]

In [101]:
x = collections.defaultdict(object)

In [99]:
x['dummy']


Out[99]:
<object at 0x108fd7b50>

In [103]:
import datetime

In [104]:
t = datetime.time(14, 59, 23)

In [105]:
t


Out[105]:
datetime.time(14, 59, 23)

In [106]:
t + 2


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-106-e2644ca8ba6c> in <module>()
----> 1 t + 2

TypeError: unsupported operand type(s) for +: 'datetime.time' and 'int'

In [109]:
t.isoformat()


Out[109]:
'14:59:23'

In [117]:
if None:
    print "None"
else:
    print "Something"


Something

In [121]:
def f():
    return None

if None:
    print "None"
else:
    print "Something"


Something

In [122]:
partition("abcde", 'c')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-122-cd2aa0985687> in <module>()
----> 1 partition("abcde", 'c')

NameError: name 'partition' is not defined

In [123]:
'abcde'.partition('c')


Out[123]:
('ab', 'c', 'de')

In [124]:
s = 'abcde'

In [125]:
s.partition('z')


Out[125]:
('abcde', '', '')

In [126]:
s.partition('d')


Out[126]:
('abc', 'd', 'e')

In [127]:
s1 = {1,2,3}
s2 = {1,4,5}

In [128]:
s1.difference(s2)


Out[128]:
{2, 3}

In [129]:
s1


Out[129]:
{1, 2, 3}

In [130]:
s2


Out[130]:
{1, 4, 5}

In [131]:
s1.difference_update(s2)

In [132]:
s1


Out[132]:
{2, 3}

In [133]:
s2


Out[133]:
{1, 4, 5}

In [139]:
{k+'$$$':v**2 for k,v in zip(('a','b', 'c'), range(3,6))}


Out[139]:
{'a$$$': 9, 'b$$$': 16, 'c$$$': 25}

In [140]:
[1,2] + [3,4]


Out[140]:
[1, 2, 3, 4]

In [144]:
print [1,2].extend([3,4])


None

In [145]:
x = [1,2]
y = [3,4]

In [148]:
print x.extend(y)


None

In [147]:
print x


[1, 2, 3, 4]

In [ ]: