getMaxMin(x, y) python
时间: 2024-05-03 13:16:42 浏览: 10
以下是 getMaxMin(x, y) 函数的 Python 实现:
```python
def getMaxMin(x, y):
"""
获取两个数的最大值和最小值
"""
if x > y:
return x, y
else:
return y, x
```
函数接收两个参数 x 和 y,返回一个元组,元组中第一个元素为较大的数,第二个元素为较小的数。如果 x 大于 y,则返回 (x, y),否则返回 (y, x)。
相关问题
getMaxMin(x, y)python
Sure, here's a Python function that takes two integers as input and returns a tuple containing their maximum and minimum values:
```python
def getMaxMin(x, y):
max_val = max(x, y)
min_val = min(x, y)
return (max_val, min_val)
```
You can call this function and pass in your own values for `x` and `y` like this:
```python
max_min = getMaxMin(10, 5)
print(max_min) # Output: (10, 5)
```
This will output a tuple containing the maximum and minimum values of 10 and 5, respectively.
getMaxMin()python
这是一个函数,它可以从给定的列表中找到最大值和最小值,并将它们以元组的形式返回。以下是一个可能的 getMaxMin() 函数的实现:
```python
def getMaxMin(lst):
if not lst:
return None
min_val = max_val = lst[0]
for val in lst:
if val < min_val:
min_val = val
if val > max_val:
max_val = val
return (max_val, min_val)
```
你可以通过传递一个列表作为参数来调用这个函数,例如:
```python
lst = [3, 5, 2, 8, 1, 4, 7, 6]
max_val, min_val = getMaxMin(lst)
print("最大值为:", max_val)
print("最小值为:", min_val)
```
这个程序会输出:
```
最大值为: 8
最小值为: 1
```
相关推荐
![](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)
![](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)