Python Program to Print Negative Numbers in an Array

Write a Python Program to Print Negative Numbers in a Numpy Array using for loop range (for i in range(len(negaArr))). The if condition (if (negaArr[i] < 0)) finds the numpy array item is less than zero. If True, it prints that Negative array item.

# Print Negatives in Array
import numpy as np

negaArr = np.array([11, -22, -33, 14, -17, 12, 0, -9, -34])

print("***The Negative Numbers in this negaArr Array***")
for i in range(len(negaArr)):
    if (negaArr[i] < 0):
        print(negaArr[i], end = "  ")
***The Negative Numbers in this negaArr Array***
-22  -33  -17  -9  -34 

Python Program to Print Negative Numbers in an Array using the for loop.

In this Python example, the for loop (for num in negaArr) iterates the actual numpy array values. Within the second for loop, the numpy less function (if (np.less(i, 0) == True)) checks whether the numpy array item is less than zero returns True. If True, print that Negative number from the negaArr numpy array.

# Print Negatives in Array
import numpy as np
negaArr = np.array([1, -4, -9, 15, -22, 0, -99, 14, -10, -7, 6])

print("**The Negative Numbers in this negaArr Array***")
for num in negaArr:
    if (num < 0):
        print(num, end = "  ")

print("\n\n=== Using less function===")
print("**The Negative Numbers in this negaArr Array***")
for i in negaArr:
    if (np.less(i, 0) == True):
        print(i, end = "  ")
Python Program to Print Negative Numbers in an Array 2

Python Program to return Negative Numbers in a Numpy Array using While loop.

# Print Negative in Array

import numpy as np

negaArr = np.array([1, -34, -77, 11, -90, 88, 65, -17, -30])
i = 0

print("**The Negative Numbers in this negaArr Array***")
while (i < len(negaArr)):
    if (np.less(negaArr[i], 0) == True):
        print(negaArr[i], end = "  ")
    i = i + 1
**The Negative Numbers in this negaArr Array***
-34  -77  -90  -17  -30  

In this Python numpy array example, we created a (def printNegativeNumbers(negaArr)) function that prints the Negative numbers.

# Print Negative in Array

import numpy as np

def printNegativeNumbers(negaArr):
    for i in negaArr:
        if (np.less(i, 0) == True):
            print(i, end = "  ")
    

negaArr = np.array([16, -99, -88, 0, -77, 44, -55, -2, 19])

print("**The Negative Numbers in this negaArr Array***")
printNegativeNumbers(negaArr)
**The Negative Numbers in this negaArr Array***
-99  -88  -77  -55  -2