def f(x):
if x <= 1:
return x
return f(x - 1) + f(x - 2)
g = f
def f(x):
return x
print(f(23))
print(g(23))
Author | Logan Chien
-
-
Item 7: Generator Function
from itertools import dropwhile, takewhile, islice def fibs(): a, b = 0, 1 while True: yield a a, b = b, a + b for x in fibs(): print(x) if x > 250: break print(list(islice(fibs(), 15))) print(list(takewhile(lambda x: x < 250, \ dropwhile(lambda x: x < 8, fibs()))))
-
Item 6: Generator Expression
a = [x * 2 for x in range(5)] b = (x * 2 for x in range(5)) print('a=', a) print('b=', b) for t in range(2): # Run Following Code Twice print('----') for i, x in enumerate(a): print('a[{}]={}'.format(i, x)) print('----') for i, x in enumerate(b …
-
Item 5: Method Resolution Order
class A(object): def __init__(self): print('A', end='') class B(A): def __init__(self): super().__init__() print('B', end='') class C(A): def __init__(self): super().__init__() print('C', end='') class D(B, C): def __init__(self): super().__init__() print('D', end='') A(); print(); B(); print(); C(); print(); D …
-
Item 4: Default Arguments
def append_x(x, a=None, b=None, c=[]): a = a or [] b = b if b is not None else [] a.append(x) b.append(x) c.append(x) print(a, b, c) s, t, u = [], [], [] append_x(1) append_x(2, s, t) append_x(3, s, t, u) print(s, t, u …
-
Item 3: Integer Division and Remainder
print(1//2, -1//2, (-1)//2, -(1//2)) print(1%2, -1%2, (-1)%2, -(1%2))
-
Item 2: for while else
def foo(xs): for i in xs: if i % 5 == 0: print('found', i) break else: print('not found') if __name__ == '__main__': foo([]) foo([3, 4]) foo([4, 5, 6])
-
Item 1: Unbound Local Error
a = 5 def foo(): if 1 == 2: a = 10 print('a=', a) if __name__ == '__main__': foo()
« Prev Page 4 / 4