Skip to Main Content

Python for Basic Data Analysis

Start your data science journey with Python. Learn practical Python programming skills for basic data manipulation and analysis.

Fun Exercises

Now that we have covered some of the basics, lets make use of what we have learnt in these exercises.

Exercise 1

Replace all odd numbers in the given array with -1


Start with:

exercise_1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])


Desired output:

[ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1]

Exercise 2

Convert a 1-D array into a 2-D array with 3 rows

Start with:

exercise_2 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])


Desired output:

[[ 0, 1, 2]
[3, 4, 5]
[6, 7, 8]]

Exercise 3

Add 202 to all the values in given array

Start with:

exercise_3 = np.arange(4).reshape(2,-1)


Desired output:

[[202, 203]
[204, 205]]

Exercise 4

Generate a 1-D array of 10 random integers. Each integer should be a number between 30 and 40 (inclusive)


Sample of desired output: 

[36, 30, 36, 38, 31, 35, 36, 30, 32, 34]


Your answer may contain different values but should fulfill the question requirements.

Exercise 5

Find the positions of:

  • elements in x where its value is more than its corresponding element in y, and
  • elements in x where its value is equals to its corresponding element in y.


Start with these:

x = np.array([21, 64, 86, 22, 74, 55, 81, 79, 90, 89]) 
y = np.array([21, 7, 3, 45, 10, 29, 55, 4, 37, 18])


Desired output:

(array([1, 2, 4, 5, 6, 7, 8, 9]),) and (array([0]),)

Exercise 6

Extract the first four columns of this 2-D array

Start with this: 

exercise_6 = np.arange(100).reshape(5,-1)

 

Desired output:

[[ 0 1 2 3]
[20 21 22 23]
[40 41 42 43]
[60 61 62 63]
[80 81 82 83]]