Five standard normal numbers

Python Practice › NumPy Programs › Program 22

Five standard normal numbers

EasyNumPy Programs · 22 of 100

Write a NumPy program to generate 5 random numbers from a standard normal distribution.

Output
[ 0.001  0.299 -0.274 -0.891 -0.455]

Approach

  1. rng.standard_normal(5).

Solution

Python
import numpy as nprng = np.random.default_rng(7)print(rng.standard_normal(5).round(3))
Output
[ 0.001  0.299 -0.274 -0.891 -0.455]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Random numbers between 1 and 2

Python Practice › NumPy Programs › Program 21

Random numbers between 1 and 2

EasyNumPy Programs · 21 of 100

Write a NumPy program to generate random numbers between 1 and 2.

Output
[1.774 1.439 1.859 1.697 1.094]

Approach

  1. rng.uniform(1, 2, size) with a seeded generator.

Solution

Python
import numpy as nprng = np.random.default_rng(42)print(rng.uniform(1, 2, 5).round(3))
Output
[1.774 1.439 1.859 1.697 1.094]

Line by line

Line 2

The seed makes the numbers repeatable.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

3 × 3 identity matrix

Python Practice › NumPy Programs › Program 20

3 × 3 identity matrix

EasyNumPy Programs · 20 of 100

Write a NumPy program to create a 3 × 3 identity matrix.

Output
[[1 0 0]
 [0 1 0]
 [0 0 1]]

Approach

  1. np.eye(3).

Solution

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

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

3 × 3 matrix

Python Practice › NumPy Programs › Program 19

3 × 3 matrix

EasyNumPy Programs · 19 of 100

Write a NumPy program to create a 3 × 3 matrix.

Output
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Approach

  1. arange(9).reshape(3, 3).

Solution

Python
import numpy as npprint(np.arange(1, 10).reshape(3, 3))
Output
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

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