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/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

发表回复

登录后才能评论