Numpy Arrays Walk Through

A quick reference to the rudiments of np arrays

Adam Ross Nelson
8 min readDec 1, 2023

--

One of the primary functions of NumPy is to provide access to array data types. The library supports fast and quick creation of example arrays. They’re powerful for many fields. And they’re important for data scientists. Let’s take a look at how they work.

The first thing we’ll do is import numpy as np after that we can create an array of all zeroes. This is going to create array with 4 items. Allitems will be zeroes.

import numpy as np

# Create an array of 4 zeros (0s)
np.zeros(4)

For the expected output:

array([0., 0., 0., 0.])

Next we can also show the simple process of created a one dimensional array with a specific starting position, a specific stopping position, and a step or interval in the sequence.

The np.arange() function is used to create an array with evenly spaced values within a defined interval. The syntax of the function is np.arange(start, stop, step), where:

  • start is the starting value of the sequence (inclusive).
  • stop is the end value of the sequence (exclusive).
  • step is the spacing between each two consecutive values.

By default, start is 0, and step is 1 if they are not provided. The stop parameter is required. The np.arange() function can handle all numerical types and returns an array of a specific type inferred from the input types.

import numpy as np

# Array of integers from 0 to 9
np.arange(10)

# Array of even nums between 10 (inclusive) & 20 (exclusive)
even_numbers = np.arange(10, 20, 2)
even_numbers

For the expected output (respectively):

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

array([10, 12, 14, 16, 18])

Arrays vs. Python Lists

Note how NumPy arrays look a lot like Python lists. Looks are decieving here. They are different.

Type of Elements

NumPy Arrays: They are homogeneous, meaning all elements must be of the same data type. This constraint allows for more efficient memory usage and computational operations. Python Lists: They are heterogeneous, meaning they can contain elements of different data types within the same list (e.g., integers, strings, objects). For example, in a…

--

--

Adam Ross Nelson

Ask about my free career course. I mentor new (💫) and aspiring data scientists enter (🚪) and level up (📈) in the field.