In [1]:
xs = [1,2,3]

In [2]:
len(xs)


Out[2]:
3

In [5]:
list(range(2, 10))


Out[5]:
[2, 3, 4, 5, 6, 7, 8, 9]

In [6]:
for i in range(10):
    print(i)


0
1
2
3
4
5
6
7
8
9

In [7]:
for x in xs:
    print(x)


1
2
3

In [9]:
for i, x in enumerate(xs, start=10):
    print(i, x)


10 1
11 2
12 3

In [10]:
xs


Out[10]:
[1, 2, 3]

In [14]:
ys = 'abcd'

In [15]:
for x, y in zip(xs, ys):
    print(x, y)


1 a
2 b
3 c

In [17]:
for i, j in enumerate(range(10), 10):
    print(i, j)


10 0
11 1
12 2
13 3
14 4
15 5
16 6
17 7
18 8
19 9

In [18]:
a, b, c = 1,2,3

In [19]:
a


Out[19]:
1

In [20]:
c


Out[20]:
3

In [21]:
a, *b, c = 1,2,3,4

In [22]:
a


Out[22]:
1

In [23]:
b


Out[23]:
[2, 3]

In [24]:
c


Out[24]:
4

In [28]:
def f(x):
    """The identity function.
    
    x: any python object
    """
    
    return x

In [29]:
f(123)


Out[29]:
123

In [33]:
def f(a, b=1, c=1):
    return a + 2*b + 3*c

In [32]:
f(1, c=3, b=2)


Out[32]:
14

In [34]:
f(10)


Out[34]:
15

In [38]:
def g(x, y, *args, **kwargs):
    print(args)
    print(kwargs)
    print(x, y)

In [39]:
g(10, 11, 1,2,3,a=4,b=5,c=6)


(1, 2, 3)
{'c': 6, 'a': 4, 'b': 5}
10 11

In [40]:
f = lambda x, y: x + y

In [41]:
f(2, 3)


Out[41]:
5

In [43]:
def pow4(x):
    return x**4

In [45]:
dispatch = {
    'square': lambda x: x**2,
    'cube' : lambda x: x**3,
    'quad': pow4
}

In [46]:
dispatch['square'](2)


Out[46]:
4

In [49]:
def foo(a, f):
    return f(a)

In [50]:
foo(2, pow4)


Out[50]:
16

In [51]:
def adder_factory(x):
    def f(y):
        return x + y
    return f

In [52]:
add3 = adder_factory(3)
add5 = adder_factory(5)

In [53]:
add3(4)


Out[53]:
7

In [54]:
add5(4)


Out[54]:
9

In [55]:
import time

In [62]:
def timer(f):
    def g(*args, **kwargs):
        start = time.time()
        result = f(*args, **kwargs)
        elapsed = time.time() - start
        return result, elapsed
    return g

In [63]:
def f(x):
    time.sleep(1)
    return x**2

In [64]:
f(2)


Out[64]:
4

In [65]:
g = timer(f)

In [66]:
g(2)


Out[66]:
(4, 1.001072883605957)

In [67]:
@timer
def h(x, y):
    return x+y

In [68]:
h(2,3)


Out[68]:
(5, 1.430511474609375e-06)

In [76]:
xs = []
for i in range(10):
    if i % 2 ==0:
        xs.append(i**2)

In [77]:
xs


Out[77]:
[0, 4, 16, 36, 64]

In [80]:
# list comprehension
xs = [i**2 for i in range(10) 
      if i % 2 ==0]

In [81]:
xs


Out[81]:
[0, 4, 16, 36, 64]

In [83]:
def square(x):
    return x**2

def is_even(x):
    return x%2 == 0

list(map(square, filter(is_even, range(10))))


Out[83]:
[0, 4, 16, 36, 64]

In [84]:
import numpy as np

In [86]:
np.arange(10)**2


Out[86]:
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])

In [90]:
for n in (i**2 for i in range(100)):
    print(n)
    if n > 10:
        break


0
1
4
9
16

In [91]:
{i % 3 for i in range(10)}


Out[91]:
{0, 1, 2}

In [92]:
{k: w for k, w in enumerate('abcd')}


Out[92]:
{0: 'a', 1: 'b', 2: 'c', 3: 'd'}

In [112]:
class A:
    def __init__(self, x):
        self.x = x
        
    def __repr__(self):
        return '%s' % self.x
    
    def foo(self):
        return self.x**2

In [114]:
a1


Out[114]:
2

In [115]:
xs


Out[115]:
[0, 4, 16, 36, 64]

In [113]:
a1 = A(2)

In [108]:
a2 = A(3)

In [109]:
a1.foo()


Out[109]:
4

In [110]:
a2.foo()


Out[110]:
9

In [ ]:


In [96]:
a1.__class__.__name__


Out[96]:
'A'

In [97]:
a2.__class__.__name__


Out[97]:
'A'

In [98]:
foo = 'foo'

In [ ]:
foo.