s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for name in L:
if name in s:
s.remove(name)
else:
s.add(name)
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for name in L:
if name in s:
s.remove(name)
else:
s.add(name)
print s
2015-06-11
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]
2015-06-11
months = set(['Jan','Feb','Mar','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec'])
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'
2015-06-11
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-06-11
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for i in d.itervalues():
sum = sum + i
print sum/len(d.values())
sum = 0.0
for i in d.itervalues():
sum = sum + i
print sum/len(d.values())
2015-06-11
1. a = 'python' 属于非空字符,属于true,所以当 print "hello,",a or "world",输出则为a 结果(hello,pypthon)
2. b = '' 属于空字符(null,0,空字符),所以为false, 结果当 print "hello,",b or "world",输出则为b,结果(hello,world)
2. b = '' 属于空字符(null,0,空字符),所以为false, 结果当 print "hello,",b or "world",输出则为b,结果(hello,world)
2015-06-10
def square_of_sum(L):
lst=[]
for i in L:
lst.append(i*i)
return sum(lst)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
lst=[]
for i in L:
lst.append(i*i)
return sum(lst)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-06-10