print [n1*100 + n2*10 + n3 for n1 in range(1,10) for n2 in range(0,10) for n3 in range(0,10) if n1==n3]
2015-02-06
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-02-06
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for v in d.itervalues():
sum += v
print sum / len(d)
sum = 0.0
for v in d.itervalues():
sum += v
print sum / len(d)
2015-02-06
def firstCharUpper(s):
return s[0].upper()+s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
return s[0].upper()+s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
2015-02-06
额,搞错了。。这个是对的:
L = range(1, 101)
print L[:10]
print L[2::3]
print L[4:50:5]
L = range(1, 101)
print L[:10]
print L[2::3]
print L[4:50:5]
2015-02-06
L = range(1, 101)
print L
print L[:10]
print L[::3]
print L[:50:5]
print L
print L[:10]
print L[::3]
print L[:50:5]
2015-02-06
def average(*args):
if len(args) == 0:
return 0.0
else:
sum = 0.0
for i in args:
sum += i
return sum / len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if len(args) == 0:
return 0.0
else:
sum = 0.0
for i in args:
sum += i
return sum / len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2015-02-06
def move(n, a, b, c):
if n == 1:
print a, '-->', c
else:
move(n-1,a,c,b)
print a, '-->', c
move(n-1,b,a,c)
move(4, 'A', 'B', 'C')
if n == 1:
print a, '-->', c
else:
move(n-1,a,c,b)
print a, '-->', c
move(n-1,b,a,c)
move(4, 'A', 'B', 'C')
2015-02-06
def square_of_sum(L):
sum = 0.0
for v in L:
sum += v * v
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum = 0.0
for v in L:
sum += v * v
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-02-06