Python Program to Create a Matrix

Introduction

A matrix is a two-dimensional array of elements arranged in rows and columns. In Python, matrices can be represented using lists of lists, where each inner list represents a row of the matrix. This tutorial will guide you through creating a Python program that creates a matrix and displays it.

Example:

  • Input: Number of rows = 2, Number of columns = 3
  • Output Matrix:
    1 2 3
    4 5 6
    

Problem Statement

Create a Python program that:

  • Takes the number of rows and columns as input.
  • Allows the user to input elements for the matrix.
  • Stores these elements in a list of lists (representing the matrix).
  • Displays the matrix in a formatted manner.

Solution Steps

  1. Take Matrix Dimensions: Ask the user for the number of rows and columns.
  2. Initialize the Matrix: Create an empty list to store the matrix.
  3. Input Matrix Elements: Use nested loops to take input for each element of the matrix.
  4. Display the Matrix: Use nested loops to display the matrix in a formatted way.

Python Program

# Python Program to Create a Matrix
# Author: https://www.rameshfadatare.com/

# Step 1: Take the number of rows and columns as input
rows = int(input("Enter the number of rows: "))
columns = int(input("Enter the number of columns: "))

# Step 2: Initialize the matrix
matrix = []

# Step 3: Input matrix elements
print("Enter the elements row-wise:")
for i in range(rows):
    row = []  # Initialize an empty row
    for j in range(columns):
        element = int(input(f"Element at position ({i+1}, {j+1}): "))
        row.append(element)
    matrix.append(row)

# Step 4: Display the matrix
print("\nThe matrix is:")
for row in matrix:
    for element in row:
        print(element, end=" ")
    print()  # Newline after each row

Explanation

Step 1: Take the Number of Rows and Columns as Input

  • The input() function is used to take the number of rows and columns from the user. These values are converted to integers and stored in rows and columns variables.

Step 2: Initialize the Matrix

  • An empty list matrix is initialized to store the matrix elements.

Step 3: Input Matrix Elements

  • A nested loop is used to take input for each element of the matrix. The outer loop iterates over the rows, and the inner loop iterates over the columns. Each element is appended to the current row, and the row is then appended to the matrix.

Step 4: Display the Matrix

  • A nested loop is used to print the matrix in a formatted manner. The elements of each row are printed on the same line, followed by a newline after each row.

Output Example

Example Output:

Enter the number of rows: 2
Enter the number of columns: 3
Enter the elements row-wise:
Element at position (1, 1): 1
Element at position (1, 2): 2
Element at position (1, 3): 3
Element at position (2, 1): 4
Element at position (2, 2): 5
Element at position (2, 3): 6

The matrix is:
1 2 3 
4 5 6 

Additional Examples

Example 1: Creating a 3×3 Matrix

# Example of a 3x3 matrix
rows = 3
columns = 3
matrix = []

print("Enter the elements row-wise:")
for i in range(rows):
    row = []
    for j in range(columns):
        element = int(input(f"Element at position ({i+1}, {j+1}): "))
        row.append(element)
    matrix.append(row)

print("\nThe 3x3 matrix is:")
for row in matrix:
    for element in row:
        print(element, end=" ")
    print()

Output:

Enter the elements row-wise:
Element at position (1, 1): 1
Element at position (1, 2): 2
Element at position (1, 3): 3
Element at position (2, 1): 4
Element at position (2, 2): 5
Element at position (2, 3): 6
Element at position (3, 1): 7
Element at position (3, 2): 8
Element at position (3, 3): 9

The 3x3 matrix is:
1 2 3 
4 5 6 
7 8 9 

Example 2: Creating a 2×2 Matrix with Negative Numbers

# Example of a 2x2 matrix with negative numbers
rows = 2
columns = 2
matrix = []

print("Enter the elements row-wise:")
for i in range(rows):
    row = []
    for j in range(columns):
        element = int(input(f"Element at position ({i+1}, {j+1}): "))
        row.append(element)
    matrix.append(row)

print("\nThe 2x2 matrix is:")
for row in matrix:
    for element in row:
        print(element, end=" ")
    print()

Output:

Enter the elements row-wise:
Element at position (1, 1): -1
Element at position (1, 2): -2
Element at position (2, 1): -3
Element at position (2, 2): -4

The 2x2 matrix is:
-1 -2 
-3 -4 

Conclusion

This Python program demonstrates how to create a matrix using lists of lists. The matrix is created by taking user input for the number of rows, columns, and elements. The program then displays the matrix in a formatted manner. Understanding how to create and work with matrices is essential for handling two-dimensional data in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top