热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python程序:统计列表中正数和负数

写一个 Python 程序,用 For 循环、While 循环和函数计算列表中的正数和负数,并给出一个实例。使用 For 循环计算列表中正数和负数的 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 Program to Count Positive and Negative Numbers in a List 1

在这个 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 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

推荐阅读
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文介绍如何使用 Python 编写程序,检查给定列表中的元素是否形成交替峰值模式。我们将探讨两种不同的方法来实现这一目标,并提供详细的代码示例。 ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文介绍如何使用 Python 将一个字符串按照指定的行和元素分隔符进行两次拆分,最终将字符串转换为矩阵形式。通过两种不同的方法实现这一功能:一种是使用循环与 split() 方法,另一种是利用列表推导式。 ... [详细]
  • 本文详细介绍 Go+ 编程语言中的上下文处理机制,涵盖其基本概念、关键方法及应用场景。Go+ 是一门结合了 Go 的高效工程开发特性和 Python 数据科学功能的编程语言。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 深入理解C++中的KMP算法:高效字符串匹配的利器
    本文详细介绍C++中实现KMP算法的方法,探讨其在字符串匹配问题上的优势。通过对比暴力匹配(BF)算法,展示KMP算法如何利用前缀表优化匹配过程,显著提升效率。 ... [详细]
  • 掌握远程执行Linux脚本和命令的技巧
    本文将详细介绍如何利用Python的Paramiko库实现远程执行Linux脚本和命令,帮助读者快速掌握这一实用技能。通过具体的示例和详尽的解释,让初学者也能轻松上手。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文介绍了在Windows环境下使用pydoc工具的方法,并详细解释了如何通过命令行和浏览器查看Python内置函数的文档。此外,还提供了关于raw_input和open函数的具体用法和功能说明。 ... [详细]
  • 本文深入探讨了 Python 列表切片的基本概念和实际应用,通过具体示例展示了不同切片方式的使用方法及其背后的逻辑。 ... [详细]
author-avatar
张光和尚山秀珍
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有