In [1]:
    
def square(x):
    return x * x
print square(10)
    
    
return inside the function, function still return None
In [2]:
    
def logging(message):
    print 'Message:', message
    
print logging('Hello world')
    
    
In [3]:
    
print square
    
    
In [4]:
    
power2 = square
print power2(10)
    
    
In [5]:
    
def exponential(x, base=2):
    value = 1
    for i in range(x):
        value = value * base
    return value
print exponential(10)
print exponential(2, 3)
    
    
In [6]:
    
def exp(base=2, x):
    return exponential(x, base)
    
    
In [7]:
    
def append2list(value, array=[]):
    array.append(value)
    return array
print append2list(1)
# get [1, 2] instead of [1] because the default value is shared between first call
print append2list(2)
    
    
In [8]:
    
def pet_color(dog, cat='black', pig='pink'):
    print 'Dog color:', dog, '. Cat color:', cat, '. Pig color:', pig
    
In [9]:
    
pet_color('yellow')
    
    
In [10]:
    
pet_color(dog='white')
    
    
In [40]:
    
pet_color('white', pig='blue')
    
    
In [12]:
    
pet_color(cat='blue', dog='white')
    
    
In [13]:
    
pet_color('white', cat='blue', 'pink')
    
    
In [46]:
    
A = [
    [-3, -2, -1],
    [2, 3, 0],
    [1, 4, 5],
    [6, 7, 8]
]
x = [7, 3, 1]
def dot(x, y):
    res = 0
    for i in xrange(len(x)):
        res += x[i] * y[i]
    
    return res
    
result = []
for i in xrange(len(A)):
    result.append(dot(A[i], x))
print result
    
    
In [27]:
    
def sum_square(*args):
    total = 0
    for no in args:
        total += no ** 2
    return total
print sum_square()
print sum_square(1, 2)
print sum_square(1, 2, 3)
    
    
In [26]:
    
def print_name(**kwargs):
    for kw in kwargs.keys():
        print kw, ':', kwargs[kw]
        
print_name(foo='bar', foo2='bar2')
    
    
In [24]:
    
def print_arg(required_arg, *args, **kwargs):
    pass
    
In [25]:
    
def print_arg(required_arg, **kwargs, *args):
    pass
    
    
In [36]:
    
range(3, 6)
    
    Out[36]:
In [37]:
    
args = [3, 6]
print range(*args)
    
    
** operator
In [39]:
    
d = {'cat': 'white', 'pig': 'blackwhite'}
print pet_color('brown', **d)
    
    
In [14]:
    
prod = lambda a, b: a * b
print prod(5, 2)
    
    
In [28]:
    
def compute(a, b, operator):
    return operator(a, b)
print compute(5, 2, lambda a, b: a + b)
print compute(5, 2, lambda a, b: a * b)
    
    
map(function,sequence): call function(item) for each of the sequence's items and returns a list of the return values.
In [34]:
    
print map(lambda x: x ** 2, range(0, 5))
    
    
filter(function, sequence): return a list contains items from sequence for which function(item) is true
In [33]:
    
print range(-5, 5)
print filter(lambda x: x > 0, range(-5, 5))
    
    
reduce(function, sequence): return a single value constructed by calling the binary function function on the first two items of the sequence, then on the result and the item, and so on.reduce(function, sequence, init_value): when init_value is provide, the binary function function will be called on the init_value and the first item of the sequence, then on the result and the next item, and so on.This is the picture describe the reduce process
In [35]:
    
print reduce(lambda a, b: a + b, [47, 11, 42, 13])
    
    
In [44]:
    
reduce(lambda a, b: a + b, [47, 11, 42, 13], -1)
    
    Out[44]: