Write a Python Program to Print Even Numbers in an Array using for loop range (for i in range(len(evenArr))). The if statement (if (evenArr[i] % 2 == 0)) checks whether the numpy array item at each index position is divisible by two. If True, print that numpy array even number.
# Print Even in Array import numpy as np evenArr = np.array([10, 23, 99, 40, 70, 22, 19]) print("**The List of Even Numbers in this evenArr Array***") for i in range(len(evenArr)): if (evenArr[i] % 2 == 0): print(evenArr[i], end = " ")
Print even numbers in a Python Numpy Array output
**The List of Even Numbers in this evenArr Array***
10 40 70 22
Python Program to Print Even Numbers in an Array using For Loop
In this Python example, we used the numpy mod and numpy remainder functions to check the remainder of each array item divisible by two is equal to zero. If True, print that even number from a numpy array.
# Print Even in Array import numpy as np evenArr = np.array([1, 22, 99, 44, 74, 89, 190]) print("**The List of Even Numbers in this evenArr Array***") for i in evenArr: if (i % 2 == 0): print(i, end = " ") print("\n\n=== Using mod function===") print("**The List of Even Numbers in this evenArr Array***") for i in evenArr: if (np.mod(i, 2) == 0): print(i, end = " ") print("\n\n=== Using remainder function===") print("**The List of Even Numbers in this evenArr Array***") for i in evenArr: if (np.remainder(i, 2) == 0): print(i, end = " ")
Print Python Numpy Array even numbers using for loop output
**The List of Even Numbers in this evenArr Array***
22 44 74 190
=== Using mod function===
**The List of Even Numbers in this evenArr Array***
22 44 74 190
=== Using remainder function===
**The List of Even Numbers in this evenArr Array***
22 44 74 190
Python Program to return Even Numbers in an Array using the While Loop.
# Print Even in Array import numpy as np evenArr = np.array([44, 15, 22, 19, 148, 17, 140]) i = 0 print("**The List of Even Numbers in this evenArr Array***") while (i < len(evenArr)): if (np.equal(evenArr[i] % 2, 0)): print(evenArr[i], end = " ") i = i + 1
Print even numbers in a Python Numpy Array using a while loop output
**The List of Even Numbers in this evenArr Array***
44 22 148 140
In this Python numpy array example, we created a function (printEvenNumbers(evenArr)) that checks and prints the Even numbers.
# Print Even in Array import numpy as np def printEvenNumbers(evenArr): for i in evenArr: if (np.remainder(i, 2) == 0): print(i, end = " ") evenArr = np.array([1, 8, 5, 22, 14, 17, 10, 18, 35, 44]) print("**The List of Even Numbers in this evenArr Array***") printEvenNumbers(evenArr)