import math
def is_sqr(x):
return x % math.sqrt(x) ==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return x % math.sqrt(x) ==0
print filter(is_sqr, range(1, 101))
2019-05-14
import math
def is_sqr(x):
sq = x ** 0.5
n = sq - int(sq)
if n >0:
return False
return True
print filter(is_sqr,[i ** 2 for i in range(1, 11)])
def is_sqr(x):
sq = x ** 0.5
n = sq - int(sq)
if n >0:
return False
return True
print filter(is_sqr,[i ** 2 for i in range(1, 11)])
2019-05-13
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2019-05-13
# -*- coding:utf-8 -*-
class Person(object):
def __init__(self, name, score):
self.name=name
self.score=score
def get_grade(self):
if self.score<60:
return 'C不合格'
elif self.score<90:
return 'B合格'
else:
return 'A优秀'
class Person(object):
def __init__(self, name, score):
self.name=name
self.score=score
def get_grade(self):
if self.score<60:
return 'C不合格'
elif self.score<90:
return 'B合格'
else:
return 'A优秀'
2019-05-05
def count():
fs = []
for i in range(1, 4):
def f(n=i):
return n*n
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(n=i):
return n*n
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2019-05-04
def count():
fs = []
for i in range(1, 4):
def f(i):
return lambda : i**2
fs.append(f(i))
return fs
for j in count():
print(j())
fs = []
for i in range(1, 4):
def f(i):
return lambda : i**2
fs.append(f(i))
return fs
for j in count():
print(j())
2019-05-04
try:
import simplejson as json
except ImportError:
import json as json
print json.dumps({'python':2.7})
import simplejson as json
except ImportError:
import json as json
print json.dumps({'python':2.7})
2019-05-02
def cmp_ignore_case(x, y):
if x.upper() > y.upper():
return 1
if x.upper() < y.upper():
return -1
return 0
def iterable(x, base=cmp_ignore_case):
return sorted(x, base)
sorted_ignore_case = functools.partial(iterable)
if x.upper() > y.upper():
return 1
if x.upper() < y.upper():
return -1
return 0
def iterable(x, base=cmp_ignore_case):
return sorted(x, base)
sorted_ignore_case = functools.partial(iterable)
2019-05-02
def wrapper(*args, **kw):
if unit=='ms':
print 'call',f.__name__ + '() in', long(1000*time.time()), 'ms'
else:
print 'call',f.__name__ + '() in', long(time.time()), 's'
return f(*args, **kw)
if unit=='ms':
print 'call',f.__name__ + '() in', long(1000*time.time()), 'ms'
else:
print 'call',f.__name__ + '() in', long(time.time()), 's'
return f(*args, **kw)
2019-05-02
import time
def performance(f):
def fn(x):
print 'call',f.__name__ + '() in', time.time()
return f(x)
return fn
def performance(f):
def fn(x):
print 'call',f.__name__ + '() in', time.time()
return f(x)
return fn
2019-05-02
def count():
fs = []
def f(x):
return x*x
for i in range(1, 4):
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1, f2, f3
fs = []
def f(x):
return x*x
for i in range(1, 4):
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1, f2, f3
2019-05-02
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-05-01
def format_name(s):
return s[0].upper()+s[1:len(s)].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:len(s)].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2019-05-01
import math
print(filter(lambda x : math.sqrt(x)%1==0, range(1,101)))
print(filter(lambda x : math.sqrt(x)%1==0, range(1,101)))
2019-04-29