def square_of_sum(L):
s=0
for n in L:
s += n*n
return s
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
s=0
for n in L:
s += n*n
return s
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2018-04-18
for x in [ 1,2,3,4,5,6,7,8,9 ]:
for y in [ 1,2,3,4,5,6,7,8,9 ]:
d=x*10+y
if x<y:
print d
for y in [ 1,2,3,4,5,6,7,8,9 ]:
d=x*10+y
if x<y:
print d
2018-04-18
d = {
'Adam':95,
'Lisa':85,
'Bart':59
}
for x in d:
print(str(x) +str(':')+str(d.get(x)))
'Adam':95,
'Lisa':85,
'Bart':59
}
for x in d:
print(str(x) +str(':')+str(d.get(x)))
2018-04-18
L=[]
for m in range(1,10):
for n in range(0,10):
for i in range(0,10):
if m==i:
L.append(m*100+n*10+i)
print L
for m in range(1,10):
for n in range(0,10):
for i in range(0,10):
if m==i:
L.append(m*100+n*10+i)
print L
2018-04-18
def square_of_sum(L):
n=0
for i in L:
n=i*i+n
return n
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
n=0
for i in L:
n=i*i+n
return n
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2018-04-18
任务要求时利用3层for循环啊
print [a*100+10*b+c for a in range(1, 10) for b in range(0, 10) for c in range(0, 10) if a==c]
print [a*100+10*b+c for a in range(1, 10) for b in range(0, 10) for c in range(0, 10) if a==c]
2018-04-18
L = range(1,101)
for i in range(1,101):
L[i-1]=i*i
print sum(L)
for i in range(1,101):
L[i-1]=i*i
print sum(L)
2018-04-18
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
def generate_tr(name, score):
return ('<tr><td>%s</td><td>%s</td></tr>' % (name, score)) if score>=60 else ('<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score))
tds = [generate_tr(name,score) for name, score in d.iteritems()]
def generate_tr(name, score):
return ('<tr><td>%s</td><td>%s</td></tr>' % (name, score)) if score>=60 else ('<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score))
tds = [generate_tr(name,score) for name, score in d.iteritems()]
2018-04-18
sum = 0
x = 1
while x <= 100:
if x % 2 > 0:
sum += x
x += 1
print sum
x = 1
while x <= 100:
if x % 2 > 0:
sum += x
x += 1
print sum
2018-04-18
sum = 0
x = 1
n = 1
while True:
sum = sum + n
x = x + 1
n = 2**(x-1)
if x > 20:
break
print sum
x = 1
n = 1
while True:
sum = sum + n
x = x + 1
n = 2**(x-1)
if x > 20:
break
print sum
2018-04-18
因为Bart是list中最后一个,所以不需要加上索引
或者改成两个L.pop(2)
或者先删除L.pop(3)再L.pop(2)
或者改成两个L.pop(2)
或者先删除L.pop(3)再L.pop(2)
2018-04-17
解释:
运用了短路计算
在计算 a or b 时,如果 a 是 True,则根据或运算法则,整个计算结果必定为 True,因此返回 a;如果 a 是 False,则整个计算结果必定取决于 b,因此返回 b。
代码中a=python 为true
所以输出的是True,即hello,python
b=''
空字符默认是false
所以输出的是hello,world
运用了短路计算
在计算 a or b 时,如果 a 是 True,则根据或运算法则,整个计算结果必定为 True,因此返回 a;如果 a 是 False,则整个计算结果必定取决于 b,因此返回 b。
代码中a=python 为true
所以输出的是True,即hello,python
b=''
空字符默认是false
所以输出的是hello,world
2018-04-17