Lecture 6 bis : Additional exercices (basics of python)

May, 2022 - François HU

Master of Science - EPITA

This lecture is available here: https://curiousml.github.io/

image.png

Exercices

These exercices are considered "easy" exercices.

Exercice 1: functions - easy

Create a function add_more, that produces the following results:

>>> add_more(1, 1)
3
>>> add_more(1, -1)
1
>>> add_more(5, 0)
6

Exercice 2: lists - easy

Crate a function create_list, that produces the following results:

>>> create_list(5)
[1, 4, 9, 16, 25]
>>> create_list(10)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> create_list(-1)
[]

Exercice 3: lists - easy

Create a function add_lists that produces the following results (we assume that the two lists have same length)

>>> add_lists([1, 2, 3], [-1, 2, -2])
[0, 4, 1]
>>> add_lists([1, 0, 0], [-1, 2, -2])
[0, 2, -2]
>>> add_lists([1, 0, 0], [0, 1, 1])
[1, 1, 1]

Exercice 4: lists - normal

Create a function add_lists_improved that produces the following results (we don't assume that the two lists have same length)

>>> add_lists_improved([1, 2, 3], [-1, 2, -2])
[0, 4, 1]
>>> add_lists_improved([1, 2, 3], [-1, 2, -2, 1, 2])
[0, 4, 1, 1, 2]
>>> add_lists_improved([1, 0, 0, -1, 0], [0, 1, 1])
[1, 1, 1, -1, 0]

Exercice 5: dictionaries - easy

We have the following dictionary students (don't forget to execute the cell):

Exercice 6: dictionaries - normal

Let us continue working with the previous dictionary students.

Exercice 7: strings - easy

Create a function concatenate_str that produces the following results:

>>> concatenate_str("I", "am")
I am
>>> concatenate_str("a", "student")
a student
>>> concatenate_str("of", "EPITA")
of EPITA

Exercice 8: array - easy/normal

Do these exercices first with explicit loops and then without explicit loops.

  1. Generate the following array without manually fill in the numbers. Name this array arr1.
     [[ 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]]
  2. Extract from arr1 the following submatrix. Name it subarr1.
     [[ 9  8 10 11]
      [15 14 16 17]
      [21 20 22 23]
      [27 26 28 29]]