Lecture 6 bis : Additional exercices (basics of python)

December, 2021 - 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