import math
def is_sqr(x):
a = int(math.sqrt(x))
return a*a == x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
a = int(math.sqrt(x))
return a*a == x
print filter(is_sqr, range(1, 101))
2019-08-14
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])
2019-08-14
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'])
2019-08-14
def format_name(s):
return s. title()
print(list(map(format_name,['adma'],['LISA'],['Bart'])))
return s. title()
print(list(map(format_name,['adma'],['LISA'],['Bart'])))
2019-08-12
import math
def is_sqr(x):
return (math.sqrt(x)%1 ==0)
print filter(is_sqr, range(1, 101))
返回平方根后除以1余数为0的值也可以达到目的
def is_sqr(x):
return (math.sqrt(x)%1 ==0)
print filter(is_sqr, range(1, 101))
返回平方根后除以1余数为0的值也可以达到目的
2019-08-08
用lambda表达式,强制保留局部值
def count():
fs = []
for i in range(1, 4):
a = i * i
fs.append(lambda a=a: a)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
def count():
fs = []
for i in range(1, 4):
a = i * i
fs.append(lambda a=a: a)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2019-08-05
class Fib(object):
def __call__(self,num):
self.num=L[:num]
return self.num
L=[0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
f = Fib()
print f(10)
def __call__(self,num):
self.num=L[:num]
return self.num
L=[0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
f = Fib()
print f(10)
2019-08-04
class Fib(object):
def __init__(self, num):
self.num=L[:num]
def __str__(self):
return str(self.num)
def __len__(self):
return len(self.num)
L=[0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
f = Fib(10)
print f
print len(f)
def __init__(self, num):
self.num=L[:num]
def __str__(self):
return str(self.num)
def __len__(self):
return len(self.num)
L=[0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
f = Fib(10)
print f
print len(f)
2019-08-04
这篇博文讲的很透彻
https://blog.csdn.net/weixin_41656968/article/details/80286238
https://blog.csdn.net/weixin_41656968/article/details/80286238
2019-08-02
class Person(object):
__count = 0
def __init__(self, name):
Person.__count += 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'attributeError'
__count = 0
def __init__(self, name):
Person.__count += 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'attributeError'
2019-08-01