def average(*args):
sum = 0.0
n = len(args)
if n ==0:
return sum
for i in args:
sum +=i
avg = sum/len(args)
return avg
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
sum = 0.0
n = len(args)
if n ==0:
return sum
for i in args:
sum +=i
avg = sum/len(args)
return avg
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2015-05-17
def square_of_sum(L):
sum = 0
for x in L:
sum += x**2
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**2
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-05-17
print [a*100+b*10+c for a in range(1,10) for b in range(0,10) for c in range(1,10) if a==c]
2015-05-17
print 'Adam:',d['Adam']
print 'Lisa:',d['Lisa']
print 'Bart:',d['Bart']
print 'Lisa:',d['Lisa']
print 'Bart:',d['Bart']
2015-05-17
for key,value in d.items():
print key,':',value
#结果为#lisa : 85
#adam : 95
#bart : 59
并不是初始化序列,因为dic是无序的
可以把key和value分开成list,然后zip(key,value)
keys = ["adam", "lisa", "bart"]
values = ["95", "85", "59"]
for key,value in zip(keys, values):
print key,":",value
print key,':',value
#结果为#lisa : 85
#adam : 95
#bart : 59
并不是初始化序列,因为dic是无序的
可以把key和value分开成list,然后zip(key,value)
keys = ["adam", "lisa", "bart"]
values = ["95", "85", "59"]
for key,value in zip(keys, values):
print key,":",value
2015-05-16
for x in xrange(10):
for y in xrange(10):
if x>0 and x<y:
print x*10+y
for y in xrange(10):
if x>0 and x<y:
print x*10+y
2015-05-16
for x in ['1','2','3','4','5','6','7','8','9']:
for y in ['2','3','4','5','6','7','8','9']:
if x<y:
print x+y
for y in ['2','3','4','5','6','7','8','9']:
if x<y:
print x+y
2015-05-16