因为它给的代码是L.pop(2);L.pop(3).因为上述列表索引只到3.所以当删除掉2的索引时,列表直接变为了到2的索引。要不就先删除第三个索引再删除第二索引。那么直接两个L.pop(2);L.pop(2),或则L.pop(3);L.pop(2)。
2019-10-28
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for name, score in d.items():
sum = sum + score
print name , ':', score
print 'average', ':', sum/len(d)
sum = 0.0
for name, score in d.items():
sum = sum + score
print name , ':', score
print 'average', ':', sum/len(d)
2019-10-28
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for v in d.values():
sum = sum + v
print sum /len(d)
sum = 0.0
for v in d.values():
sum = sum + v
print sum /len(d)
2019-10-28
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for index, name in enumerate(L):
print index+1, '-', name
for index, name in enumerate(L):
print index+1, '-', name
def firstCharUpper(s):
for i in s:
if i==s[0]:
s=s[0].upper()+s[1:]
return s
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
for i in s:
if i==s[0]:
s=s[0].upper()+s[1:]
return s
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
2019-10-28
L = range(1, 101)
for i in L:
i+=i
print L[0:10]
print L[2:101:3]
print L[4:51:5]
for i in L:
i+=i
print L[0:10]
print L[2:101:3]
print L[4:51:5]
2019-10-28
def average(*args):
if len(args)== 0:
return '0.0'
else:
return float(sum(args))/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if len(args)== 0:
return '0.0'
else:
return float(sum(args))/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2019-10-28
#改进一下
import math
def quadratic_equation(a,b,c):
if b**2-4*a*c>=0:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
else:
return '根为空'
import math
def quadratic_equation(a,b,c):
if b**2-4*a*c>=0:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
else:
return '根为空'
2019-10-27
#改进一下
import math
def quadratic_equation(a,b,c):
if b**2-4*a*c>=0:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
else:
return '根为空'
import math
def quadratic_equation(a,b,c):
if b**2-4*a*c>=0:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
else:
return '根为空'
2019-10-27
import math
def quadratic_equation(a, b, c):
x1=(-b+math.sqrt(b**2-a*c*4))/(a*2)
x2=(-b-math.sqrt(b**2-a*c*4))/(a*2)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
x1=(-b+math.sqrt(b**2-a*c*4))/(a*2)
x2=(-b-math.sqrt(b**2-a*c*4))/(a*2)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2019-10-27