12
def factorial(n):
r=1
while n>1:
r*=n
n-=1
return r
def fib(n):
a,b=1,1
while a<n:
print(a,end=' ')
a,b=b,a+b
print('%d! =%d'%(5,factorial(5)))
fib(200)
12.2
def factorial(n):
r=1
while n>1:
r*=n
n-=1
return r
def fib(n):
a,b=1,1
while a<n:
print(a,end=' ')
a,b=b,a+b
13
def bifurcate_by(L,fn):
return [[x for x in L if fn(x)],
[x for x in L if not fn(x)]]
s= bifurcate_by(['beep','boop','foo','bar'],lambda x:x[0] == 'b')
#lambda函数对应fn
print(s)
14
f=lambda x,y,z:x*y*z
L=lambda x:[x**2,x**3,x**4]
print(f(3,4,5))
print(L(2))
15
import math
import random
import numpy.random as nr
a=math.gcd(12, 21)
b=nr.randint(0,2)
c=nr.randint(0,2,(4,3))
print(a)
print(b)
print(c)
16
from random import sample
from numpy.random import randint
a=sample(range(10),5)
b=randint(0,10,5)
print(a)
print(b)
17
from math import *
a=sin(3)
b=pi
c=e
d=radians(180)
print(a)
print(b)
print(c)
print(d)
18
from ex2_12_2 import *
print(factorial(6))
fib(300)