Slides 2021/22

old 2020/21 slides

Part A

Lab A.1

Thursday 23 Sep 2021

Lab A.2

Thursday, Sep 30th, 2021

Tools recap

Exercises how to:

  • I will update softpython / sciprog often, so please download stuff on lab day

  • NOTE 1: when I ask you think what a certain code does, write down the answer, don’t just think about it - to pass the exam you must have zero uncertainties about syntax & expressions

  • NOTE 2: many times I will ask you to not use if statements, even if that solution would be more elegant & short: the reason is I want you to get familiar with boolean expressions, even if they may look ugly.


In class: Basics 4 challenge

The rest is left as homework:


In class: Strings 5 - Challenge

The rest is left as homework:


Lab A.3

Friday, Oct 1st, 2021


In class: Lists 5 - Challenges

The rest is left as homework:


In class: Tuples 2 - Challenges

The rest is left as homework:


EXTRA challenges

Since for now I want you to focus on expressions, first challenges on softpython don’t require loops. Still, if you want, you can generalize them with following extra tasks:

  • Lists - Super DUPER sorted: 2) Generalize previous approach so it works with and number of words. For this you will need cycles and/or if statements

  • Lists - Toys in the Attic: 2) Generalize the previous approach. Imagine you have an attic which has an unspecified number of categories (here it’s 5 but it could 1000):

attic = [3, 'doll',     'lego',      'minicar',
         2, 'frame',    'brushes',
         4, 'bike',     'pump',      'racket',  'ball',
         2, 'vase',     'fertilizer',
         3, 'old chair','lamp deco', 'stool']

and you want to obtain a list of lists like this:

    ['frame', 'brushes'],
    ['bike', 'pump', 'racket', 'ball'],
    ['old chair','lamp deco','stool']

(without caring about the variables toys, painting and sports)

  • In this case, you can and should use cycles and/or if statements when needed.

Lab A.4

Thursday, Oct 7th, 2021



Passerini didn’t cover them, if you want have a look at

They may be useful to start, since keys in dictionaries behave much like sets.


In class: Dictionaries 5 - Challenges

  • they always have a fixed number of inputs, but you may wish to generalize them with loops

The rest is left as homework:


In class: if 2: challenge

The rest is left as homework:

References: Andrea Passerini - complex statements

only for the pros

on repl: fill_sides

Lab A.7

Friday, Oct 15th, 2021

Lab A.8

Thursday, Oct 21st, 2021


  • today’s lab (Thursday ) will start at 16:30 in b106

  • on Friday 22nd there will be no lab

  • next week labs restart regularly

Lab A.9

Thursday, Oct 28th, 2021

Looking at the midterm

  1. After this lab, consider trying the old Part A exams which don’t display networks.

  2. IMPORTANT: there is a References part on SoftPython, where you can find extra resource for Part A of this course. In particular, I recommend doing:

    • the explicitly named exercises from Edabit and LeetCode, plus you can find more on HackerRank and Geeks for Geeks

    • have a look at Foundations of Python programming book in particular the worked projects

    • If you struggle with programming, the stuff above is not optional!

Lab A.10

Friday, Oct 29th, 2021

Lab A.11

Thursday, Nov 4th, 2021

Today we are going to see applications, particular how to display relational data.

Note focus in Part A is just displaying, not exploration algorithms (which we will see in Part B)

First install required libraries (graphviz, pydot, networkx)

If for some reason GraphViz doesn’t work on your system: ask/mail mail me, and in the meanwhile you could use an unholy hack on repl which also uses the online service Gravizo and matplotlib (you can also run it from your laptop). Just run it, wait for the thing to load all the modules, and you should see some graph - notice you might need to properly center it.

Looking at the midterm

After this lab, consider trying the remaining old Part A exams which display networks.

In class:

In class try doing one exercise per type, es:

  • matrices: Trichain, Bipartite (from soft import draw_mat)

  • adjacency lists: Friends (from soft import draw_adj)

  • networkx: Offshore (from soft import draw_nx)

Lab B.1

Thursday Nov 11th, 2021

New tutoring service

by Gabriele Masina (gabriele.masina (guess what) has been set up on the following timetable starting from Monday 15 november until Wednesday 15 december (included)

  • Mondays: 9:30-11:30 A202

  • Wednesdays: 9:30-11:30 A202

Part B Focus

  • complex data structures

  • perfomance


  • big .py files

  • debugging

  • testing


From now on we will:

  • only use Visual Studio Code

  • only edit .py files


Btw, if you want you can even load jupyter notebooks (.ipynb) inside VSCode

New testing: In part B we use unittest: see errors and testing on sciprog

  • in particular, DO READ TROUBLESHOOTING - you are very likely to encounter problems

Let’s go object oriented: OOP (only first part, next time we will do challenges together)

Lab B.2

Thursday Nov 18th, 2021


Have a look at Indexing chapter: probably you will have a hard time understanding the exercises and your solution will look different from proposed ones: try asking yourself why indexing with dictionaries might save computation time

From now on, in general try avoiding:

  • in operator on sequences like strings, lists, tuples: it scans the whole sequence!

  • search methods (.find, .index, .remove…)

    • in particular methods from strings (.replace,etc): PartB is about algorithms, not Python tricks!

  • pop(k) (but pop(-1) is fine)

  • needlessly using slices, they create new data structures!

Lab B.3

Friday Nov 19th, 2021

worksheets on sciprog:

NOTE: the worksheet is in progress, I will surely add material on multiple recursion

In general, when you don’t know how to tackle a recursive problem, first try the SimpleFP way and then ModAcc

Simple functional programming (SimpleFP)

  • immutability

  • very limited instructions set

  • never re-assign variables

  • always create NEW memory regions

  • don’t care about performance

Recursion with modifiable accumulators (ModAcc)

  • indexes instead of slicing

  • extra accumulators variables

  • helper functions

  • you can mutate accumulators

  • care more about performance


Lab B.4

Thursday Nov 25th, 2021

I split recursion into many pages and added stuff, see Recursion 1 - simplefp, Recursion 2 - accumulators and indexes and Recursion 4 - challenges

Today we see:

Lab B.5

Friday Nov 26th, 2021

In lab:

At home:

[ ]: