import math
def is_sqr(x):
return math.sqrt(x) % 1 == 0
list1 = filter(is_sqr, range(1, 101))
for item in list1:
print(item)
def is_sqr(x):
return math.sqrt(x) % 1 == 0
list1 = filter(is_sqr, range(1, 101))
for item in list1:
print(item)
2016-06-28
from _functools import reduce
def prod(x, y):
return x * y
print(reduce(prod,[2, 4, 5, 7, 12]))
def prod(x, y):
return x * y
print(reduce(prod,[2, 4, 5, 7, 12]))
2016-06-28
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))
2016-06-28
# coding=utf-8
class Person(object):
def __init__(self, name, score):
self.name = name
self._score = score
def get_grade(self):
if self._score >= 90:
print 'A-优秀'
elif self._score >= 60:
print 'B-及格'
else:
print 'C-不
class Person(object):
def __init__(self, name, score):
self.name = name
self._score = score
def get_grade(self):
if self._score >= 90:
print 'A-优秀'
elif self._score >= 60:
print 'B-及格'
else:
print 'C-不
2016-06-27
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = 'score '
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print AttributeError
def __init__(self, name, score):
self.name = name
self.__score = 'score '
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print AttributeError
2016-06-27
已采纳回答 / _且行且思
python3中用新函数(lt、le、eq、ne、ge、gt)替换python2中cmp函数,具体用法参考此篇文章。http://blog.csdn.net/sushengmiyan/article/details/11332589
2016-06-26
不好意思 應該先算出來後轉int再減去最初的時間的- -
def performance(unit):
def cget(fc):
def gtime(*args, **kw):
if unit == 'ms':
lt = int(round(time.time()*1000))
fv = fc(*args, **kw)
print 'call %s() in %d ms' % (fc.__name__, int(round(time.time()*1000))-lt)
return fv
return gtime
return cget
def performance(unit):
def cget(fc):
def gtime(*args, **kw):
if unit == 'ms':
lt = int(round(time.time()*1000))
fv = fc(*args, **kw)
print 'call %s() in %d ms' % (fc.__name__, int(round(time.time()*1000))-lt)
return fv
return gtime
return cget
2016-06-25
def performance(unit):
def cget(func):
def gtime(*args, **kw):
if unit == 'ms':
lt = int(round(time.time()*1000))
fv = func(*args, **kw)
print 'call %s() in %d ms' % (func.__name__, int(round(time.time()*1000-lt)))
return fv
return gtime
return cget
def cget(func):
def gtime(*args, **kw):
if unit == 'ms':
lt = int(round(time.time()*1000))
fv = func(*args, **kw)
print 'call %s() in %d ms' % (func.__name__, int(round(time.time()*1000-lt)))
return fv
return gtime
return cget
2016-06-25