Creating NumPy Arrays
Create 1-D and 2-D arrays with np.array, np.zeros, np.ones, and np.arange, then inspect their shape and dtype.
Creating NumPy Arrays is a free Pandas & NumPy Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Pandas & NumPy Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a NumPy Array?
NumPy is Python's go-to library for number crunching. Its core is the ndarray — a grid of same-typed values that's far faster and leaner than a list.
import numpy as np
# NumPy arrays are fast, typed, and memory-efficient
print(np.__version__)Creating Arrays from Lists
The simplest way to make an array: pass a list to np.array(). NumPy picks the dtype for you, and a list of lists becomes a 2-D matrix.
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a) # [1 2 3 4 5]
print(a.dtype) # int64
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.shape) # (2, 3)np.zeros and np.ones
Need a blank slate? np.zeros fills an array with 0.0 and np.ones fills it with 1.0 — perfect for setting up an array before you compute its real values.
import numpy as np
z = np.zeros(5)
print(z) # [0. 0. 0. 0. 0.]
o = np.ones((3, 4))
print(o.shape) # (3, 4)
print(o.dtype) # float64np.arange for Integer Sequences
np.arange works like Python's range but returns an array. Give it a start, stop, and step — the stop value isn't included.
import numpy as np
a = np.arange(0, 10, 2)
print(a) # [0 2 4 6 8]
b = np.arange(1.0, 2.0, 0.25)
print(b) # [1. 1.25 1.5 1.75]np.linspace for Evenly-Spaced Floats
Want evenly spaced floats? np.linspace gives you a set number of values between two endpoints, both included — no rounding drift.
import numpy as np
x = np.linspace(0, 1, 5)
print(x) # [0. 0.25 0.5 0.75 1. ]
y = np.linspace(0, 360, 7)
print(y) # [ 0. 60. 120. 180. 240. 300. 360.]np.full and np.eye
Use np.full to fill an array with any constant you like, and np.eye to build an identity matrix — ones on the diagonal, zeros everywhere else.
import numpy as np
f = np.full((2, 3), 7)
print(f)
# [[7 7 7]
# [7 7 7]]
I = np.eye(3)
print(I)
# [[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]Specifying dtype at Creation
Every array has one dtype for all its values. Set it yourself with dtype= — picking float32 over float64, for example, can cut memory in half.
import numpy as np
a = np.array([1, 2, 3], dtype=np.float32)
print(a.dtype) # float32
print(a.nbytes) # 12 (3 elements x 4 bytes)
b = np.zeros(4, dtype=np.int32)
print(b.dtype) # int32Random Arrays with np.random
Need random data? np.random generates it for you. Set a seed first so you get the same numbers every run — key for repeatable experiments. 🎲
import numpy as np
np.random.seed(42)
u = np.random.rand(3, 3) # uniform [0, 1)
print(u)
n = np.random.randn(5) # standard normal
print(n.round(2))Checking Array Shape and Dimensions
After making an array, you'll usually check its shape — a tuple of sizes per dimension. Use .ndim to see how many dimensions it has.
import numpy as np
a = np.arange(12)
print(a.shape) # (12,)
print(a.ndim) # 1
m = a.reshape(3, 4)
print(m.shape) # (3, 4)
print(m.ndim) # 2Converting Lists and Tuples
np.array() takes more than lists — tuples, ranges, even other arrays all work. Just avoid ragged inner lists, which kill NumPy's speed.
import numpy as np
# From tuple
t = np.array((10, 20, 30))
print(t) # [10 20 30]
# From range
r = np.array(range(5))
print(r) # [0 1 2 3 4]
# Nested lists -> 2D
m = np.array([[1, 2], [3, 4], [5, 6]])
print(m.shape) # (3, 2)np.empty for Uninitialised Arrays
np.empty grabs memory without filling it, so it's a touch faster than zeros. The catch: it holds leftover garbage — always write before you read.
import numpy as np
buf = np.empty(5)
# Values are garbage — do NOT read before writing
for i in range(5):
buf[i] = i * 2
print(buf) # [0. 2. 4. 6. 8.]Quick Check
Test your understanding of NumPy array creation from this lesson.
Lesson Recap
Nice work! You can turn lists into arrays with np.array(), and build them from scratch with zeros, ones, arange, and linspace. Next up: array attributes.
Frequently asked questions
Is the “Creating NumPy Arrays” lesson free?
Yes — the full text of “Creating NumPy Arrays” is free to read here on the web, and the Pandas & NumPy Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Pandas & NumPy Academy course, upgrade to CoddyKit PRO.
What will I learn in “Creating NumPy Arrays”?
Create 1-D and 2-D arrays with np.array, np.zeros, np.ones, and np.arange, then inspect their shape and dtype. You practise Pandas & NumPy Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Pandas & NumPy Academy?
No prior experience is required. Pandas & NumPy Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Creating NumPy Arrays” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Pandas & NumPy Academy lesson?
Yes. Every Pandas & NumPy Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating NumPy Arrays
- Array Attributes and Inspection
- Element-Wise Arithmetic
- Array Slicing and Indexing