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 numpy as np
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, so it is only important that you find the same results.
To solve the exercises you need to be aware of the if statement and its use. You can find out more details in this link: https://www.w3schools.com/python/python_conditions.asp
i) Find the numbers that are divisible by 7 and save them in an array b
# i)
a = np.arange(1000)
b = [] # Define b as a list
for i in range(len(a)):
if a[i]%7==0: # "%" gives the remainder of the division, for example: 8/2 = 4 with remainder = 0 then 8%2 = 0
b.append(a[i]) # 9/2 = 4 with remainder = 1 then 9%2 = 1
b = np.array(b) # Convert b in an array
print(b)
[ 0 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210 217 224 231 238 245 252 259 266 273 280 287 294 301 308 315 322 329 336 343 350 357 364 371 378 385 392 399 406 413 420 427 434 441 448 455 462 469 476 483 490 497 504 511 518 525 532 539 546 553 560 567 574 581 588 595 602 609 616 623 630 637 644 651 658 665 672 679 686 693 700 707 714 721 728 735 742 749 756 763 770 777 784 791 798 805 812 819 826 833 840 847 854 861 868 875 882 889 896 903 910 917 924 931 938 945 952 959 966 973 980 987 994]
# Alternative solution
b = a[a%7==0] # a%7==0 is our condition and with the square brackets we can select the elements of the array a
print(b) # where the condition is True
[ 0 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210 217 224 231 238 245 252 259 266 273 280 287 294 301 308 315 322 329 336 343 350 357 364 371 378 385 392 399 406 413 420 427 434 441 448 455 462 469 476 483 490 497 504 511 518 525 532 539 546 553 560 567 574 581 588 595 602 609 616 623 630 637 644 651 658 665 672 679 686 693 700 707 714 721 728 735 742 749 756 763 770 777 784 791 798 805 812 819 826 833 840 847 854 861 868 875 882 889 896 903 910 917 924 931 938 945 952 959 966 973 980 987 994]
# Write here your own solution
# i)
ii) Print all the values from 0 to 10 and all the values from 80 to 90 in b
# ii)
for i in range(len(b)):
if b[i]<10:
print(b[i])
elif 80<b[i]<90:
print(b[i])
0 7 84
# Alternative Solution
print(b[b<10])
print(b[(80<b) & (b<90)])
[0 7] [84]
# Write here your own solution
# ii)
The Fibonacci sequence is the series of numbers : 0, 1, 1, 2, 3, 5, 8, ....
Every next number is found by adding up the two numbers before it.
Start with 0 and 1, for example a = [0,1] and then find the remaining numbers up to 1000
a = [0,1]
for i in range(100):
val = a[i+1] + a[i]
if val<=1000:
a.append(val)
else:
break
print(a)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
# Alternative Solution
a = [0,1]
i = 0
val = 0
while val<=1000:
val = a[i+1] + a[i]
a.append(val)
i += 1
a = a[:-1]
print(a)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
# Alternative Solution
a = np.zeros(100, dtype='int')
a[1] = 1
for i in range(2,100):
a[i] = a[i-2] + a[i-1]
if a[i]>1000:
a = a[:i]
break
print(a)
[ 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987]
# Write here your own solution