Create an array from values

Python Practice › NumPy Programs › Program 12

Create an array from values

EasyNumPy Programs · 12 of 100

Write a NumPy program to create an array with the values 10, 34, 86, 26, 56.

Output
[10 34 86 26 56] int64

Approach

  1. np.array(list).

Solution

Python
import numpy as nparr = np.array([10, 34, 86, 26, 56])print(arr, arr.dtype)
Output
[10 34 86 26 56] int64

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Element-wise less than

Python Practice › NumPy Programs › Program 11

Element-wise less than

EasyNumPy Programs · 11 of 100

Write a NumPy program for an element-wise “less than” comparison.

Input
[6, 4], [2, 9]
Output
[False  True]

Approach

  1. np.less.

Solution

Python
import numpy as npprint(np.less([6, 4], [2, 9]))
Output
[False  True]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Element-wise greater or equal

Python Practice › NumPy Programs › Program 10

Element-wise greater or equal

EasyNumPy Programs · 10 of 100

Write a NumPy program for an element-wise “greater or equal” comparison.

Input
[6, 4], [6, 3]
Output
[ True  True]

Approach

  1. np.greater_equal.

Solution

Python
import numpy as npprint(np.greater_equal([6, 4], [6, 3]))
Output
[ True  True]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Element-wise greater than

Element-wise greater than

EasyNumPy Programs · 9 of 100

Write a NumPy program for an element-wise “greater than” comparison.

Input
[8, 3], [9, 1]
Output
[False  True]

Approach

  1. np.greater(a, b) or a > b.

Solution

Python
import numpy as npprint(np.greater([8, 3], [9, 1]))
Output
[False  True]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Are two arrays element-wise equal?

Are two arrays element-wise equal?

EasyNumPy Programs · 8 of 100

Write a NumPy program to test whether two arrays are element-wise equal.

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

Approach

  1. == compares element by element; np.array_equal gives one answer.

Solution

Python
import numpy as npa, b = np.array([3, 8, 5]), np.array([3, 8, 5])print(a == b, np.array_equal(a, b))
Output
[ True  True  True] True

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Is a value a scalar?

Is a value a scalar?

EasyNumPy Programs · 7 of 100

Write a NumPy program to test whether a value is a scalar.

Input
8.9, [8.9]
Output
True False

Approach

  1. np.isscalar().

Solution

Python
import numpy as npprint(np.isscalar(8.9), np.isscalar([8.9]))
Output
True False

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

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