寫一個 Python 程序,用一個實際例子找出列表中最大和最小的數字。
Python 程序查找列表中最大和最小的數字示例 1
這個 python 程序允許用戶輸入列表的長度。接下來,我們使用 For 循環向列表中添加數字。這裡, Python 中的 min 和 max 函數返回一個列表中的最小和最大數字或最小和最大值。
# Python Program to find Largest and Smallest Number in a List
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))
Python 最大和最小列表號輸出
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
Python 程序查找列表中最大和最小的數字示例 2
Python 排序函數按照升序對列表元素進行排序。接下來,我們使用索引位置 0 打印列表中的第一個元素,最後一個索引位置打印列表中的最後一個元素。
# Python Program to find Largest and Smallest Number in a List
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 程序查找列表中最大和最小的數字示例 3
在這個程序中,我們沒有使用任何內置功能,比如排序、最大或最小功能。
# Python Program to find Largest and Smallest Number in a List
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)
Python 最大和最小列表號輸出
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}
最小=最大= NumList[0] = 40
第一次迭代–對於範圍(1,5)中的 1–條件為真
因此,它開始在循環內執行 If 語句,直到條件失敗。
如果循環的內的(最小>數值列表【j】為假,因為(40 > 60)
最小= 40
位置= 1
If(最大< NumList[j]) inside the for loop is True because (40 < 60)
最大= 60
位置= 1
第二次迭代:對於範圍(1,5)中的 2–條件為真
If(40>20)–條件為真
最小= 20
位置= 2
如果(60 < 20) – Condition False
最大= 60 == >不變
位置= 1 == >不變
第三次迭代:對於範圍(1,5)中的 3–條件為真
如果(20>11)–條件為真
最小= 11
位置= 3
若(60 < 11) – Condition False
最大= 60
位置= 1
第四次迭代:對於範圍(1,5)中的 4–條件為真
如果(11>50)–條件為假
最小= 11
位置= 3
如果(60 < 11) – Condition False
最大= 60
位置= 1
第五次迭代:對於範圍(1,5)中的 5–條件為假
因此,它退出循環。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/219877.html