I need help with an excercise!

Morning you all guys!

I need help with an excercise:

import numpy as np

movies_watched = np.array([2, 3, 8, 0, 2, 4, 3, 1, 1, 0, 5, 1, 1, 7, 2])

first_quarter = np.percentile(movies_watched, 25)
third_quarter = np.percentile(movies_watched, 75)

interquartile_range = third_quarter - first_quarter

movies_watched_sorted = movies_watched.sort()
print(movies_watched_sorted)
print(first_quarter)
print(third_quarter)
print(interquartile_range)

At the moment that I print the sorted list “movies_watched_sorted” it prints out “None” I do not know what happend.
Can anyone explain me what happend?

The function on the right has no return value. Use sorted(list) instead.

But, what do you mean with sorted(list)? I have been trying but, nothing works. :frowning:

movies_watched_sorted = movies_watched.sort()

should be written,

movies_watched_sorted = sorted(movies_watched)

That way the return value is a sorted list.

Thanks I finally got it!
Thanks very much for your help

1 Like

You will have noticed that in place methods return None since the work is done directly on the object. There is nothing to return. Any other methods of this kind you encounter in the future will have the same behavior. There usually are alternate methods that do not mutate the object, but rather copy it and mutate the copy, which is then returned to the assignment.