Is there a better way to take a matrix as input?
Well, if by "better" you mean fewer lines of code, then I have the solution for you!
(lambda N: [[int(x) for x in input().split()[:N]] for _ in range(N)])(int(input("Enter N: ")))
It's pretty hard to understand what all is going on in that one line, so let's dissect it piece-by-piece.
First, we'll note the general structure is
(lambda expression)(argument), so that means we are defining an anonymous function and immediately calling it. The argument is the easier part, so let's start there.
What is the argument?
The argument is simply the size N of the matrix, where we convert the user input (prompted by
"Enter N: ") to an integer.
What is the
If you're not already familiar with Python's lambda expressions, these are how you can create anonymous functions that take in certain parameters and return a value. In this case, we are taking in the parameter
N and returning a 2D list (our matrix) formed via a "list comprehension."
If you don't know list comprehensions, what they do is construct lists of the form
[a for b in c]. That is equivalent to the following:
L = 
for b in c:
Another note to make is that when
_ is used as the loop variable, that just means we don't intend to use it later on. In this case, the row number is unimportant.
Finally, when we say
[int(x) for x in input().split()[:N]], this means we split the input row into integers and take only the first N elements, which (for better or worse) means that we can ignore any extra input on each line beyond the N integers we seek.
All things considered, this is probably not the way to do things. For evidence, consult The Zen of Python. This particular code is fairly ugly, complicated, nested, dense, and unreadable, even though it is short. However, I thought it was worth mentioning because it is a good demonstration of how short a useful Python script can be (and of lambda expressions and list comprehensions, for those who have not yet been introduced).