Creation of 2D array and print the diagonal elements of the matrix in Python
2D array has rows and columns. The data is stored in row wise. Let us create a 2D array as follows. Python code to create a 2D array of elements: #import the built-in file numpy and develop an object np1 import numpy as np1 # Create a 2D array of elements with 2 rows and 2 columns arr_2d = np1.arange(1, 5).reshape(2, 2) #print the elements print("2D Array elements are:\n", arr_2d) here, is the output. 2D Array elements are: [[1 2] [3 4]] If you want to change the rows and columns to 3. Use the below code… arr_2d = np1.arange(1, 10).reshape(3, 3) It gives you the output as follows. 2D Array elements are: [[1 2 3] [4 5 6] [7 8 9]] If you want to print 4 rows and columns. Let this code helps… arr_2d = np1.arange(1, 17).reshape(4, 4) The output is 2D Array elements are: [[ 1 2 3 4] [ 5 6 7 8] ...