sum = 0
x = 1
while x < 100 :
sum += x
x = x + 2
print sum
# print ( 1 + 99 ) * 50 / 2,验证结果
x = 1
while x < 100 :
sum += x
x = x + 2
print sum
# print ( 1 + 99 ) * 50 / 2,验证结果
2019-12-06
sum = 0
x = 1
n = 1
while True:
if n > 20:
break
sum+=x
x*=2
n+=1
print sum
x = 1
n = 1
while True:
if n > 20:
break
sum+=x
x*=2
n+=1
print sum
2019-12-03
print [x*100+y*10+z for x in range(1,10) for y in range(0,10) for z in range(0,10) if(x==z)]
2019-12-01
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for i in d.itervalues():
sum+=i
print sum/len(d)
sum = 0.0
for i in d.itervalues():
sum+=i
print sum/len(d)
2019-11-30
def square_of_sum(L):
sum = 0
ll = len(L)
for item in L:
sum += item*item
while item == L[ll-1]:
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum = 0
ll = len(L)
for item in L:
sum += item*item
while item == L[ll-1]:
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2019-11-28
import math
def quadratic_equation(a, b, c):
fm=2*a
dt=b*b-4*a*c
if dt<0:
return
fz1=-b+math.sqrt(dt)
fz2=-b-math.sqrt(dt)
x1=fz1/fm
x2=fz2/fm
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
fm=2*a
dt=b*b-4*a*c
if dt<0:
return
fz1=-b+math.sqrt(dt)
fz2=-b-math.sqrt(dt)
x1=fz1/fm
x2=fz2/fm
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2019-11-26
也可以这么写的,不过可能要用到后面的知识点了,我是学完后面的再来看前面的,有了不一样的思路。
L = [i*i for i in range(1,101)]
print sum(L)
L = [i*i for i in range(1,101)]
print sum(L)
2019-11-26
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
#print key,':',d[key](正确)
print key+':',d[key]
# 注意:+仅限字符串与字符串的连接
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
#print key,':',d[key](正确)
print key+':',d[key]
# 注意:+仅限字符串与字符串的连接
2019-11-25
def square_of_sum(L):
sum = 0
for a in L:
a = a * a
sum = sum + a
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
这样不行么
sum = 0
for a in L:
a = a * a
sum = sum + a
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
这样不行么
2019-11-25
sum = 0
x = 1
n = 1
while True:
sum=sum + x
x=x*2
n=n+1
if n > 20:
break
print sum
x = 1
n = 1
while True:
sum=sum + x
x=x*2
n=n+1
if n > 20:
break
print sum
2019-11-25
当n=3,move(n-1, a, c, b) 回溯两次
n=2;此时为m(2,A,C,B),再调用
move(n-1, a, c, b) 将a1从A到C
move(1, a, b, c) 将的a2从A到B
move(n-1, b, a, c) 将a1从C到B
退到上层
move(1, a, b, c) 将A最后一个移到C
move(n-1, b, a, c) 也回溯两次
n=2,此时为 m(2, A, B, C),再次调用
move(n-1, a, c, b) 将a1从 B到 A
move(1, a, c, b) 将a1从 B到 C
move(n-1, b, a, c) 将a1从 A到 C
n=2;此时为m(2,A,C,B),再调用
move(n-1, a, c, b) 将a1从A到C
move(1, a, b, c) 将的a2从A到B
move(n-1, b, a, c) 将a1从C到B
退到上层
move(1, a, b, c) 将A最后一个移到C
move(n-1, b, a, c) 也回溯两次
n=2,此时为 m(2, A, B, C),再次调用
move(n-1, a, c, b) 将a1从 B到 A
move(1, a, c, b) 将a1从 B到 C
move(n-1, b, a, c) 将a1从 A到 C
2019-11-24