How to Define a 2 Dimensional Array in Python?
To define a 2-dimensional array in Python, you can use a list of lists where each inner list represents a row in the array. Here is an example:
# Define a 2D array with 3 rows and 4 columns
my_array = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
You can access individual elements in the array by specifying the row and column indices:
# Access element in row 1, column 2 (value 3)
elem = my_array[0][2]
You can also modify elements in the array:
# Change element in row 2, column 3 from 8 to 99
my_array[1][2] = 99