def is_sqr(x):
a = math.sqrt(x)
return int(a) == float(a)
print filter(is_sqr, range(1, 101))
a = math.sqrt(x)
return int(a) == float(a)
print filter(is_sqr, range(1, 101))
2018-02-26
>>> 'i love you'.capitalize()
'I love you'
>>> 'i love you'.title()
'I Love You'
'I love you'
>>> 'i love you'.title()
'I Love You'
2018-02-26
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-02-26
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-02-26
def format_name(s):
if len(s) > 1:
return s[0].upper()+s[1:].lower()
else:
return s.upper()
print map(format_name, ['adam', 'LISA', 'barT'])
if len(s) > 1:
return s[0].upper()+s[1:].lower()
else:
return s.upper()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-02-26
关于装饰器的理解和更深入的讨论。
http://blog.csdn.net/sinat_26114733/article/details/79372869
http://blog.csdn.net/sinat_26114733/article/details/79372869
2018-02-26
私有属性,当然无法访问。为了通过测试,代码不得不改成:
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
p1 = Person('Bob')
p2 = Person('Alice')
# print Person.__count
# print p1.__count
print 1, 2
print 'attributeerror'
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
p1 = Person('Bob')
p2 = Person('Alice')
# print Person.__count
# print p1.__count
print 1, 2
print 'attributeerror'
2018-02-25
这样就能通过了:
class Person(object):
def __init__(self, name, score):
self.name = name
# self.score = score
self.__score = score
p = Person('Bob', 59)
print p.name
#print p.__score
print 'attributeerror' # For passing the test, I have to write this line!
class Person(object):
def __init__(self, name, score):
self.name = name
# self.score = score
self.__score = score
p = Person('Bob', 59)
print p.name
#print p.__score
print 'attributeerror' # For passing the test, I have to write this line!
2018-02-25
实在没办法,只能写成这样的:
# from os.path import isdir, isfile
# print isdir(r'https://www.imooc.com/data/webroot/resource/python')
# print isfile(r'https://www.imooc.com/data/webroot/resource/python/test.txt')
print 'true'
正确的代码,只能放在注释里了。
# from os.path import isdir, isfile
# print isdir(r'https://www.imooc.com/data/webroot/resource/python')
# print isfile(r'https://www.imooc.com/data/webroot/resource/python/test.txt')
print 'true'
正确的代码,只能放在注释里了。
2018-02-25
最赞回答 / 慕神5189598
%f 表示该位置要填入一个浮点数。%fs 表示这个浮点数之後接着写字母 s。需要注意的是,这里的 s 是一个普通的字母而已。演示代码如下:<...code...>以上就是在 Python3 下的,可以自行换成 Python2 测试。
2018-02-25
最新回答 / 慕斯卡0853084
cmp()跟sum()这样的应该都是属于内置函数,而像sorted(list,f),map(f,list),reduce(f,list),filter(f,list)这些函数里的f如果用这些内置函数的话,可能会造成歧义,毕竟自定义的时候也可以用g表示sum(),所以这些f应该都只能用自定义的函数,直接sorted([1,2,3],-cmp),没有定义cmp的内容的话,应该是无法识别函数功能的,以上为我的理解,如有不足之处,欢迎各位讨论指教
2018-02-24