Python 程序:統計元組中正數和負數

編寫一個 Python 程序,使用 for 循環範圍來計算元組中的正數和負數。if 條件(if (posngTuple[i] >= 0))檢查元組項是否大於或等於零。如果為真,我們向正元組計數添加一個;否則(tNegativeCount = tNegativeCount+1),在負元組計數值上加 1。

# Count Positive and Negative Numbers

posngTuple = (3, -22, -44, 19, -99, -37, 4, 11, -89)
print("Positive and Negative Tuple Items = ", posngTuple)

tPositiveCount = tNegativeCount = 0

for i in range(len(posngTuple)):
    if (posngTuple[i] >= 0):
        tPositiveCount = tPositiveCount + 1
    else:
        tNegativeCount = tNegativeCount + 1

print("The Count of Positive Numbers in posngTuple = ", tPositiveCount)
print("The Count of Negative Numbers in posngTuple = ", tNegativeCount)

在這個 Python 正負示例中,我們使用 for 循環(對於 posngTuple 中的 pntup)來迭代實際的元組值,並檢查它們是否大於或等於零。

# Count Positive and Negative Numbers

posngTuple = (55, -99, -88, 0, -78, 22, 4, -66, 21, 33)
print("Positive and Negative Tuple Items = ", posngTuple)

tPositiveCount = tNegativeCount = 0
for pntup in posngTuple:
    if(pntup >= 0):
        tPositiveCount = tPositiveCount + 1
    else:
        tNegativeCount = tNegativeCount + 1

print("The Count of Positive Numbers in posngTuple = ", tPositiveCount)
print("The Count of Negative Numbers in posngTuple = ", tNegativeCount)
Positive and Negative Tuple Items =  (55, -99, -88, 0, -78, 22, 4, -66, 21, 33)
The Count of Positive Numbers in posngTuple =  6
The Count of Negative Numbers in posngTuple =  4

Python 程序使用 While 循環計算元組中的正數和負數。

# Count of Tuple Positive and Negative Numbers

posngTuple = (11, -22, -33, 44, 55, -66, -77, 0, -99)
print("Positive and Negative Tuple Items = ", posngTuple)

tPositiveCount = tNegativeCount = 0
i = 0

while (i < len(posngTuple)):
    if(posngTuple[i] >= 0):
        tPositiveCount = tPositiveCount + 1
    else:
        tNegativeCount = tNegativeCount + 1
    i = i + 1

print("The Count of Positive Numbers in posngTuple = ", tPositiveCount)
print("The Count of Negative Numbers in posngTuple = ", tNegativeCount)
Positive and Negative Tuple Items =  (11, -22, -33, 44, 55, -66, -77, 0, -99)
The Count of Positive Numbers in posngTuple =  4
The Count of Negative Numbers in posngTuple =  5

在這個 Python Tuple 示例中,我們創建了一個返回正數和負數計數的函數。

# Count of Tuple Positive and Negative Numbers

def CountOfPositiveNegativeNumbers(evodTuple):
    tPositiveCount = tNegativeCount = 0
    for pntup in evodTuple:
        if(pntup >= 0):
            tPositiveCount = tPositiveCount + 1
        else:
            tNegativeCount = tNegativeCount + 1
    return tPositiveCount, tNegativeCount

evodTuple = (26, -77, -99, 75, 14, -56, 19, 81, -1, 33) 
print("Positive and Negative Tuple Items = ", evodTuple)

PositiveCount, NegativeCount = CountOfPositiveNegativeNumbers(evodTuple)
print("The Count of Positive Numbers in evodTuple = ", PositiveCount)
print("The Count of Negative Numbers in evodTuple = ", NegativeCount)
Positive and Negative Tuple Items =  (26, -77, -99, 75, 14, -56, 19, 81, -1, 33)
The Count of Positive Numbers in evodTuple =  6
The Count of Negative Numbers in evodTuple =  4

原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127221.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
簡單一點的頭像簡單一點
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相關推薦

  • Python列表中負數的個數

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

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

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

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

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

    編程 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
  • 如何使用GPU加速運行Python程序——以CSDN為中心

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

    編程 2025-04-29
  • Web程序和桌面程序的區別

    Web程序和桌面程序都是進行軟體開發的方式,但是它們之間存在很大的區別。本文將從多角度進行闡述。 一、運行方式 Web程序運行於互聯網上,用戶可以通過使用瀏覽器來訪問它。而桌面程序…

    編程 2025-04-29

發表回復

登錄後才能評論