def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-06-18
def calc_prod(lst):
def fun():
num = 1
for x in lst:
num = num * x
return num
return fun
f = calc_prod([1, 2, 3, 4])
print f()
def fun():
num = 1
for x in lst:
num = num * x
return num
return fun
f = calc_prod([1, 2, 3, 4])
print f()
2015-06-17
import math
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
2015-06-17
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2015-06-17
def format_name(s):
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2015-06-17
# 希望一次返回3个函数,分别计算1x1,2x2,3x3:
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())#假如改成这样 就能输出 1 4 9
return fs
f1, f2, f3 = count()
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())#假如改成这样 就能输出 1 4 9
return fs
f1, f2, f3 = count()
2015-06-17
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted([L1[0].name,L1[1].name,L1[2].name])
print L2[0]
print L2[1]
print L2[2]
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted([L1[0].name,L1[1].name,L1[2].name])
print L2[0]
print L2[1]
print L2[2]
2015-06-16
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2015-06-16
def format_name(s):
return s.lower().capitalize()
#lower()方法所有字母小写, capitalize()方法首字母大写
print map(format_name, ['adam', 'LISA', 'barT'])
return s.lower().capitalize()
#lower()方法所有字母小写, capitalize()方法首字母大写
print map(format_name, ['adam', 'LISA', 'barT'])
2015-06-15