# NumPy Basic: Exercises, Practice, Solution

## NumPy Basic [59 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1.Write a NumPy program to get the numpy version and show numpy build configuration. Go to the editor
Click me to see the sample solution

2. Write a NumPy program to  get help on the add function. Go to the editor
Click me to see the sample solution

3. Write a NumPy program to test whether none of the elements of a given array is zero. Go to the editor
Click me to see the sample solution

4. Write a NumPy program to test whether any of the elements of a given array is non-zero. Go to the editor
Click me to see the sample solution

5. Write a NumPy program to test a given array element-wise for finiteness (not infinity or not a Number). Go to the editor
Click me to see the sample solution

6. Write a NumPy program to test element-wise for positive or negative infinity. Go to the editor
Click me to see the sample solution

7. Write a NumPy program to test element-wise for NaN of a given array. Go to the editor
Click me to see the sample solution

8. Write a NumPy program to test element-wise for complex number, real number of a given array. Also test whether a given number is a scalar type or not. Go to the editor
Click me to see the sample solution

9. Write a NumPy program to test whether two arrays are element-wise equal within a tolerance. Go to the editor
Click me to see the sample solution

10. Write a NumPy program to create an element-wise comparison (greater, greater_equal, less and less_equal) of two given arrays. Go to the editor
Click me to see the sample solution

11. Write a NumPy program to create an element-wise comparison (equal, equal within a tolerance) of two given arrays. Go to the editor
Click me to see the sample solution

12. Write a NumPy program to create an array with the values 1, 7, 13, 105 and determine the size of the memory occupied by the array. Go to the editor
Click me to see the sample solution

13. Write a NumPy program to create an array of 10 zeros,10 ones, 10 fives. Go to the editor
Click me to see the sample solution

14. Write a NumPy program to create an array of the integers from 30 to70. Go to the editor
Click me to see the sample solution

15. Write a NumPy program to create an array of all the even integers from 30 to 70. Go to the editor
Click me to see the sample solution

16. Write a NumPy program to create a 3x3 identity matrix. Go to the editor
Click me to see the sample solution

17. Write a NumPy program to generate a random number between 0 and 1. Go to the editor
Click me to see the sample solution

18. Write a NumPy program to generate an array of 15 random numbers from a standard normal distribution. Go to the editor
Click me to see the sample solution

19. Write a NumPy program to create a vector with values ranging from 15 to 55 and print all values except the first and last. Go to the editor
Click me to see the sample solution

20. Write a NumPy program to create a 3X4 array using and iterate over it. Go to the editor
Click me to see the sample solution

21. Write a NumPy program to create a vector of length 10 with values evenly distributed between 5 and 50. Go to the editor
Click me to see the sample solution

22. Write a NumPy program to create a vector with values from 0 to 20 and change the sign of the numbers in the range from 9 to 15. Go to the editor
Click me to see the sample solution

23. Write a NumPy program to create a vector of length 5 filled with arbitrary integers from 0 to 10. Go to the editor
Click me to see the sample solution

24. Write a NumPy program to multiply the values of two given vectors. Go to the editor
Click me to see the sample solution

25. Write a NumPy program to create a 3x4 matrix filled with values from 10 to 21. Go to the editor
Click me to see the sample solution

26. Write a NumPy program to find the number of rows and columns of a given matrix. Go to the editor
Click me to see the sample solution

27. Write a NumPy program to create a 3x3 identity matrix, i.e. diagonal elements are 1, the rest are 0. Go to the editor
Click me to see the sample solution

28. Write a NumPy program to create a 10x10 matrix, in which the elements on the borders will be equal to 1, and inside 0. Go to the editor
Click me to see the sample solution

29. Write a NumPy program to create a 5x5 zero matrix with elements on the main diagonal equal to 1, 2, 3, 4, 5. Go to the editor
Click me to see the sample solution

30. Write a NumPy program to create a 4x4 matrix in which 0 and 1 are staggered, with zeros on the main diagonal. Go to the editor
Click me to see the sample solution

31. Write a NumPy program to create a 3x3x3 array filled with arbitrary values. Go to the editor
Click me to see the sample solution

32. Write a NumPy program to compute sum of all elements, sum of each column and sum of each row of a given array. Go to the editor
Click me to see the sample solution

33. Write a NumPy program to compute the inner product of two given vectors. Go to the editor
Click me to see the sample solution

34. Write a NumPy program to add a vector to each row of a given matrix. Go to the editor
Click me to see the sample solution

35. Write a NumPy program to save a given array to a binary file . Go to the editor
Click me to see the sample solution

36. Write a NumPy program to save two given arrays into a single file in compressed format (.npz format) and load it. Go to the editor
Click me to see the sample solution

37. Write a NumPy program to save a given array to a text file and load it. Go to the editor
Click me to see the sample solution

38. Write a NumPy program to convert a given array into bytes, and load it as array. Go to the editor
Click me to see the sample solution

39. Write a NumPy program to convert a given list into an array, then again convert it into a list. Check initial list and final list are equal or not. Go to the editor
Click me to see the sample solution

40. Write a NumPy program to compute the x and y coordinates for points on a sine curve and plot the points using matplotlib. Go to the editor
Click me to see the sample solution

41. Write a NumPy program to convert numpy dtypes to native python types. Go to the editor
Click me to see the sample solution

42. Write a NumPy program to add elements in a matrix. If an element in the matrix is 0, we will not add the element below this element. Go to the editor
Click me to see the sample solution

43. Write a NumPy program to find the missing data in a given array. Go to the editor
Click me to see the sample solution

44. Write a NumPy program to check whether two arrays are equal (element wise) or not. Go to the editor
Click me to see the sample solution

45. Write a NumPy program to create one-dimensional array of single, two and three digit numbers. Go to the editor
Click me to see the sample solution

46. Write a NumPy program to create a two-dimensional array of specified format. Go to the editor
Click me to see the sample solution

47. Write a NumPy program to create a one dimensional array of forty pseudo-randomly generated values. Select random numbers from a uniform distribution between 0 and 1. Go to the editor
Click me to see the sample solution

48. Write a NumPy program to create a two-dimensional array with shape (8,5) of random numbers. Select random numbers from a normal distribution (200,7). Go to the editor
Click me to see the sample solution

49. Write a NumPy program to generate a uniform, non-uniform random sample from a given 1-D array with and without replacement. Go to the editor
Click me to see the sample solution

50. Write a NumPy program to create a 4x4 array with random values, now create a new array from the said array swapping first and last rows. Go to the editor
Click me to see the sample solution

51. Write a NumPy program to create a new array of given shape (5,6) and type, filled with zeros. Go to the editor
Click me to see the sample solution

52. Write a NumPy program to sort a given array by row and column in ascending order. Go to the editor
Click me to see the sample solution

53. Write a NumPy program to extract all numbers from a given array which are less and greater than a specified number. Go to the editor
Click me to see the sample solution

54. Write a NumPy program to replace all numbers in a given array which is equal, less and greater to a given number. Go to the editor
Click me to see the sample solution

55. Write a NumPy program to create an array of equal shape and data type of a given array. Go to the editor
Click me to see the sample solution

56. Write a NumPy program to create a three-dimension array with shape (3,5,4) and set to a variable. Go to the editor
Click me to see the sample solution

57. Write a NumPy program to create a 4x4 array, now create a new array from the said array swapping first and last, second and third columns. Go to the editor
Click me to see the sample solution

58. Write a NumPy program to swap rows and columns of a given array in reverse order. Go to the editor
Click me to see the sample solution

59. Write a NumPy program to multiply two given arrays of same size element-by-element. Go to the editor
Click me to see the sample solution

Python-Numpy Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Find the index of an item in a list?

Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python?

```>>> ["foo", "bar", "baz"].index("bar")
1
```

Caveats follow

Note that while this is perhaps the cleanest way to answer the question as asked, index is a rather weak component of the list API, and I can't remember the last time I used it in anger. It's been pointed out to me in the comments that because this answer is heavily referenced, it should be made more complete. Some caveats about list.index follow. It is probably worth initially taking a look at the documentation for it:

```list.index(x[, start[, end]])
```

Linear time-complexity in list length

An index call checks every element of the list in order, until it finds a match. If your list is long, and you don't know roughly where in the list it occurs, this search could become a bottleneck. In that case, you should consider a different data structure. Note that if you know roughly where to find the match, you can give index a hint. For instance, in this snippet, l.index(999_999, 999_990, 1_000_000) is roughly five orders of magnitude faster than straight l.index(999_999), because the former only has to search 10 entries, while the latter searches a million:

```>>> import timeit
>>> timeit.timeit('l.index(999_999)', setup='l = list(range(0, 1_000_000))', number=1000)
9.356267921015387
>>> timeit.timeit('l.index(999_999, 999_990, 1_000_000)', setup='l = list(range(0, 1_000_000))', number=1000)
0.0004404920036904514
```

Only returns the index of the first match to its argument

A call to index searches through the list in order until it finds a match, and stops there. If you expect to need indices of more matches, you should use a list comprehension, or generator expression.

```>>> [1, 1].index(1)
0
>>> [i for i, e in enumerate([1, 2, 1]) if e == 1]
[0, 2]
>>> g = (i for i, e in enumerate([1, 2, 1]) if e == 1)
>>> next(g)
0
>>> next(g)
2
```

Most places where I once would have used index, I now use a list comprehension or generator expression because they're more generalizable. So if you're considering reaching for index, take a look at these excellent Python features.

Throws if element not present in list

A call to index results in a ValueError if the item's not present.

```>>> [1, 1].index(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 2 is not in list
```

If the item might not be present in the list, you should either

• Check for it first with item in my_list (clean, readable approach), or
• Wrap the index call in a try/except block which catches ValueError (probably faster, at least when the list to search is long, and the item is usually present.)

Ref: https://bit.ly/2ALwXwe