s = set(['Adam', 'Lisa', 'BArt', 'Paul'])
s1 = set([])
for key in s:
key = key.lower()
s1.add(key)
name = 'aDam'
name = name.lower()
print name in s1
这样大小写都可以兼容了
s1 = set([])
for key in s:
key = key.lower()
s1.add(key)
name = 'aDam'
name = name.lower()
print name in s1
这样大小写都可以兼容了
2018-04-08
L = range(1, 101)
print L[:10]
print L[2:-1:3]
print L[4:50:5]
print L[:10]
print L[2:-1:3]
print L[4:50:5]
2018-04-08
def greet(s='world'):
print 'Hello,%s.'%s
greet()
greet('Bart')
print 'Hello,%s.'%s
greet()
greet('Bart')
2018-04-08
def square_of_sum(L):
return sum([x*x for x in L])
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
return sum([x*x for x in L])
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2018-04-08
def square_of_sum(L):
sum = 0
for x in L:
sum = sum + x * x
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 = sum + x * x
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2018-04-08
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print '%s:%d'%(x[0],x[1])
for x in s:
print '%s:%d'%(x[0],x[1])
2018-04-08
print [int(str(x)+str(y)+str(z)) for x in range(1,10) for y in range(0,10) for z in range(1,10) if x==z]
2018-04-08
d1 = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
d = defaultdict(str)
for k,v in d1.items():
d[v]=k
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
d = defaultdict(str)
for k,v in d1.items():
d[v]=k
2018-04-08
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for k, v in d.items():
sum = sum + v
print k,':',v
print 'average', ':', sum/len(d)
sum = 0.0
for k, v in d.items():
sum = sum + v
print k,':',v
print 'average', ':', sum/len(d)
2018-04-08