move方法负责将A中的移动到C,n=4,那么首先将A中n-1个圆盘转移到B,用move方法就是move(4-1,'A','C','B'),这个执行之后就把A中的一个圆盘移动到C,print a,'-->',c,最后把B中的n-1个圆盘移动到C,就是move(n-1,'B','A','C')
2015-12-02
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-12-02
# values() 方法把 dict 转换成了包含 value 的list
# itervalues() 方法不会转换,它会在迭代过程中依次从 dict 中取出 value
# 所以 itervalues() 方法比 values() 方法节省了生成 list 所需的内存
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for n in d.itervalues():
sum += n
print sum/len(d)
# itervalues() 方法不会转换,它会在迭代过程中依次从 dict 中取出 value
# 所以 itervalues() 方法比 values() 方法节省了生成 list 所需的内存
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for n in d.itervalues():
sum += n
print sum/len(d)
2015-12-02
def average(*args):
return sum(args) and float(sum(args))/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
return sum(args) and float(sum(args))/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2015-12-02
def square_of_sum(L):
sum = 0
for i in L:
sum += i*i
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum = 0
for i in L:
sum += i*i
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-12-02
t = ('a', 'b', 'A', 'B')
print t
print t
2015-12-01
sum = 0
x = 1
n = 1
while True:
sum += x
x *= 2
n += 1
if n == 21:
break
print sum
#if n ==21 这里第一次写成 if n =21 ,罪过罪过
x = 1
n = 1
while True:
sum += x
x *= 2
n += 1
if n == 21:
break
print sum
#if n ==21 这里第一次写成 if n =21 ,罪过罪过
2015-12-01
L = [75, 92, 59, 68]
sum = 0.0
for item in L:
sum += item
print sum / 4
sum = 0.0
for item in L:
sum += item
print sum / 4
2015-12-01
L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(2)
L.pop(2)
print L
L.pop(2)
L.pop(2)
print L
2015-12-01
L = ['Adam', 'Lisa', 'Bart']
L.insert(2,'Paul')
print L
L.insert(2,'Paul')
print L
2015-12-01