0Pricing
Learn AI with Python · Lesson

Array Creation and Properties

np.array(), np.zeros/ones/eye/arange/linspace, shape, dtype, ndim, size.

Array Creation and Properties is a free Learn AI with Python 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 Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The ndarray

NumPy's core object is the ndarray: a fixed-size, homogeneous grid of numbers stored in contiguous memory. This layout is what makes NumPy fast.

import numpy as np
a = np.array([1, 2, 3, 4])
print(a)   # [1 2 3 4]

From Nested Lists

Pass a list of lists to build a 2D array. NumPy infers the shape from the structure.

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

Zeros and Ones

np.zeros and np.ones pre-allocate arrays of a given shape, the standard way to create placeholders before filling them.

print(np.zeros((2, 3)))   # 2x3 of 0.0
print(np.ones(4))         # [1. 1. 1. 1.]

Identity with eye

np.eye(n) builds an n by n identity matrix: ones on the diagonal, zeros elsewhere. Essential for linear algebra.

print(np.eye(3))
# [[1. 0. 0.]
#  [0. 1. 0.]
#  [0. 0. 1.]]

Ranges with arange

np.arange(start, stop, step) works like Python's range but returns an array and accepts float steps.

print(np.arange(0, 10, 2))   # [0 2 4 6 8]
print(np.arange(0, 1, 0.25))  # [0.   0.25 0.5  0.75]

Even Spacing with linspace

np.linspace(start, stop, num) returns num evenly spaced points, inclusive of both ends. Perfect for plotting axes.

print(np.linspace(0, 1, 5))   # [0.   0.25 0.5  0.75 1.  ]

arange vs linspace

Use arange when you know the step; use linspace when you know the count of points. linspace avoids floating-point step drift.

# 50 points from 0 to 2*pi for a smooth curve
x = np.linspace(0, 2 * np.pi, 50)

Shape and ndim

.shape is a tuple of dimension sizes; .ndim is the number of dimensions. A 2x3 array has shape (2,3) and ndim 2.

m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.shape)   # (2, 3)
print(m.ndim)    # 2

size and dtype

.size is the total element count; .dtype is the element data type. NumPy picks an efficient numeric dtype automatically.

print(m.size)    # 6
print(m.dtype)   # int64

itemsize and Memory

.itemsize is the byte size of one element. Multiply by .size to estimate the array's memory footprint.

print(m.itemsize)          # 8 (bytes per int64)
print(m.size * m.itemsize) # 48 bytes total

Choosing a dtype

You can force a dtype to save memory or ensure precision. float32 halves memory versus float64.

a = np.array([1, 2, 3], dtype=np.float32)
print(a.dtype)      # float32
print(a.itemsize)   # 4

Quick Check

Test your array creation knowledge.

Recap

Array creation toolkit:

  • np.array from lists, np.zeros/np.ones placeholders, np.eye identity
  • np.arange (by step) vs np.linspace (by count)
  • Properties: shape, ndim, size, dtype, itemsize

Frequently asked questions

Is the “Array Creation and Properties” lesson free?

Yes — the full text of “Array Creation and Properties” is free to read here on the web, and the Learn AI with Python 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 Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Array Creation and Properties”?

np.array(), np.zeros/ones/eye/arange/linspace, shape, dtype, ndim, size. You practise Learn AI with Python 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 Learn AI with Python?

No prior experience is required. Learn AI with Python 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 “Array Creation and Properties” 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 Learn AI with Python lesson?

Yes. Every Learn AI with Python 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

  1. Array Creation and Properties
  2. Indexing, Slicing, and Fancy Indexing
  3. Broadcasting and Vectorized Operations
  4. Linear Algebra with NumPy
← Back to Learn AI with Python