Skip to main content
added 720 characters in body
Source Link
Dave Rosenman
  • 1.5k
  • 11
  • 14

Here's one option:

n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]
for i in range(n):
    for j in range(n):
        a[i][j] = int(input())
        
for row in a:
    print(' '.join([str(elem) for elem in row]))

example: If IIf you use pandas, and make a dataframe... if you enter 4 for the number of rows, then enterand the numbernumbers 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, I will get the output:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Or, if you use pandas, and make a dataframe, your output would look nicer... the script below

If you again type in 4 for number of rows, and enter the numbers 1,2,3,4...16..will produce the following output. you'll get something that looks like this

Another option... using numpy... and the same values from the first option...

Using the same examples as before, your output would look like this:...would produce

Here's one option:

n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]
for i in range(n):
    for j in range(n):
        a[i][j] = int(input())
        
for row in a:
    print(' '.join([str(elem) for elem in row]))

example: If I enter 4 for number of rows, then enter the number 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, I will get the output:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Or, if you use pandas, and make a dataframe, your output would look nicer.

If you again type in 4 for number of rows, and enter the numbers 1,2,3,4...16... you'll get something that looks like this

Another option... using numpy.

Using the same examples as before, your output would look like this:

If you use pandas, and make a dataframe... if you enter 4 for the number of rows, and the numbers 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16... the script below

...will produce the following output.

Another option... using numpy... and the same values from the first option...

...would produce

added 720 characters in body
Source Link
Dave Rosenman
  • 1.5k
  • 11
  • 14
       col 1  col 2  col 3  col 4
row 1      1      2      3      4
row 2      5      6      7      8
row 3      9     10     11     12
row 4     13     14     15     16

enter image description here Another option... using numpy.

import numpy as np
n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]


    
for i in range(n):
   for j in range(n):
        a[i][j] = int(input())

print(np.matrix(a))

Using the same examples as before, your output would look like this:

[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]

enter image description here

       col 1  col 2  col 3  col 4
row 1      1      2      3      4
row 2      5      6      7      8
row 3      9     10     11     12
row 4     13     14     15     16

Another option... using numpy.

import numpy as np
n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]


    
for i in range(n):
   for j in range(n):
        a[i][j] = int(input())

print(np.matrix(a))

Using the same examples as before, your output would look like this:

[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
added 720 characters in body
Source Link
Dave Rosenman
  • 1.5k
  • 11
  • 14

Here's one option:

n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]
for i in range(n):
    for j in range(n):
        a[i][j] = int(input())
        
for row in a:
    print(' '.join([str(elem) for elem in row]))

example: If I enter 4 for number of rows, then enter the number 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, I will get the output:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Or, if you use pandas, and make a dataframe, your output would look nicer.

import pandas as pd
n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]
col_names = []
row_names = []

    
for i in range(n):
    col_names.append('col ' + str(i+1))
    row_names.append('row ' + str(i+1))  
    for j in range(n):
          a[i][j] = int(input())

print(pd.DataFrame(a,columns = col_names, index = row_names))

If you again type in 4 for number of rows, and enter the numbers 1,2,3,4...16... you'll get something that looks like this

enter image description here

Here's one option:

n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]
for i in range(n):
    for j in range(n):
        a[i][j] = int(input())
        
for row in a:
    print(' '.join([str(elem) for elem in row]))

example: If I enter 4 for number of rows, then enter the number 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, I will get the output:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Here's one option:

n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]
for i in range(n):
    for j in range(n):
        a[i][j] = int(input())
        
for row in a:
    print(' '.join([str(elem) for elem in row]))

example: If I enter 4 for number of rows, then enter the number 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, I will get the output:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Or, if you use pandas, and make a dataframe, your output would look nicer.

import pandas as pd
n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]
col_names = []
row_names = []

    
for i in range(n):
    col_names.append('col ' + str(i+1))
    row_names.append('row ' + str(i+1))  
    for j in range(n):
          a[i][j] = int(input())

print(pd.DataFrame(a,columns = col_names, index = row_names))

If you again type in 4 for number of rows, and enter the numbers 1,2,3,4...16... you'll get something that looks like this

enter image description here

Source Link
Dave Rosenman
  • 1.5k
  • 11
  • 14
Loading