Exercises

This notebook aims to provide a list of exercises useful to start practising your programming skills


How to start

Opening a new notebook, you should start importing the modules that are needed for the notebook to work. For instance: import numpy, import matplotlib, import datacube, etc.

In the following cells there will be some exercise with an hidden solution so that you can try to solve the problem yourself. Click on the green button 'Show Solution' to compare your solution with the one provided.

Be aware: in programming there are multiple right solutions, at this stage, it is only important that you find the same results.

Working with numbers

1) Find the volume of a sphere knowing that it has a radius of 4 m and print the result with no decimals


2) Given a list of integers a = [1,32,63,14,5,26,79,8,59,10]:

i) Find the maximum and minimum values
ii) The index of the max and min values
iii) Sort the elements of the list in asceding order
iv) Sort the elements of the list in desceding order
v) Convert the original list in a numpy array and re-do i) to iv) using the numpy functions

3) Create a list of numbers that goes from 0 to 10000 in ascending order (tip: use loops)


4) Create an array that goes from 0 to 10000 in ascending order using numpy (tip: do not use loops)


5) Given the array found in ex. 4:

i) Add 2 to every other number. 
   For example: a = [0,1,2,3,4,5,6,...], then adding 2 to every other number: a = [2,1,4,3,6, ...]
ii) add 3 to the first 100 elements of the list

6) Print the following pattern (tip: use loops):

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

7) Given the following array, a = np.random.rand(100)*2-1 :

    i) Find how many negative values are in a
    ii) Convert the negative values in NaN (np.nan)
    iii) Find how many NaN are in a
    iv) Convert NaN in 0
    v) Convert a to an int16 array, being careful that each element is approximated 
       to the closest integer number, for example: (0.6 -> 1) and (0.4 -> 0)

More exercise in Numpy arrays:

https://www.w3resource.com/python-exercises/numpy/basic/index.php

https://www.w3resource.com/python-exercises/numpy/index-array.php