def average(*args):
if sum(args)==0:
return 0.0
else:
return sum(args)*1.0/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if sum(args)==0:
return 0.0
else:
return sum(args)*1.0/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2015-07-31
def average(*args):
if sum(args)==0:
return 0
else:
return sum(args)/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if sum(args)==0:
return 0
else:
return sum(args)/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2015-07-31
def greet(a='word'):
print 'Hello,'+a+'.'
greet()
greet('Bart')
print 'Hello,'+a+'.'
greet()
greet('Bart')
2015-07-31
def move(n, a, b, c):
if n==1:
print a,'-->',c
return
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
return
move(n-1,a,c,b)
print a,'-->',c
move(n-1,b,a,c)
move(4, 'A', 'B', 'C')
2015-07-31
由于dict也是集合,len() 函数可以计算任意集合的大小:
>>> len(d)
3
注意: 一个 key-value 算一个,因此,dict大小为3。
>>> len(d)
3
注意: 一个 key-value 算一个,因此,dict大小为3。
2015-07-31
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for x in d.itervalues():
sum += x
print sum / len(d)
sum = 0.0
for x in d.itervalues():
sum += x
print sum / len(d)
2015-07-30
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
c = 0
for a in L:
c = 0
for b in s:
if a == b:
c = 1
if c == 0:
s.add(a)
else : s.remove(a)
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
c = 0
for a in L:
c = 0
for b in s:
if a == b:
c = 1
if c == 0:
s.add(a)
else : s.remove(a)
print s
2015-07-30