1.例子
计算用户输入的一组数字的平均值
# average.py
def main():
total = 0.0 #float
count = 0 #int
xStr = input('Enter a number (Enter to quit) >> ') # input string
while xStr != '':
x = float(xStr) # str2f
total = total + x
count = count + 1
xStr = input('Enter a number (Enter to quit) >> ') # input string
print('\nThe average of the numbers is, ' total/count) # print
main()
2.计算标准差
标准差的公式:
在该公式中, x 是平均值,xi 表示第i 个数据值,n 是数据值的数量。
3.应用列表
@ 交互示例
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> "This is an ex-parrot!".split()
[’This’, ’is’, ’an’, ’ex-parrot!’]