Python 程序查找列表中的最大值和最小值

编写一个 Python 程序,通过实际示例查找列表中的最大值和最小值。

Python 程序查找列表中的最大值和最小值 示例 1

此 Python 程序允许用户输入列表的长度。接下来,我们使用 For 循环将数字添加到列表中。这里,minmax 函数返回列表中的最小值和最大值或最小和最大值。

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

print("The Smallest Element in this List is : ", min(NumList))
print("The Largest Element in this List is : ", max(NumList))

列表中最大和最小数字的输出

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 50
Please enter the Value of 2 Element : 45
Please enter the Value of 3 Element : 33
Please enter the Value of 4 Element : 78
Please enter the Value of 5 Element : 66
The Smallest Element in this List is :  33
The Largest Element in this List is :  78

列表中最大和最小值 示例 2

sort 函数按升序对列表元素进行排序。接下来,我们使用索引位置 0 来打印第一个元素,并使用最后一个索引位置来打印列表中的最后一个元素。

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

NumList.sort()

print("The Smallest Element in this List is : ", NumList[0])
print("The Largest Element in this List is : ", NumList[Number - 1])
Python Program to find Largest and Smallest Number in a List 2

Python 程序查找列表中的最大值和最小值 示例 3

在此程序中,我们不使用任何内置函数,如 sort、max 或 min 函数。

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

smallest = largest = NumList[0]

for j in range(1, Number):
    if(smallest > NumList[j]):
        smallest = NumList[j]
        min_position = j
    if(largest < NumList[j]):
        largest = NumList[j]
        max_position = j

print("The Smallest Element in this List is : ", smallest)
print("The Index position of Smallest Element in this List is : ", min_position)
print("The Largest Element in this List is : ", largest)
print("The Index position of Largest Element in this List is : ", max_position)

列表中最大和最小数字的输出

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 40
Please enter the Value of 2 Element : 60
Please enter the Value of 3 Element : 20
Please enter the Value of 4 Element : 11
Please enter the Value of 5 Element : 50
The Smallest Element in this List is :  11
The Index position of Smallest Element in this List is :  3
The Largest Element in this List is :  60
The Index position of Largest Element in this List is :  1

从上面的 Python 程序查找列表中的最大值和最小值输出中,用户插入的值是
NumList[5] = {40, 60, 20, 11, 50}
smallest = largest = NumList[0] = 40

第一次迭代 – for 1 in range(1, 5) – 条件为真
因此,它开始执行循环内的 If 语句,直到条件失败。

If (smallest > NumList[j]) 在 For 循环内的条件为 False,因为 (40 > 60)
smallest = 40
position = 1

If (largest < NumList[j]) 在 for 循环内的条件为 True,因为 (40 < 60)
largest = 60
position = 1

第二次迭代:for 2 in range(1, 5) – 条件为真
If (40 > 20) – 条件为 True
smallest = 20
Position = 2

If (60 < 20) – 条件为 False
largest = 60 ==> 未改变
Position = 1 ==> 未改变

第三次迭代:for 3 in range(1, 5) – 条件为真
If (20 > 11) – 条件为 True
smallest = 11
Position = 3

If (60 < 11) – 条件为 False
largest = 60
Position = 1

第四次迭代: for 4 in range(1, 5) – 条件为真
If (11 > 50) – 条件为 False
smallest = 11
Position = 3

If (60 < 11) – 条件为 False
largest = 60
Position = 1

第五次迭代:for 5 in range(1, 5) – 条件为 False
因此,它退出循环。