← Back to list

NumPy Hands-On Tutorial -Chapter 3: Ways of Creating NumPy Arrays

Creating arrays is one of the most fundamental operations in NumPy. Since NumPy is designed for efficient numerical computing, it provides…

RePromptsQuest (Reddit Username: OKAccess6128) · 2026-06-20 13:53 · 0 claps · 5.1 min read
#python #python-programming #python-libraries #numpy #numpy-tutorial
Open on Medium ↗
Wiki topics: 💻 · Programming

NumPy Hands-On Tutorial -Chapter 3: Ways of Creating NumPy Arrays

Chapter 3: Ways of Creating NumPy Arrays

Chapter 3: Ways of Creating NumPy Arrays

Creating arrays is one of the most fundamental operations in NumPy. Since NumPy is designed for efficient numerical computing, it provides many powerful methods for generating arrays quickly and efficiently.

These methods allow you to:

  • Convert Python data structures into arrays
  • Generate arrays filled with specific values
  • Create sequences of numbers
  • Generate evenly spaced numeric ranges
  • Produce random datasets for simulations and machine learning

This article explains all major NumPy array creation methods with practical examples.

1. Creating Arrays from Python Lists

The most common way to create a NumPy array is by converting a Python list using the np.array() function.

Example

import numpy as np
data = [10, 20, 30, 40]
arr = np.array(data)
print(arr)
print(type(arr))

Output

[10 20 30 40]
<class 'numpy.ndarray'>

Why Convert Lists to Arrays?

Python lists:

  • Store mixed data types
  • Are slower for numerical operations

NumPy arrays:

  • Store homogeneous data
  • Support fast vectorized operations

Creating Multi-Dimensional Arrays from Lists

Lists of lists create 2D arrays.

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
print(matrix)

Output

[[1 2 3]
 [4 5 6]]

Here:

Rows = 2
Columns = 3

2. Creating Arrays from Tuples

Tuples can also be converted into NumPy arrays.

Example

import numpy as np
data = (5, 10, 15, 20)
arr = np.array(data)
print(arr)

Output

[ 5 10 15 20]

Multi-Dimensional Tuple Example

data = (
    (1,2,3),
    (4,5,6)
)
arr = np.array(data)
print(arr)

Output

[[1 2 3]
 [4 5 6]]

Both lists and tuples are commonly used to initialize arrays.

3. Creating Arrays using array()

The np.array() function is the primary array constructor.

Syntax

np.array(object, dtype=None)

Parameters

+-----------+---------------------------------------------+
| Parameter | Description                                 |
+-----------+---------------------------------------------+
| object    | Input data (list, tuple, etc.)              |
| dtype     | Optional data type                          |
+-----------+---------------------------------------------+

Example

import numpy as np
arr = np.array([1, 2, 3, 4], dtype=float)
print(arr)
print(arr.dtype)

Output

[1. 2. 3. 4.]
float64

Creating Higher Dimensional Arrays

arr = np.array([
    [[1,2],[3,4]],
    [[5,6],[7,8]]
])
print(arr)

Output

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

This is a 3D array.

4. Creating Arrays with zeros()

The zeros() function creates an array filled with 0 values.

Syntax

np.zeros(shape)

Example: 1D Array

import numpy as np
arr = np.zeros(5)
print(arr)

Output

[0. 0. 0. 0. 0.]

Example: 2D Array

arr = np.zeros((3,4))
print(arr)

Output

[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

Practical Use Case

Initialize matrices before filling values.

Example:

matrix = np.zeros((5,5))

Used in:

  • Image processing
  • Scientific simulations
  • Machine learning tensors

5. Creating Arrays with ones()

The ones() function creates arrays filled with 1 values.

Syntax

np.ones(shape)

Example

import numpy as np
arr = np.ones(4)
print(arr)

Output

[1. 1. 1. 1.]

Example: 2D Matrix

arr = np.ones((2,3))
print(arr)

Output

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

Practical Use

  • Weight initialization
  • Masking operations
  • Placeholder arrays

6. Creating Arrays with empty()

The empty() function creates an array without initializing values.

Syntax

np.empty(shape)

It allocates memory but does not set values.

Example

import numpy as np
arr = np.empty(4)
print(arr)

Output (random memory values)

[6.945e-310 6.945e-310 6.945e-310 6.945e-310]

2D Example

arr = np.empty((2,2))
print(arr)

Values depend on previous memory state.

Why Use empty()?

It is faster than zeros() or ones() because it skips initialization.

Useful when you plan to fill values immediately.

7. Creating Arrays with full()

The full() function creates arrays filled with a specific value.

Syntax

np.full(shape, value)

Example

import numpy as np
arr = np.full(5, 7)
print(arr)

Output

[7 7 7 7 7]

Example: 2D

arr = np.full((2,3), 9)
print(arr)

Output

[[9 9 9]
 [9 9 9]]

Practical Use

Creating constant arrays.

Example:

temperature grid
initial simulation state
constant matrices

8. Creating Arrays using arange()

The arange() function works like Python's range() but returns a NumPy array.

Syntax

np.arange(start, stop, step)

Example

import numpy as np
arr = np.arange(0, 10)
print(arr)

Output

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

Example with Step

arr = np.arange(0, 20, 2)
print(arr)

Output

[ 0  2  4  6  8 10 12 14 16 18]

Practical Use

Used for:

  • Generating index arrays
  • Creating sequences
  • Iteration ranges

9. Creating Arrays using linspace()

The linspace() function generates evenly spaced numbers between two limits.

Syntax

np.linspace(start, stop, num)

Example

import numpy as np
arr = np.linspace(0, 1, 5)
print(arr)

Output

[0.   0.25 0.5  0.75 1.  ]

Meaning:

5 numbers between 0 and 1

Example

arr = np.linspace(10, 50, 5)
print(arr)

Output

[10. 20. 30. 40. 50.]

Practical Uses

  • Plotting graphs
  • Numerical simulations
  • Machine learning parameter grids

10. Creating Arrays using logspace()

The logspace() function generates numbers evenly spaced on a logarithmic scale.

Syntax

np.logspace(start, stop, num)

The numbers represent powers of base 10.

Example

import numpy as np
arr = np.logspace(1, 3, 4)
print(arr)

Output

[   10.   100.  1000. 10000.]

Explanation

10^1
10^2
10^3
10^4

Practical Uses

  • Scientific computing
  • Logarithmic plots
  • Signal processing

11. Creating Identity Matrices

An identity matrix is a square matrix where:

  • Diagonal values = 1
  • Other values = 0

Example

1 0 0
0 1 0
0 0 1

Using identity()

import numpy as np
arr = np.identity(4)
print(arr)

Output

[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

Practical Use

Identity matrices are used in:

  • Linear algebra
  • Matrix multiplication
  • Machine learning algorithms

12. Creating Arrays with eye()

eye() also creates an identity matrix but allows more control.

Syntax

np.eye(rows, columns)

Example

import numpy as np
arr = np.eye(3)
print(arr)

Output

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

Rectangular Identity Matrix

arr = np.eye(3,5)
print(arr)

Output

[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]]

13. Creating Arrays with Random Values

NumPy provides powerful random number generation functions.

Random Array (Uniform Distribution)

import numpy as np
arr = np.random.rand(3,3)
print(arr)

Example Output

[[0.34 0.81 0.66]
 [0.59 0.12 0.92]
 [0.77 0.41 0.55]]

Values range between 0 and 1.

Random Integers

arr = np.random.randint(1,10,(3,3))
print(arr)

Example Output

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

Random Normal Distribution

arr = np.random.randn(3,3)
print(arr)

Produces numbers from a Gaussian distribution.

Setting Random Seed

For reproducible results:

np.random.seed(42)
print(np.random.rand(3))

Every run produces the same numbers.

Practical Example Combining Methods

import numpy as np
zeros_array = np.zeros((2,2))
ones_array = np.ones((2,2))
range_array = np.arange(0,10)
lin_array = np.linspace(0,1,5)
random_array = np.random.rand(2,2)
print("Zeros\n", zeros_array)
print("Ones\n", ones_array)
print("Range\n", range_array)
print("Linspace\n", lin_array)
print("Random\n", random_array)

Summary

NumPy provides powerful functions for creating arrays efficiently.

Common creation methods include:

+-------------------+----------------------------------------------+
| Function          | Purpose                                      |
+-------------------+----------------------------------------------+
| array()           | Convert lists/tuples to arrays               |
| zeros()           | Create arrays filled with 0                  |
| ones()            | Create arrays filled with 1                  |
| empty()           | Create uninitialized arrays                 |
| full()            | Fill arrays with a specific value           |
| arange()          | Generate numerical sequences               |
| linspace()        | Generate evenly spaced numbers             |
| logspace()        | Generate logarithmic scale numbers         |
| identity()        | Create identity matrices                   |
| eye()             | Create flexible identity matrices          |
| random functions  | Generate arrays with random values         |
+-------------------+----------------------------------------------+

These functions are used constantly in:

  • Data science
  • Machine learning
  • Simulations
  • Numerical computing

Practice Examples

21. Create an array using np.empty() of size 5:

  • Print the array
  • Observe values
  • Assign [1,2,3,4,5] manually
  • Print again

22. Create a 2×3 array filled with 7 using np.full():

  • Print the array
  • Verify all values are identical

23. Use np.logspace() to generate 4 values from 10¹ to 10⁴:

  • Print the array
  • Explain value generation

24. Create:

  • A 4×4 identity matrix using np.identity()
  • A 3×5 matrix using np.eye()

Print both and compare.

25. Generate random arrays:

  • np.random.rand(2,2)
  • np.random.randint(1,10, (2,2))

Set a random seed and regenerate one array. Print all outputs.

Thanks for reading this article, for such more articles do follow RePromptsQuest.


메타데이터
post_id
4e2dccff90ce
slug
numpy-hands-on-tutorial-chapter-3-ways-of-creating-numpy-arrays-4e2dccff90ce
url
https://medium.com/@repromptsquest/numpy-hands-on-tutorial-chapter-3-ways-of-creating-numpy-arrays-4e2dccff90ce
canonical_url
https://medium.com/@repromptsquest/numpy-hands-on-tutorial-chapter-3-ways-of-creating-numpy-arrays-4e2dccff90ce
author_url
https://medium.com/@repromptsquest
status
ok
fetched_at
2026-06-26 21:52:29