In the previous article, we have discussed Python Program for Frequencies of Even and Odd Numbers in a Matrix

Given a square matrix, the task is to swap the upper diagonal elements and lower diagonal elements of a given matrix.

**What is a matrix:**

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

**Diagonal Matrix:**

The entries outside the main diagonal of a diagonal matrix are all 0; the word usually refers to square matrices.

**Example:**

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

- Python Program to Swap Major and Minor Diagonals of a Square Matrix
- Python Program for Multiplication of Two Matrices | How do you do Matrix Multiplication in Python?
- Python Program to Calculate Sum of all Maximum Occurring Elements in Matrix

**Examples:**

**Example1:**

**Input:**

Given Matrix : 10 20 30 45 60 70 11 12 13

**Output:**

The Matrix after swapping the upper diagonal and lower diagonal elements is: 10 45 11 20 60 12 30 70 13

**Example2:**

**Input:**

Given Matrix : 3 5 1 4 2 0 2 6 4

**Output:**

The Matrix after swapping the upper diagonal and lower diagonal elements is: 3 4 2 5 2 6 1 0 4

## Program to Swap Upper Diagonal Elements with Lower Diagonal Elements of Matrix in Python

Below are the ways to swap the upper diagonal elements and lower diagonal elements of a given matrix in python:

### Method #1: Using For Loop (Static Input)

**Approach:**

- Give the matrix as static input and store it in a variable.
- Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
- Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
- Loop till the given number of rows using the For loop.
- Inside the For loop, iterate from the iterator value +1 of the parent for loop to the given number of rows using another Nested For loop(Inner For loop).
- Swap mtrx[itor][k], mtrx[k][itor] using the comma(,) operator ( where itor is the iterator value of the parent for loop and k is the iterator value of the inner for loop).
- Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
- Print the element of the matrix by printing gvnmatrix[n][m] value where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
- The Exit of the Program.

**Below is the implementation:**

# Give the matrix as static input and store it in a variable. mtrx = [[10, 20, 30], [45, 60, 70], [11, 12, 13]] # Calculate the number of rows of the given matrix by # calculating the length of the nested list using the len() function # and store it in a variable mtrxrows. mtrxrows = len(mtrx) # Calculate the number of columns of the given matrix by # calculating the length of the first list in the nested list # using the len() function and store it in a variable mtrxcols. mtrxcols = len(mtrx[0]) # Loop till the given number of rows using the For loop. for itor in range(0, mtrxrows): # Inside the For loop, iterate from the iterator value +1 of the parent for loop to the # given number of rows using another Nested For loop(Inner For loop). for k in range(itor + 1, mtrxrows): # Swap mtrx[itor][k], mtrx[k][itor] using the comma(,) operator ( where itor is the # iterator value of the parent for loop and k is the iterator value # of the inner for loop). mtrx[itor][k], mtrx[k][itor] = mtrx[k][itor], mtrx[itor][k] print("The Matrix after swapping the upper diagonal and lower diagonal elements is:") # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of rows using another # Nested For loop(Inner For loop). for m in range(mtrxcols): # Print the element of the matrix by printing gvnmatrix[n][m] value # where n is the iterator value of the parent For loop and m is the iterator # value of the inner For loop. print(mtrx[n][m], end=' ') print()

**Output:**

The Matrix after swapping the upper diagonal and lower diagonal elements is: 10 45 11 20 60 12 30 70 13

### Method #2: Using For loop (User Input)

**Approach:**

- Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
- Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
- Take a list and initialize it with an empty value using [] or list() to say
**gvnmatrix**. - Loop till the given number of rows using the For loop
- Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
- Add the above row elements list to
**gvnmatrix**using the append() function. - Loop till the given number of rows using the For loop.
- Inside the For loop, iterate from the iterator value +1 of the parent for loop to the given number of rows using another Nested For loop(Inner For loop).
- Swap mtrx[itor][k], mtrx[k][itor] using the comma(,) operator ( where itor is the iterator value of the parent for loop and k is the iterator value of the inner for loop).
- Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
- Print the element of the matrix by printing gvnmatrix[n][m] value where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
- The Exit of the Program.

**Below is the implementation:**

# Give the number of rows of the matrix as user input using the int(input()) function # and store it in a variable. mtrxrows = int(input('Enter some random number of rows of the matrix = ')) # Give the number of columns of the matrix as user input using the int(input()) function # and store it in another variable. mtrxcols = int(input('Enter some random number of columns of the matrix = ')) # Take a list and initialize it with an empty value using [] or list() to say gvnmatrix. mtrx = [] # Loop till the given number of rows using the For loop for n in range(mtrxrows): # Inside the For loop, Give all the row elements of the given Matrix as a list using # the list(),map(),int(),split() functions and store it in a variable. l = list(map(int, input( 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split())) # Add the above row elements list to gvnmatrix using the append() function. mtrx.append(l) # Loop till the given number of rows using the For loop. for itor in range(0, mtrxrows): # Inside the For loop, iterate from the iterator value +1 of the parent for loop to the # given number of rows using another Nested For loop(Inner For loop). for k in range(itor + 1, mtrxrows): # Swap mtrx[itor][k], mtrx[k][itor] using the comma(,) operator ( where itor is the # iterator value of the parent for loop and k is the iterator value # of the inner for loop). mtrx[itor][k], mtrx[k][itor] = mtrx[k][itor], mtrx[itor][k] print("The Matrix after swapping the upper diagonal and lower diagonal elements is:") # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of rows using another # Nested For loop(Inner For loop). for m in range(mtrxcols): # Print the element of the matrix by printing gvnmatrix[n][m] value # where n is the iterator value of the parent For loop and m is the iterator # value of the inner For loop. print(mtrx[n][m], end=' ') print()

**Output:**

Enter some random number of rows of the matrix = 3 Enter some random number of columns of the matrix = 3 Enter {3} elements of row {1} separated by spaces = 3 5 1 Enter {3} elements of row {2} separated by spaces = 4 2 0 Enter {3} elements of row {3} separated by spaces = 2 6 4 The Matrix after swapping the upper diagonal and lower diagonal elements is: 3 4 2 5 2 6 1 0 4

Find the best practical and ready-to-use Python Programming Examples that you can simply run on a variety of platforms and never stop learning.