Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Array Creation | NumPy Basics
Ultimate NumPy

Basic Array CreationBasic Array Creation

A NumPy array is an efficient, multidimensional container for storing and manipulating large datasets of homogeneous (same) data types. Although they are similar to Python lists, they are more memory-efficient and allow for high-performance mathematical and numerical operations.

Now, it’s time to create your first NumPy arrays. The most straightforward way to do this is by using the array() function, passing either a list or a tuple as its argument.

Note

You should create NumPy arrays only from lists in all the tasks throughout our course.

Let’s take a look at an example:

As you can see, these two NumPy arrays are equal.

Specifying Data Type

The data type of the array elements is defined implicitly; however, you can specify it explicitly with the dtype parameter:

The first integer array uses the default int64 data type, which is an 8-byte integer. The second array uses int8, a 1-byte integer.

Other common NumPy data types include numpy.float16, numpy.float32, and numpy.float64, which store 2-byte, 4-byte, and 8-byte floating point numbers, respectively.

Using int8 for small integers is more memory-efficient. However, be careful when choosing the data type (dtype) to avoid overflow if the numbers are too large for the specified type.

Here is a list of possible data types in NumPy:

dtype range significant digits
np.int8 -128...127
np.int16 -32768...32768
np.int32 -2.1 * 109...2.1 * 109
np.int64 -9.2 * 1018...9.2 * 1018
np.float16 ±(6.0 * 10-8...65504) 3
np.float32 ±(1.4 * 10-45...3.4 * 1038) 6
np.float64 ±(4.9 * 10-324...1.8 * 10308) 15

Завдання

Create a 1D array named float_array:

  • Use the correct function to create a NumPy 1D array;
  • Create an array with two elements (use a list as the first argument): 1.32, 4.6 in this order;
  • Set the data type of its elements to np.float16 via specifying the second argument.

Все було зрозуміло?

Секція 1. Розділ 2
toggle bottom row
course content

Зміст курсу

Ultimate NumPy

Basic Array CreationBasic Array Creation

A NumPy array is an efficient, multidimensional container for storing and manipulating large datasets of homogeneous (same) data types. Although they are similar to Python lists, they are more memory-efficient and allow for high-performance mathematical and numerical operations.

Now, it’s time to create your first NumPy arrays. The most straightforward way to do this is by using the array() function, passing either a list or a tuple as its argument.

Note

You should create NumPy arrays only from lists in all the tasks throughout our course.

Let’s take a look at an example:

As you can see, these two NumPy arrays are equal.

Specifying Data Type

The data type of the array elements is defined implicitly; however, you can specify it explicitly with the dtype parameter:

The first integer array uses the default int64 data type, which is an 8-byte integer. The second array uses int8, a 1-byte integer.

Other common NumPy data types include numpy.float16, numpy.float32, and numpy.float64, which store 2-byte, 4-byte, and 8-byte floating point numbers, respectively.

Using int8 for small integers is more memory-efficient. However, be careful when choosing the data type (dtype) to avoid overflow if the numbers are too large for the specified type.

Here is a list of possible data types in NumPy:

dtype range significant digits
np.int8 -128...127
np.int16 -32768...32768
np.int32 -2.1 * 109...2.1 * 109
np.int64 -9.2 * 1018...9.2 * 1018
np.float16 ±(6.0 * 10-8...65504) 3
np.float32 ±(1.4 * 10-45...3.4 * 1038) 6
np.float64 ±(4.9 * 10-324...1.8 * 10308) 15

Завдання

Create a 1D array named float_array:

  • Use the correct function to create a NumPy 1D array;
  • Create an array with two elements (use a list as the first argument): 1.32, 4.6 in this order;
  • Set the data type of its elements to np.float16 via specifying the second argument.

Все було зрозуміло?

Секція 1. Розділ 2
toggle bottom row
some-alt