import math
def is_sqr(x):
return int(math.sqrt(x)) == math.sqrt(x)
print filter(is_sqr, range(1, 101))
代码都差不多,思路跟上就行了
def is_sqr(x):
return int(math.sqrt(x)) == math.sqrt(x)
print filter(is_sqr, range(1, 101))
代码都差不多,思路跟上就行了
2017-08-23
def count():
fs = []
for i in range(1, 4):
def f(i):
def g():
return i*i
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(i):
def g():
return i*i
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2017-08-22
1.capitalize() 首字母大写。
2.title() 文章式格式,一句话中单词首字母大写。
3.s[0].upper s[1:].lower 这是在拼凑结果,没有内置函数时可以拼凑,有内置函数可以搞定时没必要
2.title() 文章式格式,一句话中单词首字母大写。
3.s[0].upper s[1:].lower 这是在拼凑结果,没有内置函数时可以拼凑,有内置函数可以搞定时没必要
2017-08-22
import math
def is_sqr(x):
return int(math.sqrt(x))-math.sqrt(x)==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return int(math.sqrt(x))-math.sqrt(x)==0
print filter(is_sqr, range(1, 101))
2017-08-22
这样也可以
def __cmp__(self, s):
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
elif self.score == s.score:
return cmp(self.name, s.name)
return 0
def __cmp__(self, s):
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
elif self.score == s.score:
return cmp(self.name, s.name)
return 0
2017-08-22
def count(i):
def f():
return i*i
# return f(i)
return f
f1 = count(1)
f2 = count(2)
f3 = count(3)
print f1(),f2(),f3()
def f():
return i*i
# return f(i)
return f
f1 = count(1)
f2 = count(2)
f3 = count(3)
print f1(),f2(),f3()
2017-08-21
class Fib(object):
def __call__(self, num):
a, b, n, l = 0, 1, 1, []
while n<= num:
l.append(a)
a, b = b, a + b
n += 1
return l
f = Fib()
print f(10)
def __call__(self, num):
a, b, n, l = 0, 1, 1, []
while n<= num:
l.append(a)
a, b = b, a + b
n += 1
return l
f = Fib()
print f(10)
2017-08-20
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
#map in python3 is a class;
#map in python2 is a build-in function;
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
#map in python3 is a class;
#map in python2 is a build-in function;
2017-08-20
def __cmp__(self, s):
if self.score > s.score:
return -1
elif self.score < s.score:
return 1
elif self.name < s.name:
return -1
return 0
if self.score > s.score:
return -1
elif self.score < s.score:
return 1
elif self.name < s.name:
return -1
return 0
2017-08-20
思考题解决思路:用过滤函数生成一个只有Student类的list,再进行排序。
print sorted(filter(lambda obj: type(obj) == Student, L))
print sorted(filter(lambda obj: type(obj) == Student, L))
2017-08-20
class BStudent(Person,BasketballMixin):
def __init(self):
super(Person,self).__init()
super(BasketballMixin,self).__init()
pass
class FTeacher(Person,FootballMixin):
def __init(self):
super(Person,self).__init()
super(FootballMixin,self).__init()
pass
def __init(self):
super(Person,self).__init()
super(BasketballMixin,self).__init()
pass
class FTeacher(Person,FootballMixin):
def __init(self):
super(Person,self).__init()
super(FootballMixin,self).__init()
pass
2017-08-20