NumPy array

  • Post category:NumPy

In this NumPy tutorial, you’ll learn how to create a NumPy array in Python. You can use a np.array() function for this task.

NumPy 1d array

# import numpy module
import numpy as np

# create a numpy array
var = np.array([11, 22, 33, 44, 55])

# print the array
print(var)

# print the type
print(type(var))

# print the dimension
print(var.ndim)

Output:

[11 22 33 44 55]
<class 'numpy.ndarray'>
1

2d NumPy array

# import numpy module
import numpy as np

# create a numpy array
var = np.array([[11, 22, 33, 44, 55],
               [66, 77, 88, 99, 100]])

# print the array
print(var)

# print the type
print(type(var))

# print the dimension
print(var.ndim)

Output:

[[ 11  22  33  44  55]
 [ 66  77  88  99 100]]
<class 'numpy.ndarray'>
2

Free resources to learn advanced skills: AiHints and CodeAllow