This notebook aims to provide a list of exercises useful to start practising your programming skills
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.
# Always run this cell at beginning because it contains the needed modules for the exercises
import math #import all the mathematical functions
pi = math.pi # pi = 3.14
import numpy as np
print(pi)
3.141592653589793
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.
radius = 4. #metres
volume_sphere = 4./3. * pi * radius**3
print(round(volume_sphere),' m^3')
print(volume_sphere)
268 m^3 268.082573106329
# Write here your own solution
i) Find the maximum and minimum values
# i)
a = [1,32,63,14,5,26,79,8,59,10]
print(a,type(a))
a_max = max(a)
a_min = min(a)
print('min value: %i; max value: %i' %(a_min,a_max)) #different way of printing
print('min value:',a_min,'; max value:',a_max)
[1, 32, 63, 14, 5, 26, 79, 8, 59, 10] <class 'list'> min value: 1; max value: 79 min value: 1 ; max value: 79
# Write here your own solution
# i)
ii) The index of the max and min values
# ii)
for i,val in enumerate(a):
if val==a_max:
index_max = i
elif val==a_min:
index_min = i
print(index_max, index_min)
print(a[index_max],a[index_min])
print(a)
6 0 79 1 [1, 32, 63, 14, 5, 26, 79, 8, 59, 10]
# Write here your own solution
# ii)
iii) Sort the elements of the list in asceding order
# iii)
a.sort() # Built-in method, find on internet more methods for lists
print(a) # For list object the .sort() will overwrite the list, so from now on a will be sorted
[1, 5, 8, 10, 14, 26, 32, 59, 63, 79]
# Write here your own solution
# iii)
iv) Sort the elements of the list in desceding order
# iv)
a.sort()
a = a[::-1] # espression to change the order of a list or array
print(a)
[79, 63, 59, 32, 26, 14, 10, 8, 5, 1]
# Write here your own solution
# iv)
v) Convert the original list in a numpy array and re-do i) to iv) using the numpy functions
# v)
a = [1,32,63,14,5,26,79,8,59,10]
a = np.array(a)
print(a,type(a))
# Find max and min values
a_max = np.max(a)
a_min = np.min(a)
print(a_min,a_max)
# Find index of max and min values
index_max = np.argmax(a)
index_min = np.argmin(a)
print(index_min,index_max)
# Sort the array
np.sort(a)
print(a,' In this case the numpy method np.sort() does not overwrite a')
a = np.sort(a) # This will overwrite a in ascending order, if you want to know more run 'help(np.sort)' in cell
print(a)
a = a[::-1]
print(a)
[ 1 32 63 14 5 26 79 8 59 10] <class 'numpy.ndarray'> 1 79 0 6 [ 1 32 63 14 5 26 79 8 59 10] In this case the numpy method np.sort() does not overwrite a [ 1 5 8 10 14 26 32 59 63 79] [79 63 59 32 26 14 10 8 5 1]
# Write here your own solution
# v)
a = []
for i in range(1001):
a.append(i)
print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000]
# Write here your own solution
a = np.arange(0,10001)
print(a)
[ 0 1 2 ... 9998 9999 10000]
# Write here your own solution
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, ...]
# i)
a = np.arange(0,10001)
a[::2] = a[::2] + 2
print(a)
[ 2 1 4 ... 10000 9999 10002]
# Write here your own solution
# i)
ii) add 3 to the first 100 elements of the list
# ii)
a[:100] = a[:100] + 3
print(a)
[ 5 4 7 ... 10000 9999 10002]
# You can also print only the first 100 elements
print(a[:100])
[ 5 4 7 6 9 8 11 10 13 12 15 14 17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30 33 32 35 34 37 36 39 38 41 40 43 42 45 44 47 46 49 48 51 50 53 52 55 54 57 56 59 58 61 60 63 62 65 64 67 66 69 68 71 70 73 72 75 74 77 76 79 78 81 80 83 82 85 84 87 86 89 88 91 90 93 92 95 94 97 96 99 98 101 100 103 102]
# Write here your own solution
# ii)
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
for num in range(6):
for j in range(num):
print (num, end=" ") #print number
# new line after each row to display pattern correctly
print("\n")
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
# Write here your own solution
i) Find how many negative values are in a
# i)
np.random.seed(0)
a = np.random.rand(100)*2-1 # np.random.rand(100) gives a 100 elements array, where each element is a random number
# between 0 and 1. np.random.rand()*2-1 gives a random number between -1 and 1
neg_val = (a<0).sum() # or neg_val = np.sum(a<0)
print('In a there are %i negative values' % neg_val)
# If you want to get always the same answer use "np.random.seed(0)" before extracting the random numbers.
In a there are 51 negative values
# Write here your own solution
# i)
ii) Convert the negative values in NaN (np.nan)
# ii)
a = np.where(a<0,np.nan,a)
print(a)
[0.09762701 0.43037873 0.20552675 0.08976637 nan 0.29178823 nan 0.783546 0.92732552 nan 0.58345008 0.05778984 0.13608912 0.85119328 nan nan nan 0.66523969 0.5563135 0.7400243 0.95723668 0.59831713 nan 0.56105835 nan 0.27984204 nan 0.88933783 0.04369664 nan nan 0.54846738 nan 0.1368679 nan 0.23527099 0.22419145 0.23386799 0.88749616 0.3636406 nan nan 0.39526239 nan 0.33353343 0.34127574 nan nan nan nan 0.14039354 nan 0.97674768 nan nan nan 0.30621665 nan nan nan nan nan 0.31265918 nan nan nan 0.64198646 nan 0.67588981 nan 0.95291893 nan 0.95352218 0.20969104 0.47852716 nan nan nan nan nan nan nan nan 0.38494424 0.13320291 nan 0.04649611 nan 0.15189299 0.8585924 nan 0.33482076 nan 0.43265441 nan nan 0.17302587 nan 0.65788006 nan]
# Write here your own solution
# ii)
iii) Find how many NaN are in a
# iii)
nan_val = (np.isnan(a)).sum() # or nan_val = np.sum(np.isnan(a)
print('In a there are %i NaN values' % nan_val)
In a there are 51 NaN values
# Write here your own solution
# iii)
iv) Convert NaN in 0
# iv)
a = np.where(np.isnan(a),0,a)
print(a)
[0.09762701 0.43037873 0.20552675 0.08976637 0. 0.29178823 0. 0.783546 0.92732552 0. 0.58345008 0.05778984 0.13608912 0.85119328 0. 0. 0. 0.66523969 0.5563135 0.7400243 0.95723668 0.59831713 0. 0.56105835 0. 0.27984204 0. 0.88933783 0.04369664 0. 0. 0.54846738 0. 0.1368679 0. 0.23527099 0.22419145 0.23386799 0.88749616 0.3636406 0. 0. 0.39526239 0. 0.33353343 0.34127574 0. 0. 0. 0. 0.14039354 0. 0.97674768 0. 0. 0. 0.30621665 0. 0. 0. 0. 0. 0.31265918 0. 0. 0. 0.64198646 0. 0.67588981 0. 0.95291893 0. 0.95352218 0.20969104 0.47852716 0. 0. 0. 0. 0. 0. 0. 0. 0.38494424 0.13320291 0. 0.04649611 0. 0.15189299 0.8585924 0. 0.33482076 0. 0.43265441 0. 0. 0.17302587 0. 0.65788006 0. ]
# Write here your own solution
# iv)
type(a[0])
numpy.float64
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)
# v)
a = np.rint(a) # Approximate to the nearest integer, you can also use np.round()
a = a.astype('int16')
print(a)
print(type(a[0]))
[0 0 0 0 0 0 0 1 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0] <class 'numpy.int16'>
# Write here your own solution
# v)