Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Let a = np.arange(10, 100, 10). By hand, write what these give, then run them to check: a[2], a[-2], a[2:5], a[::3], a[::-2] and a[a > 50].
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. First write out the array: 10, 20, 30, 40, 50, 60, 70, 80, 90 (the stop of 100 is not included). The places go from 0 to 8.
Check. a[2] is the third number, 30. a[-2] is the second from the end, 80. a[2:5] is the places 2, 3 and 4: 30, 40, 50. a[::3] takes the places 0, 3 and 6: 10, 40, 70. a[::-2] goes backwards in steps of 2, from 90: 90, 70, 50, 30, 10. The mask picks the numbers above 50: 60, 70, 80, 90.
Answer: 30, 80, [30 40 50], [10 40 70], [90 70 50 30 10], [60 70 80 90].
Log in to join the discussion.
Loading…