sum = 0
x = 1
n = 1
while True:
x = 2**(n-1)
n = n + 1
sum = sum + x
if n > 20:
break
print sum
x = 1
n = 1
while True:
x = 2**(n-1)
n = n + 1
sum = sum + x
if n > 20:
break
print sum
2018-08-04
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()]
2018-08-04
# -*- coding: utf-8 -*-
print '''静夜思\n\n床前明月光,\n\n疑是地上霜。\n\n举头望明月,\n\n低头思故乡。'''
print '''静夜思\n\n床前明月光,\n\n疑是地上霜。\n\n举头望明月,\n\n低头思故乡。'''
2018-08-04
其实sum也是一个函数直接return float(sum(args))/len(args)即可 不过也要判断初始条件当len(args)==0的情况
2018-08-04
def average(*args):
if len(args)==0 :
return 0.0
n=float(len(args))
return sum(args)/n
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if len(args)==0 :
return 0.0
n=float(len(args))
return sum(args)/n
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2018-08-04
def square_of_sum(L):
z=[]
for n in L:
z.append(n*n)
return sum(z)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
z=[]
for n in L:
z.append(n*n)
return sum(z)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2018-08-04
# -*- coding: utf-8 -*-
print '静夜思\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。'
print '静夜思\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。'
2018-08-04
print [x*100+y*10+z for x in range(1,10) for y in range(0,10) for z in range(0,10) if x==z]
2018-08-03
def average(*args):
ss=0.0
if len(args)==0:
return ss;
for c in args:
ss=ss+c
return ss/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
ss=0.0
if len(args)==0:
return ss;
for c in args:
ss=ss+c
return ss/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2018-08-03
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for score in d.values():
sum+=score
print sum/len(d)
sum = 0.0
for score in d.values():
sum+=score
print sum/len(d)
2018-08-03