觉得这个也是对的。 print [str(x)+str(y)+str(x) for x in range (1,10) for y in range(0,10)]
print [100*x+10*y+z for x in range (1,10) for y in range(0,10) for z in range(0,10) if x==z]
print [100*x+10*y+z for x in range (1,10) for y in range(0,10) for z in range(0,10) if x==z]
2015-06-16
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
def generate_tr(name, score):
if score<60:
return'<tr><td>%s</td><td style="color:red">%s</td></tr>'%(name,score)
return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)
tds = [generate_tr(name,score) for name, score in d.iteritems()]
def generate_tr(name, score):
if score<60:
return'<tr><td>%s</td><td style="color:red">%s</td></tr>'%(name,score)
return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)
tds = [generate_tr(name,score) for name, score in d.iteritems()]
2015-06-16
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for k, v in d.iteritems():
sum = sum + v
print k,":",v
print 'average', ':', sum/len(d)
sum = 0.0
for k, v in d.iteritems():
sum = sum + v
print k,":",v
print 'average', ':', sum/len(d)
2015-06-16
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for i in d.itervalues():
sum = sum + i
print sum/len(d.values())
sum = 0.0
for i in d.itervalues():
sum = sum + i
print sum/len(d.values())
2015-06-16
def firstCharUpper(s):
s=s[0].upper()+s[1:]
return s
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
s=s[0].upper()+s[1:]
return s
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
2015-06-15
def average(*args):
sum =0.0
if len(args) ==0:
return sum
for x in args:
sum = sum +x
return sum/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
sum =0.0
if len(args) ==0:
return sum
for x in args:
sum = sum +x
return sum/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2015-06-15
def square_of_sum(L):
sum =0
for x in L:
sum = x*x+sum
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum =0
for x in L:
sum = x*x+sum
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-06-15
因为 if当age=20时, age >= 6: 一样是成立的,而条件判断是从上至下的,因此执行到print 'teenager'已经跳出了……
解决办法,1、倒叙排列,先执行age >= 18
2、将判断语句多做条件限制age >= 6 and <18:
解决办法,1、倒叙排列,先执行age >= 18
2、将判断语句多做条件限制age >= 6 and <18:
2015-06-14
把list → ['A','B'] 换成
tuple → ('A','B')
tuple → ('A','B')
2015-06-14