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

Conteúdo do Curso

Ultimate NumPy

Ultimate NumPy

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

Basic 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:

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

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:

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.

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:

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 1. Capítulo 2
toggle bottom row

Basic 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:

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

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:

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.

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:

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 1. Capítulo 2
toggle bottom row

Basic 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:

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

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:

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.

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:

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

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:

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

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:

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.

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:

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 2
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt