d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
def generate_tr(name, score):
return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
tds = ['<tr><td>%s</td><td>%s</td></tr>' % (name, score) for name, score in d.iteritems()]
print '<table border="1">'
......
def generate_tr(name, score):
return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
tds = ['<tr><td>%s</td><td>%s</td></tr>' % (name, score) for name, score in d.iteritems()]
print '<table border="1">'
......
2018-07-09
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for v in d.values():
sum += v
print sum/len(d)
sum = 0.0
for v in d.values():
sum += v
print sum/len(d)
2018-07-09
def firstCharUpper(s):
return s.upper()[:1]+s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
return s.upper()[:1]+s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
2018-07-09
def average(*args):
sum = 0.0
aver = 0.0
if len(args)==0:
return sum
else :
for i in args:
sum += i
aver = sum / len(args)
return aver
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
sum = 0.0
aver = 0.0
if len(args)==0:
return sum
else :
for i in args:
sum += i
aver = sum / len(args)
return aver
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2018-07-09
def greet(name='world'):
print 'Hello,',name,'.'
greet()
greet('Bart')
print 'Hello,',name,'.'
greet()
greet('Bart')
2018-07-09
import math
def quadratic_equation(a, b, c):
return (-b+math.sqrt(b*b-4*a*c))/(2*a),(-b-math.sqrt(b*b-4*a*c))/(2*a)
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
return (-b+math.sqrt(b*b-4*a*c))/(2*a),(-b-math.sqrt(b*b-4*a*c))/(2*a)
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2018-07-09
def square_of_sum(L):
s=0
for i in L:
s=s+i*i
return s
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
s=0
for i in L:
s=s+i*i
return s
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2018-07-09
L = []
x = 0
while True:
x = x + 1
if x > 100:
break
else:
L.append(x * x)
print sum(L)
x = 0
while True:
x = x + 1
if x > 100:
break
else:
L.append(x * x)
print sum(L)
2018-07-09
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print x[0],':',x[1]
for x in s:
print x[0],':',x[1]
2018-07-09
months = set(['Jan', 'Feb'])
x1 = 'Feb'
x2 = 'Sun'
if x1 in months:
print 'x1: ok'
else:
print 'x1: error'
if x2 in months:
print 'x2: ok'
else:
print 'x2: error'
x1 = 'Feb'
x2 = 'Sun'
if x1 in months:
print 'x1: ok'
else:
print 'x1: error'
if x2 in months:
print 'x2: ok'
else:
print 'x2: error'
2018-07-09
s = set(['adam','bart'])
print 'adam' in s
print 'bart' in s
print 'adam' in s
print 'bart' in s
2018-07-09
s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
2018-07-09