Python 查找列表中最大数字的程序

使用实际示例编写一个 Python 程序来查找列表中的最大数字。内置的 max() 函数返回列表中的最大项,sort() 函数按升序对项目进行排序,最后一项将是最大的。除了这两个函数,您还可以使用 for 循环或 while 循环来迭代列表项,并使用 if 语句查找最大值。

使用 max() 的 Python 查找列表中最大数字的程序

max 函数返回 列表 中的最大值。

a = [10, 50, 60, 120, 20, 15]

print(max(a))

输出

120

max() 函数示例 2

这个最大列表数字的 程序 与上面相同。但这次,我们允许用户输入列表的长度。接下来,我们使用 For 循环 将数字添加到 Python 列表中。

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 Largest Element in this List is : ", max(NumList))
Python Program to find Largest Number in a List

使用 sort 函数的 Python 查找列表中最大数字的程序

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

a = [10, 50, 60, 80, 20, 15]

a.sort()
print(a[5])

输出

80

使用 sort() 函数示例 2

这个最大列表数字的程序与上面相同。但这次,我们允许用户输入自己的项目列表。

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 Largest Element in this List is : ", NumList[Number - 1])
Please enter the Total Number of List Elements: 3
Please enter the Value of 1 Element : 90
Please enter the Value of 2 Element : 56
Please enter the Value of 3 Element : 70
The Largest Element in this List is :  90

使用 sort 和 reverse 查找列表中最大数字的程序

这个 程序 按升序对列表项进行排序。接下来,我们使用 reverse 函数 来反转列表项。最后,我们使用索引位置 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()
NumList.reverse()

print("The Largest Element in this List is : ", NumList[0])
Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : 60
Please enter the Value of 2 Element : 30
Please enter the Value of 3 Element : 90
Please enter the Value of 4 Element : 89
Please enter the Value of 5 Element : 120
Please enter the Value of 6 Element : 45
The Largest Element in this List is :  120

使用 for 循环的 Python 查找列表中最大数字的程序

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

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)

largest = NumList[0]    
for j in range(1, Number):
    if(largest < NumList[j]):
        largest = NumList[j]
        position = j

print("The Largest Element in this List is : ", largest)
print("The Index position of the Largest Element is : ", position)
Program to find the Largest Number in a List using for loop

用户输入的查找列表中最大数字的程序值为
NumList[5] = {70, 80, 120, 87, 46}
largest = NumList[0] = 70

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

for 循环中的 if (largest < NumList[j]) 为 True,因为 (70 < 80)
largest = NumList[1]
largest = 80
position = 1

第二次迭代: for 2 in range(1, 5) – 条件为真
If (largest < NumList[2]) = (80 < 120) – 条件为真
largest = NumList[2] = 120
Position = 2

第三次迭代: for 3 in range(1, 5) – 条件为真
If (largest < NumList[3]) = (120 < 87) – 条件为假
largest = 120
Position = 2

第四次迭代: for 4 in range(1, 5) – 条件为真
If (largest < NumList[4]) = (120 < 46) – 条件为假
largest = 120
Position = 2

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