import math
def quadratic_equation(a, b, c):
delta = b**2 - 4*a*c
if delta < 0:
return None
elif delta == 0:
return -b/2*a
return (-b+math.sqrt(delta))/(2*a), (-b-math.sqrt(delta))/(2*a)
print(quadratic_equation(2, 3, 0))
print(quadratic_equation(1, -6, 5))
def quadratic_equation(a, b, c):
delta = b**2 - 4*a*c
if delta < 0:
return None
elif delta == 0:
return -b/2*a
return (-b+math.sqrt(delta))/(2*a), (-b-math.sqrt(delta))/(2*a)
print(quadratic_equation(2, 3, 0))
print(quadratic_equation(1, -6, 5))
2018-12-29
def square_of_sum(L):
arr = []
for item in L:
arr.append(item*item)
return sum(arr)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
arr = []
for item in L:
arr.append(item*item)
return sum(arr)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2018-12-29
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for item in L:
if item in s:
s.remove(item)
else:
s.add(item)
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for item in L:
if item in s:
s.remove(item)
else:
s.add(item)
print s
2018-12-29
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]
2018-12-29
months = set(['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','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'
2018-12-29
s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
print s
print s
2018-12-29
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for (k,v) in d.iteritems():
print k,':',v
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for (k,v) in d.iteritems():
print k,':',v
2018-12-29
答案中给出的print key+':',d[key]应该改成print key,':',d[key]。
2018-12-29
for x in range(1,10):
for y in range( x+1 , 10 ):
print x*10+y,'\r'
for y in range( x+1 , 10 ):
print x*10+y,'\r'
2018-12-29
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break
if x%2==0:
continue
sum+=x
print sum
x = 0
while True:
x = x + 1
if x > 100:
break
if x%2==0:
continue
sum+=x
print sum
2018-12-29
sum = 0
x = 1
n = 1
while True:
sum+=x
x*=2
n+=1
if n>20:
break
print sum
x = 1
n = 1
while True:
sum+=x
x*=2
n+=1
if n>20:
break
print sum
2018-12-29
L = ['Adam', 'Lisa', 'Bart']
L.reverse()
print L
L.reverse()
print L
2018-12-29