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'])
2016-08-07
def cmp_ignore_case(s1, s2):
return cmp(s1.lower(), s2.lower())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return cmp(s1.lower(), s2.lower())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-08-07
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
return 'A' if (self.__score > 80) else 'C' if (self.__score < 60) else 'B'
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
return 'A' if (self.__score > 80) else 'C' if (self.__score < 60) else 'B'
2016-08-07
再按照答案的运行后结果是:
call factorial() in 0.001000s
3628800
与通过需要的相差了 0.001000s
所以就把函数里的输出改成:
print 'call %s() in' % (f.__name__)
就可以了
在python 3.0运行时需要稍作修改
call factorial() in 0.001000s
3628800
与通过需要的相差了 0.001000s
所以就把函数里的输出改成:
print 'call %s() in' % (f.__name__)
就可以了
在python 3.0运行时需要稍作修改
2016-08-07
可能是想让学生学会异常处理的思想吧
class Person(object):
__count = 0
def __init__(self, name):
Person.__count=Person.__count+1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except:
print "attributeerror"
class Person(object):
__count = 0
def __init__(self, name):
Person.__count=Person.__count+1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except:
print "attributeerror"
2016-08-06
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 =L1[1].name+L1[0].name+L1[2].name
print L1[0].name
print L1[1].name
print L1[2].name
print L2
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 =L1[1].name+L1[0].name+L1[2].name
print L1[0].name
print L1[1].name
print L1[2].name
print L2
2016-08-06
def format_name(s):
s=s.lower().capitalize()
return s
print map(format_name, ['adam', 'LISA', 'barT'])
s=s.lower().capitalize()
return s
print map(format_name, ['adam', 'LISA', 'barT'])
2016-08-05
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
print count()
f1,f2,f3 = count()
print f1
print f2
print f3
其实就是f 与 f() 的区别,f 是一个函数, f() 是一个函数的结果
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
print count()
f1,f2,f3 = count()
print f1
print f2
print f3
其实就是f 与 f() 的区别,f 是一个函数, f() 是一个函数的结果
2016-08-05
def format_name(s):
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-08-05