The dtypes of scalars and arrays

Question Q6.1.11

The dtype np.uint8 represents an unsigned integer in 8 bits. Its value may therefore be in the range $0 - 255$. Explain the following behaviour:

In [x]: x = np.uint8(250)
In [x]: x*2
Out[x]: 500

In [x]: x = np.array([250,], dtype=np.uint8)
In [x]: x*2
Out[x]: array([244], dtype=uint8)

Solution

To access solutions, please obtain an access code from Cambridge University Press at the Lecturer Resources page for my book (registration required) and then sign up to scipython.com providing this code.