NumPy Creating Arrays

0-D, 1-D, 2-D arrays and helper functions.

✍️ 3 exercises + code challenge in this lesson
  • np.array([...]) from a list; nested lists make 2-D arrays.
  • ndim gives the number of dimensions.
  • Helpers: np.zeros(), np.ones(), np.arange(start, stop, step), np.linspace(a, b, n), np.eye(n)

Key Takeaways: NumPy Creating Arrays

Mastering NumPy Creating Arrays helps you write clean and reliable NumPy code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

NumPy Creating Arrays Code Example

NumPy
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix)
print("dimensions:", matrix.ndim)
print(np.zeros(3))
print(np.arange(0, 10, 2))
print(np.linspace(0, 1, 5))
Output
[[1 2 3]
 [4 5 6]]
dimensions: 2
[0. 0. 0.]
[0 2 4 6 8]
[0.   0.25 0.5  0.75 1.  ]
Free sign-in required · Google login available

NumPy Creating Arrays Practice Exercises

Check what you learned in “NumPy Creating Arrays”.

/ 3 solved
Fill in the blank

Create the numbers 0, 2, 4, 6, 8.

np.(0, 10, 2)
✓ Correct! Not quite. Try again.
Multiple choice

What does ndim tell you?

✓ Correct! Not quite. Try again.
Code challenge

Create a 2-D array [[1, 2], [3, 4]] and print its number of dimensions.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services