All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Matrix Addition In Python User Input

Last Updated : Mar 11, 2024

Matrix Addition In Python User Input

In this tutorial we will show you the solution of matrix addition in python user input, Matrix addition is a very commonly used in python programming.

Matrix gets added to the same element with the other one within a sequence manner.

In this tutorial, we’re going to understand the concept of addition of two matrix in python programming along with taking input from the user. We’re going to understand this concept step by step here.

Step By Step Guide On Matrix Addition In Python User Input :-

Addition of matrix by taking input from the user

row = int(input("Kindly enter the number of rows you want " ))
column = int(input("Kindly enter the number of rows you want "))
print("Now enter the elements of First Matrix:")
matrix_x= [[int(input()) for i in range(column)] for i in range(rows)]
print("First Matrix is: ")
for n in matrix_x:
    print(n)
print("Enter the elements of Second Matrix:")
matrix_b= [[int(input()) for i in range(column)] for i in range(rows)]
for n in matrix_y:
    print(n)
   result=[[0 for i in range(column)] for i in range(rows)]
for i in range(rows):
    for j in range(column):
        result[i][j] = matrix_x[i][j]+matrix_y[i][j]
print("The Sum of Above two Matrices is : ")
for r in result:
    print(r)
  1. In this code, we declared row variable and within that, we took input from the user of the number of rows user want in the matrix.
  2. Similarly we declared another variable column in which we took input from the user that how much number of columns he wants to be appears in the matrix.
  3. After taking input of the number of rows and columns from the user of the first matrix, we took the elements of the matrix through the user by using input range function and named it as matrix_x.
  4. After getting the elements from the user, we showed the matrix to the user by using print(n) function in order to make the program comprehensive.
  5. Here, i is the number of columns and j is the number of rows which stored in array with range function.
  6. Similarly, we again took the input of second matrix by using input range function and stored it in array variable of i and j as referring to the matrix_y.
  7. Now, the result will be the addition of both the matrix, for that we added both the array of i and j and stored it in result - result[i][j] = matrix_x[i][j]+matrix_y[i][j]
  8. that’s it, we got our result, and now we printed it by using print_r function.

Points to Remember -

  1. Matrix input should be taken in proper sequence
  2. Order of matrix should be relevant
  3. Result should be appear in matrix form only

Conclusion :-

In this tutorial, we’ve understood the code of addition of two matrix in python.

You can easily complete this task by following the above mentioned steps and can construct a program in python as per your need.

This tutorial will help you to seek information about such code and deep understanding of their working and condition. I hope this tutorial on matrix addition in python user input helps you.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪