编写程序,求列表 s=[9,8,7,3,2,1,55,6]中元素的个数,最大值、最小值、元素之和、平均值。请至少写出三种实现方法。提示:①for循环;②while循环;③list的函数
时间: 2024-05-29 10:10:00 浏览: 69
方法一:for循环
s = [9,8,7,3,2,1,55,6]
count = 0
total = 0
max_num = s[0]
min_num = s[0]
for num in s:
count += 1
total += num
if num > max_num:
max_num = num
if num < min_num:
min_num = num
average = total / count
print("元素个数:", count)
print("最大值:", max_num)
print("最小值:", min_num)
print("元素之和:", total)
print("平均值:", average)
方法二:while循环
s = [9,8,7,3,2,1,55,6]
count = 0
total = 0
max_num = s[0]
min_num = s[0]
i = 0
while i < len(s):
count += 1
total += s[i]
if s[i] > max_num:
max_num = s[i]
if s[i] < min_num:
min_num = s[i]
i += 1
average = total / count
print("元素个数:", count)
print("最大值:", max_num)
print("最小值:", min_num)
print("元素之和:", total)
print("平均值:", average)
方法三:list函数
s = [9,8,7,3,2,1,55,6]
count = len(s)
total = sum(s)
max_num = max(s)
min_num = min(s)
average = total / count
print("元素个数:", count)
print("最大值:", max_num)
print("最小值:", min_num)
print("元素之和:", total)
print("平均值:", average)
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)