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

发表回复

登录后才能评论