Python 程序:在列表中查找最大和最小數字

寫一個 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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-09 11:01
下一篇 2024-12-09 11:01

相關推薦

  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python循環符合要求數字求和

    這篇文章將詳細介紹如何通過Python循環符合要求數字求和。如果你想用Python求和但又不想手動輸入數字,那麼本文將是一個不錯的選擇。 一、使用while循環實現求和 sum =…

    編程 2025-04-29
  • Python程序文件的拓展

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • Python購物車程序

    Python購物車程序是一款基於Python編程語言開發的程序,可以實現購物車的相關功能,包括商品的添加、購買、刪除、統計等。 一、添加商品 添加商品是購物車程序的基礎功能之一,用…

    編程 2025-04-29
  • 爬蟲是一種程序

    爬蟲是一種程序,用於自動獲取互聯網上的信息。本文將從如下多個方面對爬蟲的意義、運行方式、應用場景和技術要點等進行詳細的闡述。 一、爬蟲的意義 1、獲取信息:爬蟲可以自動獲取互聯網上…

    編程 2025-04-29
  • Vb運行程序的三種方法

    VB是一種非常實用的編程工具,它可以被用於開發各種不同的應用程序,從簡單的計算器到更複雜的商業軟件。在VB中,有許多不同的方法可以運行程序,包括編譯器、發布程序以及命令行。在本文中…

    編程 2025-04-29
  • Python基本數字類型

    本文將介紹Python中基本數字類型,包括整型、布爾型、浮點型、複數型,並提供相應的代碼示例以便讀者更好的理解。 一、整型 整型即整數類型,Python中的整型沒有大小限制,所以可…

    編程 2025-04-29
  • Python一元二次方程求解程序

    本文將詳細闡述Python一元二次方程求解程序的相關知識,為讀者提供全面的程序設計思路和操作方法。 一、方程求解 首先,我們需要了解一元二次方程的求解方法。一元二次方程可以寫作: …

    編程 2025-04-29
  • Python打印數字三角形

    本文將詳細闡述如何使用Python打印數字三角形,包括從基本代碼實現到進階操作的應用。通過本文的學習,您可以掌握Python的基礎語法,同時加深對Python循環和函數的理解,提高…

    編程 2025-04-29

發表回復

登錄後才能評論