Integers 1 to 100 in steps of 6

Python Practice › NumPy Programs › Program 18

Integers 1 to 100 in steps of 6

EasyNumPy Programs · 18 of 100

Write a NumPy program to create an array of integers from 1 to 100 with a difference of 6.

Output
[ 1  7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97]

Approach

  1. np.arange(1, 101, 6).

Solution

Python
import numpy as npprint(np.arange(1, 101, 6))
Output
[ 1  7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Odd integers from 1 to 20

Python Practice › NumPy Programs › Program 17

Odd integers from 1 to 20

EasyNumPy Programs · 17 of 100

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

Output
[ 1  3  5  7  9 11 13 15 17 19]

Approach

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

Solution

Python
import numpy as npprint(np.arange(1, 21, 2))
Output
[ 1  3  5  7  9 11 13 15 17 19]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

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