Python 程序:統計列表中正數和負數

寫一個 Python 程序,用 For 循環、While 循環和函數計算列表中的正數和負數,並給出一個實例。

使用 For 循環計算列表中正數和負數的 Python 程序

在這個 python 程序中,我們使用 For 循環來迭代給定列表中的每個元素。在 Python for 循環中,我們使用 If 語句來檢查和計數正數和負數。

# Python Program to Count Positive and Negative Numbers in a List

NumList = []
Positive_count = 0
Negative_count = 0

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)

for j in range(Number):
    if(NumList[j] >= 0):
        Positive_count = Positive_count + 1
    else:
        Negative_count = Negative_count + 1

print("\nTotal Number of Positive Numbers in this List =  ", Positive_count)
print("Total Number of Negative Numbers in this List = ", Negative_count)

在這個 python 程序中,用戶輸入了列表元素= [12,-22,3,5],正 計數= 0,負 計數= 0

對於循環–第一次迭代:對於範圍(0,4)
中的 0,條件為真。因此,進入 If 語句T3 If(NumList[0]>= 0)=>If(12>= 0)–條件為真
計數=正 計數+ 1 = > 0 + 1 = 1

第二次迭代:對於範圍(0,4)中的 1–條件為真
如果(NumList[1] > = 0) = >如果(-22>= 0)–條件為假,則進入 Else 塊。
計數=負 計數+ 1 = > 0 + 1 = 1

第三次迭代:對於範圍(0,4)中的 2–條件為真
如果(NumList[2] > = 0) = >如果(3>= 0)–條件為真
正 _ 計數= 1 + 1 = > 2

第四次迭代:對於範圍(0,4)中的 3,如果(5>= 0)–條件為真,則條件為真
。所以,它進入了 Else 塊。
陽性計數= 2 + 1 = > 3

第五次迭代:對於範圍(4)中的 4–條件為假。所以,蟒蛇從 離開

Python 程序使用 While 循環計算列表中的正數和負數

這個計算正數和負數的 Python 程序與上面的相同。我們剛剛將 For Loop 替換為 While loop 。

# Python Program to Count Positive and Negative Numbers in a List

NumList = []
Positive_count = 0
Negative_count = 0
j = 0

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)

while(j < Number):
    if(NumList[j] >= 0):
        Positive_count = Positive_count + 1
    else:
        Negative_count = Negative_count + 1
    j = j + 1

print("\nTotal Number of Positive Numbers in this List =  ", Positive_count)
print("Total Number of Negative Numbers in this List = ", Negative_count)

Python 使用 while 循環輸出計算正負列表數

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : -3
Please enter the Value of 2 Element : -5
Please enter the Value of 3 Element : 9
Please enter the Value of 4 Element : 8
Please enter the Value of 5 Element : -6

Total Number of Positive Numbers in this List =   2
Total Number of Negative Numbers in this List =  3

使用函數計算列表中正負項目的 Python 程序

這個 Python 計算正負列表項目的程序與第一個示例相同。但是,我們使用函數來分離邏輯

def count_Positive(NumList):
    Positive_count = 0
    for j in range(Number):
        if(NumList[j] >= 0):
            Positive_count = Positive_count + 1
    return Positive_count

def count_Negative(NumList):
    Negative_count = 0
    for j in range(Number):
        if(NumList[j] % 2 != 0):
            Negative_count = Negative_count + 1
    return Negative_count

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)

Positive_cnt = count_Positive(NumList)
Negative_cnt = count_Negative(NumList)
print("\nTotal Number of Positive Numbers in this List =  ", Positive_cnt)
print("Total Number of Negative Numbers in this List = ", Negative_cnt)
Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : -11
Please enter the Value of 2 Element : -22
Please enter the Value of 3 Element : 33
Please enter the Value of 4 Element : 44
Please enter the Value of 5 Element : -55
Please enter the Value of 6 Element : 66

Total Number of Positive Numbers in this List =   3
Total Number of Negative Numbers in this List =  3

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/271834.html

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

相關推薦

  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python程序需要編譯才能執行

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

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

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

    編程 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一元二次方程求解程序的相關知識,為讀者提供全面的程序設計思路和操作方法。 一、方程求解 首先,我們需要了解一元二次方程的求解方法。一元二次方程可以寫作: …

    編程 2025-04-29
  • Python列表中大於某數的元素處理方法

    本文將會介紹如何在Python列表中找到大於某數的元素,並對其進行進一步的處理。 一、查找大於某數的元素 要查找Python列表中大於某數的元素,可以使用列表推導式進行處理。 nu…

    編程 2025-04-29
  • 如何使用GPU加速運行Python程序——以CSDN為中心

    GPU的強大性能是眾所周知的。而隨着深度學習和機器學習的發展,越來越多的Python開發者將GPU應用於深度學習模型的訓練過程中,提高了模型訓練效率。在本文中,我們將介紹如何使用G…

    編程 2025-04-29

發表回復

登錄後才能評論