Lab activity

The purpose of the lab is to practice and measure one’s preparation: the exercises are not too difficult, if you have followed the lectures. However, no score is put on them.

Working mode: students should work independently or in small groups, without disturbing colleagues. Group work is fruitful only if everyone participates and if everyone writes their own solution for all exercises.

The teacher will try as much as possible not to occupy the time of the laboratory to introduce new material, although sometimes this will be necessary. The lecturer is available to help students, who can start working even before the lecturer arrives in the classroom, if they wish.

Recommendations: Read the text of the exercises carefully before ask for clarification. In any case, I will be in the classroom with you.

Using test files: To help you complete this exercise you have test programs available to test your solution. These are similar to those you will have on the exam, so I recommend that you learn how to use them. To complete the exercise you will need to

where <fileditest> should obviously be replaced by the name of the test appropriate for the exercise you are working on. For each exercise there is an independent test file, so that you can easily work on the exercises one at a time.

The result of each test is a screen(s) in which it is shown:

For each function written, calls are made with different values of the parameters. The outcome of the tests is reported with the character


Create a dictionary associating integers to their squares

Write a function create_square_dict(n) that:

For example:

print(create_square_dict(5)
{1: 1, 2: 4, 3: 9, 4: 16}

Python code must be saved in file: create_square_dict.py

Test file (download by right click): test_create_square_dict.py


Create a dictionary from a key list and a value list

Write a function create_dict_from_lists(n) that:

For example:

print(create_dict_from_lists(["cat", "dog", "mouse"],
                             ["gatto", "cane", "topo"]))
{'cat': 'gatto', 'dog': 'cane', 'mouse': 'topo'}

Python code must be saved in file: create_dict_from_lists.py

Test file (download by right click): test_create_dict_from_lists.py