短路运算
a and b ,如果a为假,结果为假,返回a;a为真,运算结果取决于b,返回b
a or b ,如果a为真,结果确定,返回a;a为假,运算结果取决于b,返回b
a and b ,如果a为假,结果为假,返回a;a为真,运算结果取决于b,返回b
a or b ,如果a为真,结果确定,返回a;a为假,运算结果取决于b,返回b
2015-04-14
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for i in L:
if i in s:
s.remove(i)
else:
s.add(i)
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for i in L:
if i in s:
s.remove(i)
else:
s.add(i)
print s
2015-04-14
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-04-14
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key+':',d[key]
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key+':',d[key]
2015-04-14
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break
if not x%2:
continue
sum=sum+x
print sum
x = 0
while True:
x = x + 1
if x > 100:
break
if not x%2:
continue
sum=sum+x
print sum
2015-04-14
L = ['Adam', 'Lisa', 'Bart']
temp = L[0]
L[0] =L[-1]
L[-1]=temp
print L
temp = L[0]
L[0] =L[-1]
L[-1]=temp
print L
2015-04-14
最优代码
for x in range(1,10):
for y in range(2,10):
if x < y:
print x*10 + y
for x in range(1,10):
for y in range(2,10):
if x < y:
print x*10 + y
2015-04-13
dict 中的 key和value 如果其中key 或者value 为字符串则要加上 '' 号 如果是数字或者字符则不需要加上 '' 号
2015-04-13
def average(*args):
if sum(args)!=0:
return float(sum(args))/len(args)
else:
return float(sum(args))
if sum(args)!=0:
return float(sum(args))/len(args)
else:
return float(sum(args))
2015-04-13
def generate_tr(name, score):
return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
tds = [generate_tr(name,score) for name, score in d.iteritems()]
return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
tds = [generate_tr(name,score) for name, score in d.iteritems()]
2015-04-13