入一个由任意整数组成的列表,输出所有元素中的最大值、最小值和平均值,要求平均值保留2位小数。
时间: 2023-05-17 19:07:46 浏览: 228
好的,以下是您要求的答案:
您可以使用以下 Python 代码来解决这个问题:
```python
lst = [1, 2, 3, 4, 5] # 任意整数列表
max_val = max(lst)
min_val = min(lst)
avg_val = round(sum(lst) / len(lst), 2)
print("最大值:", max_val)
print("最小值:", min_val)
print("平均值:", avg_val)
```
输出结果为:
```
最大值: 5
最小值: 1
平均值: 3.0
```
希望这个答案能够帮到您!
相关问题
本题目要求读入一个由任意整数组成的列表,输出所有元素中的最大值、最小值和平均值,要求平均值保留2位小数
好的,这是一个简单的统计问题。我们可以先读入列表,然后找到其中的最大值、最小值和总和,最后计算平均值即可。以下是代码示例:
```python
nums = input().split()
nums = [int(num) for num in nums] # 将输入的字符串转换为整数列表
max_num = max(nums)
min_num = min(nums)
sum_num = sum(nums)
average = sum_num / len(nums)
print("最大值:", max_num)
print("最小值:", min_num)
print("平均值:", format(average, '.2f'))
```
输入格式示例:
```
1 2 3 4 5
```
输出结果示例:
```
最大值: 5
最小值: 1
平均值: 3.00
```
用Python要求读入一个由任意整数组成的列表,输出所有元素中的最大值、最小值和平均值,要求平均值保留2位小数。
好的,这个问题很简单,下面是Python代码实现:
```python
lst = list(map(int, input().split())) # 读入整数列表
max_val = max(lst) # 获取最大值
min_val = min(lst) # 获取最小值
avg_val = sum(lst) / len(lst) # 获取平均值
print("最大值:", max_val)
print("最小值:", min_val)
print("平均值:", round(avg_val, 2))
```
你可以将任意整数列表输入到程序中,程序会输出该列表的最大值、最小值和平均值。注意,平均值保留2位小数,使用`round()`函数实现。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)