python取max函数
时间: 2023-07-25 12:38:35 浏览: 41
在 Python 中,`max()` 函数用于返回给定参数的最大值。可以传入任意数量的参数,也可以传入一个可迭代对象(如列表、元组、集合等)。下面是几个示例:
```
# 传入多个参数
a = 10
b = 20
c = 30
print(max(a, b, c)) # 输出 30
# 传入一个列表
lst = [5, 10, 15, 20]
print(max(lst)) # 输出 20
# 传入一个元组
tpl = (3, 6, 9, 12)
print(max(tpl)) # 输出 12
# 传入一个集合
st = {2, 4, 6, 8}
print(max(st)) # 输出 8
```
相关问题
Python的max函数
Python的max函数是内置函数之一,用于返回一个可迭代对象中的最大值。max函数可以接受任意多个参数或者一个可迭代对象作为参数。如果参数中有多个值,则返回其中的最大值。如果参数是一个迭代器,则会遍历这个迭代器并返回其中的最大值。
以下是使用max函数的示例代码:
```python
a = [1, 3, 5, 2, 4]
print(max(a)) # 输出:5
b = (2, 4, 6, 1)
print(max(b)) # 输出:6
c = {'a': 1, 'b': 2, 'c': 3}
print(max(c)) # 输出:'c'
```
Python中max函数
Python中的max()函数用于返回可迭代对象中的最大值。它可以用于所有可迭代对象,如列表、元组、字符串、字典等[^1][^2]。
下面是一个例子,用于在列表中查找最大值:
```python
list1 = [5, 10, 15, 20, 25]
print("Max value element : ", max(list1)) # 输出:Max value element : 25
```
在这个例子中,max()函数查找列表list1中的最大元素,并将其输出。
示例2:对于字符串,max()函数返回其ASCII或Unicode码的最大值:
```python
string = "hello world"
print("Max character : ", max(string)) # 输出:Max character : w
```
在这个例子中,max()函数返回了字符串“hello world”中最大的字符,即“w”。