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
course content

Contenido del Curso

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

book
Basic Array Creation

A NumPy array is an efficient, multidimensional container for storing and manipulating large datasets of the 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, and only them.

Note

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

1234567
import numpy as np # Creating an array from list array_from_list = np.array([1, 2, 3, 2, 6, 1]) # Creating an array from tuple array_from_tuple = np.array((1, 2, 3, 2, 6, 1)) print(f'Array from list: {array_from_list}') print(f'Array from tuple: {array_from_tuple}')
copy

Specifying Data Type

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

1234567
import numpy as np # Creating an integer array without specifying dtype array_1 = np.array([1, 2, 3]) # Creating an integer array with setting dtype to 1-byte integer array_2 = np.array([1, 2, 3], dtype=np.int8) print(f'First array dtype: {array_1.dtype}') print(f'Second array dtype: {array_2.dtype}')
copy

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.

The most 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.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt