函数
In [1]:
bigx = 10
def double_times(x = bigx):
return x * 2
bigx = 1000
double_times()
Out[1]:
在可变的集合类型中(list和dictionary)中,如果默认参数为该类型,那么所有的操作调用该函数的操作将会发生变化
In [4]:
def foo(values, x=[]):
for value in values:
x.append(value)
return x
foo([0,1,2])
Out[4]:
In [5]:
foo([4,5])
Out[5]:
In [8]:
def foo_fix(values, x=[]):
if len(x) != 0:
x = []
for value in values:
x.append(value)
return x
foo_fix([0,1,2])
Out[8]:
In [9]:
foo_fix([4,5])
Out[9]:
In [14]:
x = 5
def set_x(y):
x = y
print 'inner x is {}'.format(x)
set_x(10)
print 'global x is {}'.format(x)
x = 5 表明为global变量,但是在set_x函数内部中,出现了x,但是其为局部变量,因此全局变量x并没有发生改变。
In [16]:
def set_global_x(y):
global x
x = y
print 'global x is {}'.format(x)
set_global_x(10)
print 'global x now is {}'.format(x)
通过添加global关键字,使得global变量x发生了改变。
In [1]:
def fib_recursive(n):
if n == 0 or n == 1:
return n
else:
return fib_recursive(n-1) + fib_recursive(n-2)
fib_recursive(10)
Out[1]:
In [3]:
def fib_iterator(n):
g = 0
h = 1
i = 0
while i < n:
h = g + h
g = h - g
i += 1
return g
fib_iterator(10)
Out[3]:
In [8]:
def fib_iter(n):
g = 0
h = 1
i = 0
while i < n:
h = g + h
g = h -g
i += 1
yield g
for value in fib_iter(10):
print value,
In [20]:
import numpy as np
a = np.array([[1,1],[1,0]])
def pow_n(n):
if n == 1:
return a
elif n % 2 == 0:
half = pow_n(n/2)
return half.dot(half)
else:
half = pow_n((n-1)/2)
return a.dot(half).dot(half)
def fib_pow(n):
a_n = pow_n(n)
u_0 = np.array([1,0])
return a_n.dot(u_0)[1]
fib_pow(10)
Out[20]:
In [22]:
def quick_sort(array):
if len(array) < 2:
return array
else:
pivot = array[0]
left = [item for item in array[1:] if item < pivot]
right = [item for item in array[1:] if item >= pivot]
return quick_sort(left)+[pivot]+quick_sort(right)
quick_sort([10,11,3,21,9,22])
Out[22]:
In [ ]: