求平均值,就是总和除以数量,精度为1位小数
用求和函数sum以及查询元素数量函数len,然后求和的值*1.0转换精度,再除以数量。此时在len为0时会报错,所以在此之前,使用if把0的情况单独返回一个值,就可以了。
def average(*args):
if len(args)==0:
return 0.0
return sum(args)*1.0 / len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
用求和函数sum以及查询元素数量函数len,然后求和的值*1.0转换精度,再除以数量。此时在len为0时会报错,所以在此之前,使用if把0的情况单独返回一个值,就可以了。
def average(*args):
if len(args)==0:
return 0.0
return sum(args)*1.0 / len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2019-11-15
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for name in L:
if name in s:
s.remove(name)
print 'remove:',name
else:
s.add(name)
print 'add:',name
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for name in L:
if name in s:
s.remove(name)
print 'remove:',name
else:
s.add(name)
print 'add:',name
print s
2019-11-13
for x in [ 1,2,3,4,5,6,7,8,9 ]:
for y in [ 0,1,2,3,4,5,6,7,8,9 ]:
# print str(x) +"---"+ str(y)
if x < y:
print (str(x) + str(y))
for y in [ 0,1,2,3,4,5,6,7,8,9 ]:
# print str(x) +"---"+ str(y)
if x < y:
print (str(x) + str(y))
2019-11-13
age = 20
if age >= 6 and age <18:
print 'teenager'
elif age >= 18:
print 'adult'
else:
print 'kid'
if age >= 6 and age <18:
print 'teenager'
elif age >= 18:
print 'adult'
else:
print 'kid'
2019-11-12
def average(*args):
if len(args)==0:
return 0.0
else:
return (sum(args)*1.0/len(args))
print (average())
print (average(1, 2))
print (average(1, 2, 2, 3, 4))
if len(args)==0:
return 0.0
else:
return (sum(args)*1.0/len(args))
print (average())
print (average(1, 2))
print (average(1, 2, 2, 3, 4))
2019-11-11
print [int(m+n+m) for m in '123456789' for n in '0123456789']
2019-11-10
L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(2)
L.pop(2)
print L
和
L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(3)
L.pop(2)
print L
L.pop(2)
L.pop(2)
print L
和
L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(3)
L.pop(2)
print L
2019-11-07
Python次方表示是“**”
sum = 0
x = 1
n = 1
while True:
sum +=x
n = n +1
x=2**(n-1)
if n > 20:
break
print sum
sum = 0
x = 1
n = 1
while True:
sum +=x
n = n +1
x=2**(n-1)
if n > 20:
break
print sum
2019-11-07
L = [1]
for x in L:
if x < 100:
L.append(x+1)
for y in L:
if y >= 10 and (y/10)%10 < y%10:
print y
了解一下 先拿到1-100的集合再开始进行筛选。
for x in L:
if x < 100:
L.append(x+1)
for y in L:
if y >= 10 and (y/10)%10 < y%10:
print y
了解一下 先拿到1-100的集合再开始进行筛选。
2019-11-07