十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
嵌套函数这么写的意思是:把函数nake_ave赋值给r,然后调用并执行参数make_ave,返回值是ave,并没有执行ave函数,执行函数要加( )的。
创新互联拥有一支富有激情的企业网站制作团队,在互联网网站建设行业深耕10多年,专业且经验丰富。10多年网站优化营销经验,我们已为成百上千中小企业提供了成都网站设计、成都网站建设、外贸网站建设解决方案,按需制作网站,设计满意,售后服务无忧。所有客户皆提供一年免费网站维护!
此函数只能传数字,不能传列表、元组、字符串等,否则会出现不同类型之间的运算,报错。
# coding = GBK
a =[1,2,3,4,5]
sum=0
b = len(a)
print("这个数组的长度为:",b)
for i in a:
sum =sum +i
print("这个数组之和为:",sum)
print("这个数组平均数为",sum/b)
或
import sys
sum = 0
cnt = 0
f = open('1.txt', 'r')
files = f.readline()
while (files ):
sum = sum + float(files .split(",")[0])
cnt = cnt + 1
files = f.readline()
print(sum / cnt)
f.close()
或者。
#!/usr/bin/env pythonimport timeimport numpy as np
dd = np.random.randint(0, 20, size=(2*1000*1000))t_start = time.clock()avg_sum1 =
0.0BlockOffset = 0 while BlockOffset len(dd):
if dd[BlockOffset + 1] = 10:
avg_sum1 += dd[BlockOffset + 1] * 0.1
else:
avg_sum1 += dd[BlockOffset + 0] * 0.01
BlockOffset += 2print('Avg: ' + str(avg_sum1 / len(dd) / 2)) print('Exe time: ' +
str(time.clock() - t_start))
扩展资料:
python 实现求和、计数、最大最小值、平均值、中位数、标准偏差、百分比。
import sys
class Stats:
def __init__(self, sequence):
# sequence of numbers we will process
# convert all items to floats for numerical processing
self.sequence = [float(item) for item in sequence]
def sum(self):
if len(self.sequence) 1:
return None
else:
return sum(self.sequence)
def count(self):
return len(self.sequence)
def min(self):
if len(self.sequence) 1:
return None
else:
return min(self.sequence)
def max(self):
if len(self.sequence) 1:
return None
else:
return max(self.sequence)
def avg(self):
if len(self.sequence) 1:
return None
else:
return sum(self.sequence) / len(self.sequence)
def median(self):
if len(self.sequence) 1:
return None
else:
self.sequence.sort()
return self.sequence[len(self.sequence) // 2]
def stdev(self):
if len(self.sequence) 1:
return None
else:
avg = self.avg()
sdsq = sum([(i - avg) ** 2 for i in self.sequence])
stdev = (sdsq / (len(self.sequence) - 1)) ** .5
return stdev
def percentile(self, percentile):
if len(self.sequence) 1:
value = None
elif (percentile = 100):
sys.stderr.write('ERROR: percentile must be 100. you supplied: %s\n'% percentile)
value = None
else:
element_idx = int(len(self.sequence) * (percentile / 100.0))
self.sequence.sort()
value = self.sequence[element_idx]
return value
参考资料来源:百度百科-python
h=input("请输入5个人的身高:")
h=h.strip().split()
s=sum(map(float,h))
ave=s/len(h)
print("平均身高:%.1f" %(ave))