Two Dimensional Array in Python

Two dimensional array in Python programming is represented in the form of rows and columns, also known as a matrix. It is also known as array of arrays or list of arrays.

Program Printing Elements of Two Dimensional Array

Program

arr = [[2, 3, 4], #'arr' is name of Two Dimensional Array and
       [5, 6, 7], #assigning Elements to it.
       [8, 9, 10],
       [11, 12, 13]]
for row in range(4): #loop looping from 0 to 3.
    for col in range(3): #loop looping from 0 to 2.
        print(arr[row][col] ,end="  ") #and printing.
    print() #used for new line.
Output
2  3  4
5  6  7
8  9  10
11  12  13

Program Printing Index of Two Dimensional Array

Program

arr = [[2, 3, 4], #'arr' is name of Two Dimensional Array and
       [5, 6, 7], #assigning Elements to it.
       [8, 9, 10],
       [11, 12, 13]]
print(arr[3][2]) #printing Index Element, it printing 13.
Output
13

Program User Entered Values in Two Dimensional Array Printing

Program

arr = [[0 for row in range(0, 3)] for col in range(0, 4)] #setting array.
for row in range(4): #loop looping from 0 to 3.
    for col in range(3): #loop looping from 0 to 2.
        arr[row][col] = int(input("Enter Element: "))
for ro in range(4): #loop looping from 0 to 3.
    for co in range(3): #loop looping from 0 to 2.
        print(arr[ro][co], end="  ") #and printing.
    print() #used for new Line.
Output
Enter Element: 5
Enter Element: 2
Enter Element: 8
Enter Element: 2
Enter Element: 2
Enter Element: 5
Enter Element: 4
Enter Element: 7
Enter Element: 8
Enter Element: 0
Enter Element: 9
Enter Element: 3
5  2  8
2  2  5
4  7  8
0  9  3