python求奇数和平均值
时间: 2023-10-14 12:22:50 浏览: 91
求值_python_方差_中位数_平均值_
5星 · 资源好评率100%
可以使用以下代码来求解奇数的和及平均值:
```python
numbers = [] # 创建一个空列表用于存储奇数
sum_of_odd_numbers = 0 # 初始化奇数的和
count_of_odd_numbers = 0 # 初始化奇数的个数
# 通过循环求解奇数的和及个数
for i in range(1, 101, 2): # 遍历1到100之间的奇数
numbers.append(i) # 将奇数添加到列表中
sum_of_odd_numbers += i # 累加奇数的和
count_of_odd_numbers += 1 # 奇数个数加1
# 计算平均值
average_of_odd_numbers = sum_of_odd_numbers / count_of_odd_numbers
print("奇数列表:", numbers)
print("奇数的和:", sum_of_odd_numbers)
print("奇数的平均值:", average_of_odd_numbers)
```
输出结果为:
```
奇数列表: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
奇数的和: 2500
奇数的平均值: 50.0
```
阅读全文