Python Program to Print Positive Numbers in an Array

Write a Python Program to Print Positive Numbers in an Array using for loop range (or i in range(len(posArr))). The if condition (if (posArr[i] >= 0)) checks the numpy array item is greater than or equals to zero. If True, it prints that Positive array item.

# Print Positives in Array

import numpy as np

posArr = np.array([10, -22, -14, 19, 11, -9, 0])

print("**The Positive Numbers in this posArr Array***")
for i in range(len(posArr)):
    if (posArr[i] >= 0):
        print(posArr[i], end = "  ")
**The Positive Numbers in this posArr Array***
10  19  11  0  

Python Program to Print Positive Numbers in an Array using for loop

In this Python example, the for loop (for num in posArr) iterates the original values. Within the second for loop, numpy greater_equal function (if (np.greater_equal(i, 0) == True)) checks whether the numpy array item is greater than or equals zero returns True. If True, print that positive number from the numpy array.

# Print Positive in Array

import numpy as np

posArr = np.array([1, 22, -99, -4, 14, 11, -10])

print("**The Positive Numbers in this posArr Array***")
for num in posArr:
    if (num >= 0):
        print(num, end = "  ")


print("\n\n=== Using greater equal function===")
print("**The Positive Numbers in this posArr Array***")
for i in posArr:
    if (np.greater_equal(i, 0) == True):
        print(i, end = "  ")
**The Positive Numbers in this posArr Array***
1  22  14  11  

=== Using greater equal function===
**The Positive Numbers in this posArr Array***
1  22  14  11  

Python Program to return Positive Numbers in a Numpy Array using the while loop.

# Print Positive in Array

import numpy as np

posArr = np.array([4, -5, 22, -9, -48, 11, 14])
i = 0

print("**The Positive Numbers in this posArr Array***")
while (i < len(posArr)):
    if (np.greater_equal(posArr[i], 0) == True):
        print(posArr[i], end = "  ")
    i = i + 1
**The Positive Numbers in this posArr Array***
4  22  11  14  

In this Python numpy array example, we created a (def printPositiveNumbers(posArr)) function that prints the Positive numbers.

# Print Positive in Array

import numpy as np

def printPositiveNumbers(posArr):
    for i in posArr:
        if (np.greater_equal(i, 0) == True):
            print(i, end = "  ")
    

posArr = np.array([1, -11, 0, 15, -9, -17, 22, -67, 55])

print("**The Positive Numbers in this posArr Array***")
printPositiveNumbers(posArr)
Python Program to Print Positive Numbers in an Array 4