Element-wise real test

Element-wise real test

EasyNumPy Programs · 6 of 100

Write a NumPy program to test element-wise for real numbers.

Input
[2, 4, 6+9j, 7]
Output
[ True  True False  True]

Approach

  1. np.isreal().

Solution

Python
import numpy as npprint(np.isreal(np.array([2, 4, 6 + 9j, 7])))
Output
[ True  True False  True]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Element-wise complex test

Element-wise complex test

EasyNumPy Programs · 5 of 100

Write a NumPy program to test element-wise for complex numbers.

Input
[2, 4, 6+9j, 7]
Output
[False False  True False]

Approach

  1. np.iscomplex() is True where the imaginary part is non-zero.

Solution

Python
import numpy as npprint(np.iscomplex(np.array([2, 4, 6 + 9j, 7])))
Output
[False False  True False]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Element-wise NaN test

Element-wise NaN test

EasyNumPy Programs · 4 of 100

Write a NumPy program to test element-wise for NaN.

Input
[2, 4, np.nan, 7]
Output
[False False  True False] [False False False False]

Approach

  1. np.isnan(); x == np.nan never works because NaN is not equal to itself.

Solution

Python
import numpy as npa = np.array([2, 4, np.nan, 7])print(np.isnan(a), a == np.nan)
Output
[False False  True False] [False False False False]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Element-wise finite test

Element-wise finite test

EasyNumPy Programs · 3 of 100

Write a NumPy program to test element-wise whether values are finite.

Input
[6, 8, 3, np.inf]
Output
[ True  True  True False]

Approach

  1. np.isfinite().

Solution

Python
import numpy as npprint(np.isfinite(np.array([6, 8, 3, np.inf])))
Output
[ True  True  True False]
Note

The original sample output was inverted; isfinite is True for normal numbers and False for infinity.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Test that no element is zero

Test that no element is zero

EasyNumPy Programs · 2 of 100

Write a NumPy program to test whether none of the elements of an array is zero.

Input
[6, 8, 3, 5]
Output
True False

Approach

  1. np.all(arr) is True when every element is non-zero.

Solution

Python
import numpy as npprint(np.all(np.array([6, 8, 3, 5])), np.all(np.array([6, 0, 3])))
Output
True False

Line by line

Line 2

Zero counts as False, so a single 0 makes all False.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Get the NumPy version

Get the NumPy version

EasyNumPy Programs · 1 of 100

Write a Python program to get the installed NumPy version.

Output
2.5.4

Approach

  1. np.__version__.

Solution

Python
import numpy as npprint(np.__version__)
Output
2.5.4

Line by line

Line 1

np is the standard alias for NumPy.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Python NumPy Programs with Solutions

Python Practice › NumPy Programs

Python NumPy Programs with Solutions

100 programs100 easy0 medium0 hard

100 NumPy programs: creating arrays and matrices, element-wise tests and comparisons, indexing, reshaping, sorting, set operations, NaN handling, statistics and rounding. The outputs come from NumPy 2.5; the in-browser Try it uses an older NumPy, so a few printouts may look slightly different.

Before you start

Read the NumPy lesson first if the topic is new to you.

All programs