Create Small Arrays

Choose an array constructor from existing values, a required shape, a start-stop-step rule, or a start-stop-count rule.

An array constructor should express what is known about the data. Sometimes we already have the values. Sometimes we know only a shape, a step, or a requested number of evenly spaced points.

Construct an Array from Existing Values

Use np.array when the values already exist in a Python sequence:

[[18.5 20. ]
 [21.5 22. ]
 [19.  24. ]]
(3, 2)

The tuple (3, 2) states the shape: three rows and two columns. Each nested list contributes one row, and every row has two values.

NumPy usually infers a suitable numerical data type from the supplied values. Use an explicit type only when the task requires that representation. For example, a result table that will hold decimal measurements can begin with floating-point zeros:

results = np.zeros((3, 2), dtype=float)

The shape argument is itself the tuple (3, 2). The keyword dtype=float makes the intended decimal representation visible.

Q1. Build a table from known readings

Create the three-row, two-column array from the given nested list. Then print its shape.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintConvert the complete nested list

Use readings = np.array(values). The row structure is already present in values.

SolutionConstruct the array from existing values
readings = np.array(values)

The outer list has three items and each inner list has two, so the resulting shape is (3, 2).

Not attempted
Review

Not marked done.

Fill a Stated Shape

Use np.zeros or np.ones when the shape and fill value are known before the individual measurements exist:

[[0. 0.]
 [0. 0.]
 [0. 0.]]
[1. 1.]

(3, 2) describes a two-dimensional table. (2,) describes a one-dimensional array with two elements; the comma is what makes it a one-item tuple. We could also pass the integer 2 for this one-dimensional case, but the tuple keeps the shape notation explicit.

Do not choose zeros merely because zero looks harmless. A zero-filled table is suitable only when zero is a valid initial value or a clearly documented placeholder. If zero could be mistaken for an observed measurement, the program needs another representation or an explicit validity record.

Q2. Choose a constructor for a stated shape

A program needs a floating-point table with four observations and two sensors, initially filled with zeros. Which expression states that requirement?

Choose one

Select one choice, then check.

HintWrite rows and columns as a tuple

The requested shape is (4, 2), and every starting value should be zero.

SolutionUse zeros with the stated shape

np.zeros((4, 2), dtype=float) produces four rows, two columns, and a floating-point zero in every position.

Not attempted
Review

Not marked done.

Express a Step or a Count

Two constructors create evenly spaced one-dimensional sequences, but they ask different questions.

np.arange(start, stop, step) uses a step size and excludes the stop value:

[ 0  5 10]

Read the call as “start at 0, stop before 15, moving by 5.” The stop is a boundary, not a promised element.

np.linspace(start, stop, count) uses a requested count and includes both endpoints under its default behavior:

[0.   0.25 0.5  0.75 1.  ]

Read this call as “give me five evenly spaced values from 0.0 through 1.0.” The third argument is a count, not a step size.

Known ruleConstructorEndpoint behavior used here
start, stop boundary, and steparangestop is excluded
start, included stop, and number of valueslinspaceboth endpoints are included

With non-integer steps, accumulated floating-point approximations can make an arange endpoint easy to misread. When the required number of samples and both endpoints matter, linspace states the question more directly.

Q3. Choose between step and count

Create four evenly spaced values from 0.0 through 1.5, including both endpoints. The result should be [0.0, 0.5, 1.0, 1.5].

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintThe task states a count and both endpoints

Use np.linspace with start 0.0, stop 1.5, and count 4.

SolutionRequest four included positions
levels = np.linspace(0.0, 1.5, 4)

The interval has three equal gaps of 0.5, so four positions include both endpoints.

Not attempted
Review

Not marked done.

Use np.array for known values, zeros or ones for a known filled shape, arange for a start/stop/step rule, and linspace for a start/stop/count rule. Once an array exists, its dimensions and numerical representation need names before later operations can be read safely.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.