I'm using numpy.eye(N, M, dtype=int) to print a matrix whose diagonal elements are 1 and rest of the elements are 0. Code:

import numpy
import sys

line = sys.stdin.readline()

N, M = map(int, line.split())

print numpy.eye(N, M, dtype=int)

The above runs successfully in Python 2. But in python 3 I get the following error:

File "solution.py", line 7
    print numpy.eye( N , M , dtype =int )
SyntaxError: invalid syntax

Is there an alternate package other than numpy in Python 3 through which I can accomplish this? Has the eye function become obsolete with Python 3?

  • $\begingroup$ Welcome to DataScience.SE! If your question has been resolved, please tick the answer. $\endgroup$ – Emre May 17 '16 at 21:00

No, the print function has changed; it needs to be treated as a proper function, with parentheses. Try:

print(numpy.eye(N, M, dtype=int))
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.