Creating Arrays (array, zeros, arange, linspace)

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.

Learn Creating Arrays (array, zeros, arange, linspace) in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice…

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.

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.

Expected output: [1 2 3 4] , the 2×3 matrix, and a shape of (2, 3) .

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).

Expected output: a 2×3 grid of 0. , a row of four 1. values, and a 2×2 grid where every element is 7 .

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.

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.

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.

Expected output: [0. 0.25 0.5 0.75 1. ] , then [0. 5. 10.] , then a 3×3 identity matrix from np.eye(3) .

Replace each ___ so the program builds a zeros grid, a range, and five evenly spaced 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 .)

❌ 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 does not zero the memory; it reuses whatever was there.

✅ Fix: use np.zeros if you need clean values, or overwrite every element first.

Create a 3×3 grid filled with the value 1, then a range of even numbers from 0 to 8.

Lesson 3 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.

🚀 Up next: Array Data Types — control exactly what dtype your arrays use and convert between them.

Practice quiz

What does np.arange(5) return?

arange(5) counts from 0 up to but not including 5.

What does np.arange(2, 10, 2) return?

It steps by 2 from 2 up to but not including 10.

What does np.linspace(0, 1, 5) return?

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.