python找出一个列表中的最大值和最小值
时间: 2023-05-22 21:06:54 浏览: 491
可以使用 Python 内置函数 `min` 和 `max` 来找出一个列表中的最大值和最小值。例如:
```python
my_list = [1, 2, 3, 4, 5]
max_value = max(my_list)
min_value = min(my_list)
print(f"最大值是 {max_value},最小值是 {min_value}")
```
输出结果为:
```
最大值是 5,最小值是 1
```
相关问题
Python找出列表中最大值和最小值。
可以使用 Python 内置函数 `max` 和 `min` 来找出列表中的最大值和最小值,示例代码如下:
```python
my_list = [1, 5, -2, 10, 20, 3]
max_value = max(my_list)
min_value = min(my_list)
print("最大值为:", max_value)
print("最小值为:", min_value)
```
输出结果:
```
最大值为: 20
最小值为: -2
```
Python中用循环找出列表中最大值和最小值。
可以使用for循环和if条件语句来实现:
```python
lst = [1, 3, 5, 7, 9, 2, 4, 6, 8]
# 找最大值
max_num = lst[0]
for num in lst:
if num > max_num:
max_num = num
print("最大值为:", max_num)
# 找最小值
min_num = lst[0]
for num in lst:
if num < min_num:
min_num = num
print("最小值为:", min_num)
```
输出结果为:
```
最大值为: 9
最小值为: 1
```
阅读全文
相关推荐














