Creating Arrays (array, zeros, arange, linspace)
Reviewed & published by Brayan K
Creating arrays in NumPy means turning data — or just a shape and a pattern — into an ndarray, and NumPy gives you a small family of factory functions like array, zeros, ones, arange, and linspace to do exactly that.
Part of the free NumPy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll learn the most common ways to build arrays: from existing lists, from a shape filled with a constant, from a numeric range, and from evenly spaced points.
What You'll Learn in This Lesson
- Building arrays from lists with np.array
- Filling shapes with np.zeros, np.ones, np.full
- Number ranges with np.arange
- Evenly spaced points with np.linspace
- Identity matrices with np.eye and np.identity
- The np.empty caveat
1 From Lists with np.array
The most direct way to make an array is np.array(), which converts a Python list (or a list of lists) into an ndarray. A flat list becomes a 1D array; a list of lists becomes a 2D array.
import numpy as np
a = np.array([1, 2, 3, 4])
print(a) # [1 2 3 4]
m = np.array([[1, 2, 3],
[4, 5, 6]])
print(m)
# [[1 2 3]
# [4 5 6]]
print(m.shape) # (2, 3)
# ✅ Expected output:
# [1 2 3 4]
# [[1 2 3]
# [4 5 6]]
# (2, 3)Expected output: [1 2 3 4], the 2×3 matrix, and a shape of (2, 3).
2 Filled Arrays: zeros, ones, full
Often you want an array of a certain shape pre-filled with a constant. Pass a shape tuple to np.zeros (all 0.0), np.ones (all 1.0), or np.full (any value you choose).
import numpy as np
print(np.zeros((2, 3)))
# [[0. 0. 0.]
# [0. 0. 0.]]
print(np.ones(4))
# [1. 1. 1. 1.]
print(np.full((2, 2), 7))
# [[7 7]
# [7 7]]
# ✅ Expected output:
# [[0. 0. 0.]
# [0. 0. 0.]]
# [1. 1. 1. 1.]
# [[7 7]
# [7 7]]Expected output: a 2×3 grid of 0., a row of four 1. values, and a 2×2 grid where every element is 7.
3 Number Ranges with np.arange
np.arange(start, stop, step) works like Python's range() but returns an array. It counts from start up to (but not including) stop, jumping by step. Unlike range, the step can be a float.
import numpy as np
print(np.arange(5)) # [0 1 2 3 4]
print(np.arange(2, 10, 2)) # [2 4 6 8]
print(np.arange(0, 1, 0.25)) # [0. 0.25 0.5 0.75]
# ✅ Expected output:
# [0 1 2 3 4]
# [2 4 6 8]
# [0. 0.25 0.5 0.75]Expected output: [0 1 2 3 4], then [2 4 6 8], then [0. 0.25 0.5 0.75] — the stop value is never included.
4 Evenly Spaced Points with np.linspace
When you know how many points you want rather than the step, use np.linspace(start, stop, num). It returns num evenly spaced values and, by default, includes the stop endpoint.
import numpy as np
print(np.linspace(0, 1, 5)) # [0. 0.25 0.5 0.75 1. ]
print(np.linspace(0, 10, 3)) # [ 0. 5. 10.]
# eye / identity make identity matrices
print(np.eye(3))
# [[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]
# ✅ Expected output:
# [0. 0.25 0.5 0.75 1. ]
# [ 0. 5. 10.]
# [[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]Expected output: [0. 0.25 0.5 0.75 1. ], then [0. 5. 10.], then a 3×3 identity matrix from np.eye(3).
🎯 YOUR TURN: Fill in the Blanks
Replace each ___ so the program builds a zeros grid, a range, and five evenly spaced points.
import numpy as np
grid = np.___((2, 3)) # fill in the function for an all-zeros array
print(grid) # a 2x3 grid of 0.
steps = np.arange(0, 10, ___) # fill in the step to get [0 2 4 6 8]
print(steps)
points = np.linspace(0, 1, ___) # fill in num to get 5 points: [0. 0.25 0.5 0.75 1.]
print(points)Expected output: a 2×3 zeros grid, [0 2 4 6 8], and [0. 0.25 0.5 0.75 1. ]. (Answers: zeros, 2, 5.)
⚠️ Common Errors & Quick Reference
❌ TypeError: Cannot interpret '2' as a data type
You wrote np.zeros(2, 3) — NumPy read the 3 as the dtype.
✅ Fix: pass the shape as a tuple: np.zeros((2, 3)).
❌ np.empty returned random-looking values
np.empty does not zero the memory; it reuses whatever was there.
✅ Fix: use np.zeros if you need clean values, or overwrite every element first.
| Task | Code |
|---|---|
| From a list | np.array([1, 2, 3]) |
| All zeros, 2×3 | np.zeros((2, 3)) |
| A range | np.arange(0, 10, 2) |
| 5 evenly spaced points | np.linspace(0, 1, 5) |
🏆 Mini Challenge: Build a Starter Grid
Create a 3×3 grid filled with the value 1, then a range of even numbers from 0 to 8.
import numpy as np
board = np.full((3, 3), 1)
print(board)
# [[1 1 1]
# [1 1 1]
# [1 1 1]]
evens = np.arange(0, 9, 2)
print(evens) # [0 2 4 6 8]
# ✅ Expected output:
# [[1 1 1]
# [1 1 1]
# [1 1 1]]
# [0 2 4 6 8]❓ Frequently Asked Questions
Lesson complete — you can build any array!
You can now create arrays from lists, fill a shape with zeros, ones, or any constant, generate ranges with arange, and lay down evenly spaced points with linspace.
Practice quiz
What does np.arange(5) return?
- [1 2 3 4 5]
- [0 1 2 3 4 5]
- [5]
- [0 1 2 3 4]
Answer: [0 1 2 3 4]. arange(5) counts from 0 up to but not including 5.
What does np.arange(2, 10, 2) return?
- [2 4 6 8 10]
- [0 2 4 6 8]
- [2 4 6 8]
- [2 4 6 8 9]
Answer: [2 4 6 8]. It steps by 2 from 2 up to but not including 10.
What does np.linspace(0, 1, 5) return?
- [0 1 2 3 4]
- [0. 0.2 0.4 0.6 0.8]
- [0. 0.25 0.5 0.75 1.]
- [0.25 0.5 0.75]
Answer: [0. 0.25 0.5 0.75 1.]. linspace gives 5 evenly spaced points including the endpoint 1.
What does np.full((2, 2), 7) create?
- A 2x2 array of all 7s
- A 7x7 array
- A 1D array of two 7s
- An identity matrix
Answer: A 2x2 array of all 7s. np.full fills the given shape with the constant value 7.
What is the key difference between np.arange and np.linspace?
- arange is faster always
- linspace cannot use floats
- arange returns lists
- arange uses a step size; linspace uses a count of points
Answer: arange uses a step size; linspace uses a count of points. Use arange when you know the step, linspace when you know how many points.
What does np.zeros((2, 3)) create?
- A 3x2 array
- An error
- A 2x3 array filled with 0.0
- A 1D array of zeros
Answer: A 2x3 array filled with 0.0. Passing the shape tuple makes a 2-row, 3-column array of 0.0.
By default, does np.linspace include the stop endpoint?
- Only for integers
- Yes, it includes stop by default
- No, it always excludes stop
- Only if num is odd
Answer: Yes, it includes stop by default. linspace includes the stop value by default (endpoint=True).
Why might np.empty contain strange values?
- It does not initialize the allocated memory
- It fills with random integers 0-9
- It always uses -1
- It copies another array
Answer: It does not initialize the allocated memory. np.empty leaves whatever was already in memory, so you must overwrite it.
How do you create a 3x3 identity matrix?
- np.zeros((3, 3))
- np.full((3, 3), 1)
- np.ones((3, 3))
- np.eye(3)
Answer: np.eye(3). np.eye(3) makes a 3x3 matrix with 1s on the diagonal.
What is the default dtype of np.zeros and np.ones?
- int32
- int64
- float64
- uint8
Answer: float64. Both default to float64, so values print as 0. and 1.
Continue this course
- Previous: The ndarray: NumPy's Core Object
- Next: Images as Arrays: Shape, uint8 & Channels — Create a blank canvas, address pixels, and split RGB channels
- Quick reference: NumPy cheat sheet › Creating Arrays
Frequently asked questions
What is the difference between np.arange and np.linspace?
np.arange(start, stop, step) picks values by a fixed step size and stops before the stop value. np.linspace(start, stop, num) picks an exact number of evenly spaced values and, by default, includes the stop value. Use arange when you know the step, linspace when you know how many points you want.
What does np.zeros and np.ones do?
np.zeros(shape) creates an array of the given shape filled entirely with 0.0, and np.ones(shape) fills it with 1.0. Both default to the float64 dtype. They are handy for pre-allocating arrays you will fill in later.
Why does np.empty contain strange numbers?
np.empty does not initialize the memory it allocates, so it returns whatever values happened to already be in that memory. It is slightly faster than np.zeros, but you must overwrite every element before using it.
How do I create a 2D array of a given shape?
Pass a shape tuple. For example np.zeros((2, 3)) makes a 2-row, 3-column array of zeros, and np.full((2, 2), 7) makes a 2x2 array where every value is 7.