PRINT A VECTOR: NUMPY BASIC EXERCISE 19


Print a vector in numpy ranging from 10 to 50 skipping the first and the last values and print the rest of them.

In this exercise, we are going to print vector values ranging from 10 to 50 skipping the first and last values.

Note: arange([start,] stop[, step,], dtype=None) Return evenly spaced values within a given interval. Values are generated within the half-open interval “[start, stop)“ (in other words, the interval including `start` but excluding `stop`). For integer arguments the function is equivalent to the Python built-in `range` function, but returns an ndarray rather than a list.

Code

import numpy as np
arr = np.arange(10,50)
print("Initial Array")
print(arr)
print("Printing all values except first and last values")
print(arr[1:-1])

Output

Initial Array
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]
Printing all values except first and last values
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 35 36 37 38 39 40 41 42 43 44 45 46 47 48]

Code Editor

import numpy as np arr = np.arange(10,50) print("Initial Array") print(arr) print("Printing all values except first and last values") print(arr[1:-1])