a,b,c=['adr1','adr2','adr3']
print a,b,c
list还有这种用法
fs = []
for i in range(1, 4):
f(j)的闭包
r = f(i)
fs.append(r)
fs其实是存放了三个函数地址fs[f(1),f(2),f(3)]
f1(),f2(),f3()其实是使用函数(闭包)分别计算值1,4,9
print a,b,c
list还有这种用法
fs = []
for i in range(1, 4):
f(j)的闭包
r = f(i)
fs.append(r)
fs其实是存放了三个函数地址fs[f(1),f(2),f(3)]
f1(),f2(),f3()其实是使用函数(闭包)分别计算值1,4,9
2018-02-09
def calc_prod(lst):
def count():
return reduce(lambda x,y:x*y,lst)
return count
f = calc_prod([1, 2, 3, 4])
print f()
def count():
return reduce(lambda x,y:x*y,lst)
return count
f = calc_prod([1, 2, 3, 4])
print f()
2018-02-09
def cmp_ignore_case(s1, s2):
x=s1.upper()
y=s2.upper()
if(x>y):
return 1
if(x<y):
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
x=s1.upper()
y=s2.upper()
if(x>y):
return 1
if(x<y):
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-02-09
def is_sqr(x):
return math.sqrt(x) % 1 == 0
print filter(is_sqr, range(1, 101))
return math.sqrt(x) % 1 == 0
print filter(is_sqr, range(1, 101))
2018-02-09
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))
2018-02-08
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])
2018-02-08
def format_name(L):
return L[0:1].upper() + L[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return L[0:1].upper() + L[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-02-08
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)
2018-02-08
def calc_prod(lst):
def calc_prod():
prod=1
for x in lst:
prod=prod*x
return prod
return calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
def calc_prod():
prod=1
for x in lst:
prod=prod*x
return prod
return calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-02-08
def cmp_ignore_case(s1, s2):
if s1[0].lower()<s2[0].lower():
return -1
else:
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].lower()<s2[0].lower():
return -1
else:
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-02-08
class Student(Person):
#__slots__ = ('score')
def __init__(self,name,gender,score,**kw):
super(Student,self).__init__(name,gender)
self.score=score
for k,v in kw.items():
setattr(self,k,v)
s = Student('Bob', 'male', 99,sss='sss')
print s.score
print s.sss
#__slots__ = ('score')
def __init__(self,name,gender,score,**kw):
super(Student,self).__init__(name,gender)
self.score=score
for k,v in kw.items():
setattr(self,k,v)
s = Student('Bob', 'male', 99,sss='sss')
print s.score
print s.sss
2018-02-07