Write a Python Program to Count Positive and Negative Numbers in an Array using for loop range. The if condition (if (posNegaArr[i] >= 0)) checks whether the array item is greater than or equals to zero. If True, we add one to the Positive array count; otherwise, add one to the negative array count.
# Count Positive and Negative in Array import numpy as np posNegaArr = np.array([1, -19, -22, 16, -76, 0, 22, 50, -122, 70]) print("posNegaArr Array = ", posNegaArr) posArrCount = 0 negArrCount = 0 for i in range(len(posNegaArr)): if (posNegaArr[i] >= 0): posArrCount = posArrCount + 1 else: negArrCount = negArrCount + 1 print("The Count of Positive Numbers in posNegaArr Array = ", posArrCount) print("The Count of Negative Numbers in posNegaArr Array = ", negArrCount)
Python Program to Count Positive and Negative Numbers in an Array
In this Python example, we used the for loop to iterate the actual values, not the index position. Within the second for loop, numpy greater_equal function examines whether a particular numpy array item is greater than or equals zero returns True.
# Count Positive and Negative in Array import numpy as np posNegaArr = np.array([4, 25, -9, 8, -6, -7, 0, 11, -17, 6, -44, 89]) posArrCount = posArrCount1 = 0 negaArrCount = negaArrCount1 = 0 for i in posNegaArr: if (i >= 0): posArrCount = posArrCount + 1 else: negaArrCount = negaArrCount + 1 print("The Count of Positive Numbers in evennegaArr Array = ", posArrCount) print("The Count of Negative Numbers in evennegaArr Array = ", negaArrCount) print("\n=== Using greater_equal function===") for i in posNegaArr: if (np.greater_equal(i, 0) == True): posArrCount1 = posArrCount1 + 1 else: negaArrCount1 = negaArrCount1 + 1 print("The Count of Positive Numbers in evennegaArr Array = ", posArrCount1) print("The Count of Negative Numbers in evennegaArr Array = ", negaArrCount1)
Count Positive and Negative Numbers in a Python Numpy Array using for loop output
The Count of Positive Numbers in evennegaArr Array = 7
The Count of Negative Numbers in evennegaArr Array = 5
=== Using greater_equal function===
The Count of Positive Numbers in evennegaArr Array = 7
The Count of Negative Numbers in evennegaArr Array = 5
Python Program to Count Positive and Negatives in Numpy Array using the While loop.
# Count Positive and Negative in Array import numpy as np posNegArr = np.array([418, -22, -33, 0, -89, -56, 11, -99]) i = 0 posArrCount = 0 negArrCount = 0 while (i < len(posNegArr)): if (np.greater_equal(posNegArr[i], 0) == True): posArrCount = posArrCount + 1 else: negArrCount = negArrCount + 1 i = i + 1 print("The Count of Positive Numbers in posNegArr Array = ", posArrCount) print("The Count of Negative Numbers in posNegArr Array = ", negArrCount)
Count Positive and Negative Python Numpy Array items using a while loop output
The Count of Positive Numbers in posNegArr Array = 3
The Count of Negative Numbers in posNegArr Array = 5
In this Python numpy array example, we created a function (CountEvenOddNumbers(posNegArr)) that returns the Positive and Negative numbers count.
# Count Positive and Negative in Array import numpy as np def CountEvenOddNumbers(posNegArr): posArrCount = 0 negArrCount = 0 for i in posNegArr: if (np.greater_equal(i, 0) == True): posArrCount = posArrCount + 1 else: negArrCount = negArrCount + 1 return posArrCount, negArrCount posNegArr = np.array([6, -9, -11, 8, 0, -14, -16, -9, 1]) positive, negative = CountEvenOddNumbers(posNegArr) print("The Count of Positive Numbers in posNegArr Array = ", positive) print("The Count of Negative Numbers in posNegArr Array = ", negative)
The Count of Positive Numbers in posNegArr Array = 4
The Count of Negative Numbers in posNegArr Array = 5