Even integers from 1 to 20

Python Practice › NumPy Programs › Program 16

Even integers from 1 to 20

EasyNumPy Programs · 16 of 100

Write a NumPy program to create an array of even integers from 1 to 20.

Output
[ 2  4  6  8 10 12 14 16 18 20]

Approach

  1. np.arange(2, 21, 2).

Solution

Python
import numpy as npprint(np.arange(2, 21, 2))
Output
[ 2  4  6  8 10 12 14 16 18 20]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Integers from 1 to 20

Python Practice › NumPy Programs › Program 15

Integers from 1 to 20

EasyNumPy Programs · 15 of 100

Write a NumPy program to create an array of integers from 1 to 20.

Output
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

Approach

  1. np.arange(1, 21).

Solution

Python
import numpy as npprint(np.arange(1, 21))
Output
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Array of three ones

Python Practice › NumPy Programs › Program 14

Array of three ones

EasyNumPy Programs · 14 of 100

Write a NumPy program to create an array of 3 ones.

Output
[1 1 1] [1. 1. 1.]

Approach

  1. np.ones(3, dtype=int).

Solution

Python
import numpy as npprint(np.ones(3, dtype=int), np.ones(3))
Output
[1 1 1] [1. 1. 1.]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Memory used by an array

Python Practice › NumPy Programs › Program 13

Memory used by an array

EasyNumPy Programs · 13 of 100

Write a NumPy program to find the memory occupied by an array.

Output
5 8 40

Approach

  1. nbytes = number of items × bytes per item.

Solution

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

Line by line

Line 2

The dtype is fixed to int64 so the size is the same on every platform.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

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