50
loading...
This website collects cookies to deliver better user experience
# Import the numpy library
import numpy as np
# Declaring and Initializing the one Dimension Array
numbers = np.array([10,20,30,40,50])
# Indexing and accessing array as 2D causes IndexError
print(numbers[0,2])
Traceback (most recent call last):
File "c:\Projects\Tryouts\Python Tutorial.py", line 8, in <module>
print(numbers[0,2])
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
# Import the numpy library
import numpy as np
# Declaring and Initializing the one Dimension Array
numbers = np.array([10,20,30,40,50])
# Indexing and accessing array correctly
print("The array element is ",numbers[2])
The array element is 30
len()
method, as shown below.len()
function and return the array’s dimension.# Import the numpy library
import numpy as np
# Declaring and Initializing the one Dimension Array
numbers1d = np.array([10,20,30,40,50])
# Declaring and Initializing the one Dimension Array
numbers2d = np.array([[[10,1],[20,2],[30,3],[40.4],[50.5]]])
print("The array dimension is ", len(numbers1d.shape))
print("The array dimension is ", len(numbers2d.shape))
The array dimension is 1
The array dimension is 2