def calc_prod(lst):
def k():
return reduce(lambda x,y :x*y,lst)
return k
f = calc_prod([1, 2, 3, 4])
print f()
def k():
return reduce(lambda x,y :x*y,lst)
return k
f = calc_prod([1, 2, 3, 4])
print f()
2015-07-02
def cmp_ignore_case(s1, s2):
if s1.lower()<s2.lower():
return -1
if s1.lower()>s2.lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower()<s2.lower():
return -1
if s1.lower()>s2.lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-06-30
import math
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
2015-06-30
class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for key in kw:
setattr(self,key,kw[key])
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for key in kw:
setattr(self,key,kw[key])
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2015-06-29
def count():
fs = []
for i in range(1, 4):
def f(i):
def g():
return i*i
return g
r=f(i)
fs.append(r)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i):
def g():
return i*i
return g
r=f(i)
fs.append(r)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-06-29
没有学过最大公约数gcd的哭晕在厕所呀
写了不包含0.1的质数组合
除了余数等于0的除了再打印
不为0的直接打印
心想这么麻烦
看了示例哭晕在测试,看来要回头去先把函数背背
写了不包含0.1的质数组合
除了余数等于0的除了再打印
不为0的直接打印
心想这么麻烦
看了示例哭晕在测试,看来要回头去先把函数背背
2015-06-28
实例代码就是精简
无语
def __cmp__(self, s):
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
else:
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
无语
def __cmp__(self, s):
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
else:
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
2015-06-28
@property
def grade(self):
return 'A' if self.__score >= 80 else 'B' if self.__score >= 60 else 'C'
def grade(self):
return 'A' if self.__score >= 80 else 'B' if self.__score >= 60 else 'C'
2015-06-28
from __future__ import division
def __float__(self):
return self.p / self.q
def __float__(self):
return self.p / self.q
2015-06-28
class Fib(object):
def __init__(self,num):
self.list = []
for x in range(0,num):
if x < 2:
self.list.append(x)
else:
self.list.append(self.list[x-2]+self.list[x-1])
def __len__(self):
return len(self.list)
def __str__(self):
return self.list.__str__()
def __init__(self,num):
self.list = []
for x in range(0,num):
if x < 2:
self.list.append(x)
else:
self.list.append(self.list[x-2]+self.list[x-1])
def __len__(self):
return len(self.list)
def __str__(self):
return self.list.__str__()
2015-06-28
def count():
fs = []
for i in range(1, 4):
def calc():
a = i 为什么我把a从形参里拿出来写到这里就不行呢?
return a*a
fs.append(calc)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def calc():
a = i 为什么我把a从形参里拿出来写到这里就不行呢?
return a*a
fs.append(calc)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-06-27
看到有人用title()和capitalize()函数,去help()了一下这两个函数,给个例子说明这两个函数的区别:
s = 'this is a string'
s.title()的结果为'This Is A String'
s.capitalize()结果为'This is a string'
s = 'this is a string'
s.title()的结果为'This Is A String'
s.capitalize()结果为'This is a string'
2015-06-27