Python program to get matrix as input from user and print it in different type

Here, we are going to learn how to get input from the user on a matrix element and then use different methods to print the values of the matrix?
Submitted by Shivang Yadav, on February 22, 2021

Matrix in python is a two-dimensional data structure which is an array of arrays.

In python, you can easily create a matrix and access elements of the matrix. Here is a program to illustrate creating a matrix and different methods to print it.

Program to get matrix as input from user and print it in different type

t=()

rows=int(input("Enter number of Rows of the matrix : "))
cols=int(input("Enter number of columns of the matrix : "))

for i in range(rows):
    c=()
    for j in range(cols):
        v=int(input("Enter Value : "))
        c+=(v,)
    t+=(c,)

print("Method 1")
for i in range(len(t)):
    for j in  range(len(t[i])):
        print(t[i][j],end=' ')
    print()

print("Method 2")
for R in t:
    for C in R:
        print(C,end = ' ')
    print()

print("Method 3 ", end = ' ')
print(t)

Output:

Enter number of Rows of the matrix : 2
Enter number of columns of the matrix : 2
Enter Value : 54
Enter Value : 1
Enter Value : 76
Enter Value : 34
Method 1
54 1 
76 34 
Method 2
54 1 
76 34 
Method 3  ((54, 1), (76, 34))

Python Array Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.