Table of Contents
Python code to count positive negative and zero in a list
In this article, we will discuss the concept of Python code to count positive negative and zero in a list.
In this program, we are going to learn how to count Positive numbers, Negative numbers and zeros in a list using several ways in Python language.
This is done using for loop , while loop, and function in Python language
Count positive negative and zero in a list
Code to count positive negative and zero in a list using for loop
In this program, we will count positive number, negative number and zeros from given numbers in a list using a for loop in Python language
Program 1
#Python program to count positive, negative numbers in a list Num_List=[] positive_Count=0 negative_Count=0 zero_Count=0 num=int(input("Please enter the total number of elements in a list: ")) for i in range(1,num+1): val=int(input("Please enter the values of %d elements list: "%i)) Num_List.append(val) for j in range(num): if(Num_List[j]>0): positive_Count=positive_Count+1 elif(Num_List[j]<0): negative_Count=negative_Count+1 else: zero_Count=zero_Count+1 print("Total positive number in the list=",positive_Count) print("Total negative number in the list=",negative_Count) print("Total zeros in the list=",zero_Count)
When the above code is executed, it produces the following result
Please enter the total number of elements in a list: 8 Please enter the values of 1 elements list: 543 Please enter the values of 2 elements list: 785 Please enter the values of 3 elements list: 0 Please enter the values of 4 elements list: -524 Please enter the values of 5 elements list: -542 Please enter the values of 6 elements list: 567 Please enter the values of 7 elements list: 541 Please enter the values of 8 elements list: -67 Total positive number in the list= 4 Total negative number in the list= 3 Total zeros in the list= 1
Code to count positive negative and zero in a list using while loop
In this program, we will count positive number, negative number and zeros from given numbers in a a list using a while loop in Python language
Program 2
#Python program to count positive, negative numbers in a list Num_List=[] positive_Count=0 negative_Count=0 zero_Count=0 j=0 num=int(input("Please enter the total number of elements in a list: ")) for i in range(1,num+1): val=int(input("Please enter the values of %d elements list: "%i)) Num_List.append(val) while(j<num): if(Num_List[j]>0): positive_Count=positive_Count+1 elif(Num_List[j]<0): negative_Count=negative_Count+1 else: zero_Count=zero_Count+1 j=j+1 print("Total positive number in the list=",positive_Count) print("Total negative number in the list=",negative_Count) print("Total zeros in the list=",zero_Count)
When the above code is executed, it produces the following result
Please enter the total number of elements in a list: 10 Please enter the values of 1 elements list: 432 Please enter the values of 2 elements list: 0 Please enter the values of 3 elements list: -567 Please enter the values of 4 elements list: 321 Please enter the values of 5 elements list: -673 Please enter the values of 6 elements list: 0 Please enter the values of 7 elements list: 543 Please enter the values of 8 elements list: -865 Please enter the values of 9 elements list: 345 Please enter the values of 10 elements list: 678 Total positive number in the list= 5 Total negative number in the list= 3 Total zeros in the list= 2
Code to count positive negative and zero in a list using function
In this program, we will count positive number, negative number and zeros from given numbers in a list using a function in Python language
Program 3
#Python program to count positive, negative numbers in a list def countPositive(Num_List): positive_Count=0 for j in range(num): if(Num_List[j]>0): positive_Count=positive_Count+1 return positive_Count def countNegative(Num_List): negative_Count=0 for j in range(num): if(Num_List[j]<0): negative_Count=negative_Count+1 return negative_Count def countZero(Num_List): zero_Count=0 for j in range(num): if(Num_List[j]==0): zero_Count=zero_Count+1 return zero_Count Num_List=[] num=int(input("Please enter the total number of elements in a list: ")) for i in range(1,num+1): val=int(input("Please enter the values of %d elements list: "%i)) Num_List.append(val) print("Total positive number in the list=",countPositive(Num_List)) print("Total negative number in the list=",countNegative(Num_List)) print("Total zeros in the list=",countZero(Num_List))
When the above code is executed, it produces the following result
Please enter the total number of elements in a list: 7 Please enter the values of 1 elements list: 67 Please enter the values of 2 elements list: 0 Please enter the values of 3 elements list: -7 Please enter the values of 4 elements list: 97 Please enter the values of 5 elements list: -88 Please enter the values of 6 elements list: 87 Please enter the values of 7 elements list: 77 Total positive number in the list= 4 Total negative number in the list= 2 Total zeros in the list= 1
Suggested post
Similar post
Program to count Positive, Negative and zero in an array of C++
Program to count Positive, Negative and zero in an array of Python
Program to count Positive, Negative and zero in an array of Java
Program to count Positive, Negative and zero in an array of C language