greends-ipython

Introduction to Python 2026/2027

Masters in Data Science applied to agricultural and food sciences, environment, and forestry engineering.

Instructor: Manuel Campagnolo (mlc@isa.ulisboa.pt)

Teaching assistant: Hannah Nathanson (hnathanson@edu.ulisboa.pt)


Revised class schedule: class (Friday, room 48) will start at 11:45 and end at 14:45, with a ~45 minute break for lunch.


Online resources for the course

* You need to self-register for the course in Moodle at <https://elearning.ulisboa.pt/user/index.php?id=12863>. The Moodle site allows you to access assignments; submissions; detailed grades and comments * If you don't have one already, create a GitHub account at <https://github.com/signup> * [CS50P: CS50’s Introduction to Programming with Python](https://cs50.harvard.edu/python). Lectures (videos and notes), problems sets, shorts; * [CS50 codespace](https://cs50.dev/). VScode codespace that allows you to use a customized AI bot (CS50's duck debugger, or DDB for short) and test your solutions for CS50P proposed problems (you can access the codespace with your GitHub user). * [Python tutor](https://pythontutor.com/). Python Tutor is a online tool that lets you write code in a web browser and see what happens step-by-step as the computer runs it. * [Fenix webpage for the course](https://fenix.isa.ulisboa.pt/courses/intpy-564938523285845): administrative information and final grades * [Help me](https://1drv.ms/o/c/0bedfe7ffa5a0eb8/IgA-CgpLN8IVTJsDg9FJ9m67AQrx95SYxoXvGRBOMDVRwjI): general help, assignements
Advanced exercises, alternative tutorials and reference materials
  • Codewars is an online educational platform used to practice, improve, and master programming skills through gamified coding challenges in Python and many other programming languages. You can log in with your GitHub account and choose your proficiency level. Codewars will propose exercises for your level.
  • Learn Python with Scrimba, Olof Paulson: interactive lectures (videos), examples and exercises
  • Introduction to Python (VScode): interactive step by step lectures and exercises, real-time quizzes.
  • Basic concepts and features of the Python language and system: The Python Tutorial at python.org.
  • Python Programming course at PP.fi: same features as CS50 but to test your solutions to problems you are required to pass previous tests
  • If you curious about the story of Python, check out this video. In this video, you’ll learn about the output of >>> import this and many other anecdotes about Python.

</blockquote>

</details> ---

Class 1 (September 11, 2026): data types, variables, lists, functions

1. The recommendation for this class is to code using the CS50 codespace. Two steps: 1. log in into your GitHub account; 2. access your code space at <https://cs50.dev/>. This environment allows you to test automatically your scripts for the CS50 problem sets and gives access to a customized AI bot (DDB) 2. Some useful keyworks for the command line interface (CLI): * `code filename.py` to create a new file * `ls` to list files in folder * `cp filename newfilename` to copy a file, e.g. `cp ..\hello.py farewell.py` (`..` represents parent folder) * `mv filename newfilename` to rename or move file, e.g. `my farewell.py goodbye.py` or `mv farewell.py ..` (move one folder up) * `rm filename` to delete (remove) file * `mkdir foldername` to create new folder * `cd foldername` change directory, e.g. `cd ..` * `rmdir foldername` to delete folder * `clear` to clear terminal window 3. Typing `python` gives you access to a REPL (interactive Read-Eval-Print-Loop) environment (<https://realpython.com/interacting-with-python/>). 4. All values in Python have a **type**. The **primitive data types** are: integer, float, string, Boolean, and None (see <https://www.geeksforgeeks.org/python/primitive-data-types-vs-non-primitive-data-types-in-python/>). There are also **Non-Primitive Data Types** like `list`, `tuple`, `set` and `dictionary`. * strings (`str`), variables, print (a function), parameters (e.g. `end=`), input, comments, formatted strings (`f"..."`), `.strip()`, `.title` (methods) * integers (`int`), operations for integers, casting (e.g. `str` to `int`) * floating point values (`float`), round, format floats (e.g. `f"{z:.2f}`) * Boolean: `True`, `False`, that can be combined with operators `and`, `or`, `not` 5. Functions, `def`, `return`. Example: ``` def square(x): return x*x ``` 6. View the step-by-step execution of this code on [Python Tutor](https://pythontutor.com/visualize.html#code=def%20main%28%29%3A%0A%20%20%20%20x%3D4%0A%20%20%20%20y%3Dsquare%28x%29%0A%20%20%20%20print%28f%22%7Bx%7D%20squared%20is%20%7By%7D%22%29%0A%0Adef%20square%28n%29%3A%0A%20%20%20%20s%3Dn*n%0A%20%20%20%20return%28s%29%0A%0Amain%28%29&curInstr=12&mode=display&origin=opt-frontend.js&py=311). Identify the scope of the variables. Try to add `print(s)` in line 4 after calling function `square` inside the `main` function: why do you get a `NameError`? 7. Suggested problems: [CS50 Problem set 0](https://cs50.harvard.edu/python/psets/0/). Find a solution to the problems at your CS50 codespace, and dialog with the AI bot (DDB) for help. The bot won't produce code but will help you by answering your to questions: very precise prompts originate more details in the answers. Resist the temptation of looking for a solution on the web or using a generic AI bot.
---

Class 2 (September 18, 2026): conditionals, lists, dictionaries

1. See: - [notes on CS50P lecture 1](https://cs50.harvard.edu/python/notes/1/): conditionals `if`, `match`, boolean expressions, etc - [notes on CS50P lecture 2](https://cs50.harvard.edu/python/weeks/2/): `for` and `while` loops, etc 3. Conditionals: - `if`, `elif`, `else`: ```Python if score >= 70: print("Grade: C to A") elif score >= 60: print("Grade: D") else: print("Grade: F") ``` - `match`: ```Python match species: case 'versicolor': label=0 case 'virginica': label=1 case _: label=2 ``` 3. Pythonic coding: `def main()`, define other functions, call `main()`. The code must be modular. 4. While loops, for loops, `break`, `break` and `return` ``` s='intro to python' for i in range(15): print(s[i]) ``` ``` # require user to provide a positive number while True: n = int(input("What's n? ")) if n > 0: break ``` 6. Data type *list* `[]`: methods `append`, `extend` 7. Data type *dictionary* `{}`, `items()`, keys `.key()` and values `.values()` ```Python knights = {'gallahad': 'the pure', 'robin': 'the brave'} for k, v in knights.items(): print(k, v) if 'gallahad' in knights: print('Go Gallahad') ``` 8. Suggested problems: [CS50 Problem set 1](https://cs50.harvard.edu/python/2022/psets/): Deep Thought, Home Federal Savings Bank, Math Interpreter, Meal Time 9. Assignment #1 available on Moodle: problems [File extensions (set 1)](https://cs50.harvard.edu/python/2022/psets/1/extensions/), [Coke machine (set 2)](https://cs50.harvard.edu/python/2022/psets/2/coke/), [Plates (set 2)](https://cs50.harvard.edu/python/2022/psets/2/plates/)
---

Class 3 (September 25, 2026): list and dictionary comprehensions, collaborative exercise

1. See lecture on [List and dictionary comprehensions](https://cs50.harvard.edu/python/shorts/list_dictionary_comprehensions/) 2. [Exercises on list comprehension (with some solutions)](/greends-ipython/docs/exercises_list_comprehension.html) 2. [Exercises on dictionary comprehension (with some solutions)](/greends-ipython/docs/exercises_dict_comprehension.html) 3. Collaborative project: each student or small group of students should define each necessary function to complete the script below. The side effect of `main()` is a simple histogram printed in the terminal. ```Python from read_values import read_values from determine_min_max import determine_min_max from number_of_classes_sturges import number_of_classes_sturges from amplitude import amplitude from determine_frequency import determine_frequency from print_frequency import print_frequency def main(): # read values x=read_values() # x is a list of numbers, either integers or floats n=len(x) # integer; number of values # determine min value and max value xmin,xmax=determine_min_max(x) # integers or floats # determine number of classes m=number_of_classes_sturges(n) # m is a positive integer such that 2**(m-1) <= n <= 2**m # determine class amplitude delta=amplitude(xmin,xmax,m) # positive float, the range of values divided by the number of classes # Compute frequency for each class and plot histogram row by row for i in range(m): left=xmin+i*delta if i < m-1: right=left+delta else: right=left+delta+1 # either 1 or any positive value freq=determine_frequency(x,left,right) # integer; note that each value must belong to one and only one class print_frequency(freq) # the output must be '****' where each * represents one observation # execute main main() ``` --- To share code, students should submit a *pull request* on GitHub. Test how to perform a pull request with a trivial `run()` function for each team: ```text 1. Fork the course repository `https://github.com/isa-ulisboa/greends-ipython` on your GitHub account 2. In your fork, go to `collaborative_proj/submissions/`. 3. Create a file called `team_name.py` with your own team name, say `team_01.py` 4. Add a `run()` function to the file with contents like `print('team 1')` 5. Commit the file. 6. Click Contribute → Open pull request. 7. Make sure it goes from your fork to the teacher’s original repository `https://github.com/isa-ulisboa/greends-ipython` 8. Click `Create pull request`. ``` If the teacher's original repository changes after you forked it (for instance, the README is updated or other teams' pull requests are merged), your fork does not update automatically. Before working on a new submission, bring your fork up to date: ```text 1. Open your fork on GitHub (https://github.com//greends-ipython). 2. If it says "This branch is X commits behind isa-ulisboa/greends-ipython:main", click Sync fork → Update branch. 3. If you also have a local clone of your fork, run `git pull` in it to get the updates. ``` Alternatively, from a local clone of your fork, using the command line: ```bash git remote add upstream https://github.com/isa-ulisboa/greends-ipython.git # only the first time git fetch upstream git merge upstream/main git push origin main ``` </blockquote> </details> ---

Class 4 (October 2, 2026): handling exceptions in Python: catching and raising exceptions

  1. See CS50P lecture 3, in particular the short on handling exceptions: try, except, ValueError, KeyError.

  2. A few examples of code that can be helpful to solve problems in CS50 Problem set 3.

Example of basic use of `try-except` to catch a `ValueError`: ``` try: x = int(input("What's x?")) except ValueError: print("x is not an integer") else: print(f"x is {x}") ``` Function for requesting an integer from the user until no exceptions are caught: ``` def get_int(): while True: try: x = int(input("What's x?")) except ValueError: print("x is not an integer") else: break return x ``` For a list of Python Built-in Exceptions, besides `ValueError`, you can check https://www.w3schools.com/python/python_ref_exceptions.asp
  1. The fuel gauge problem (https://cs50.harvard.edu/python/2022/psets/3/fuel/)
To solve this problem, try to organize your code as follows. As suggested in *hints*, you should catch `ValueError` and `ZeroDivisionError` exceptions in your code. In the code below, the user is being asked for correct values for `x,y` until they satisfy the requirements: `x,y` must be inputted as a string `x/y`, `x` has to be less or equal to `y`, and `y` cannot be zero. The function `get_string_of_integers_X_less_than_Y` in the code below should take care of that. ``` def main(): # asks user for input until the input is as expected x,y=get_string_of_integers_X_less_than_Y() # compute percentage from two integers p=compute_percentage(x,y) # print output print_gauge(p) ```
  1. Example from https://cs50.harvard.edu/python/2022/shorts/handling_exceptions/
Exercise: adapt the code proposed in the short to be more modular, where the main function is something like the one below: ``` def main(): spacecraft = input("Enter a spacecraft: ") au=get_au(spacecraft) m = convert(au) print(f"{m} m") ```
  1. Other useful applications of try-except
- We may want to exit the execution of our script if some exception is caught. This can be done with `sys.exit()`, which can also be used to print a message. ``` import sys # import module try: x = int(input("What's x?")) except ValueError: sys.exit("x is not an integer") ``` - Example of code that catches `CRTL-C` or `CRTL-D`: ``` while True: try: x=int(input()) except ValueError: print('x is not integer') except KeyboardInterrupt: #CTRL-C (in Linux, interrupt execution) print('\n KeyboardInterrupt') break except EOFError: #CTRL-D (in Linux, log out terminal/end-of-file) print('\n EOFError') break else: print(x) ```
  1. Exercise (Asking for an haphazard list of numbers): Create a program that asks the user to provide haphazardly a series of numbers that you want to store in a list. The user is asked for a number at the time. Only inputs that are numbers are stored in the list. When the user wants to stop, it should type CTRL-D. Then, the program should print the list of numbers.
---

Class 5 (October 9, 2026): modules, packages, APIs

  1. See lecture https://cs50.harvard.edu/python/weeks/4/

  2. Modules and packages

Suggestion: watch <https://cs50.harvard.edu/python/shorts/creating_modules_packages/> Modules are just python scripts (files like `module_name.py`) which can be imported into your main code and that can be shared between multiple applications. You can import everything that belongs to the module, or just some given function(s) or other objects.
Create and import your own module

Exercise: Create file named utils.py and file main.py in http://cs50.dev. Organize the files in the following folders:

|--- class_5 # or whatever folder name you wish
     |--- common # General reusable code used across the project
          |--- __init__.py # optional: marks the folder as a package
          |--- utils.py
     |--- main.py

The contents of utils.py are typically functions or constants that you can re-use in different contexts. Let’s suppose that utils.py has the following contents.

utils.py
import sys

Constants={
    'e'   : 2.718281828459045, # Euler's constant
    'pi'  : 3.141592653589793, # Archimedes' constant
    'phi' : 1.618033988749895  # Golden ratio
}

def get_integer() -> int:
    #get integer from user
    while True:
        try:
            return(int(input('Type a number:  ')))
        except ValueError:
            print('Not an integer number: try again')
        except KeyboardInterrupt: #CTRL-C
            print('\n If you want to exit type CTRL-D')
        except EOFError: # CTRL-D
            sys.exit('\n Exit as requested')

and main.py is the following file:

main.py (1st version)
import common.utils

def main():
    x=common.utils.get_integer()
    print(x)

main()

Run main.py from inside the class_5 folder (cd class_5, then python main.py), so that Python can find the common folder.

If you prefer, you can explicitly import some given functions from the module as in the following example.

main.py (2nd version)
from  common.utils import get_integer

def main():
    x=get_integer()
    print(x)

main()

You can also import everything from the module with from common.utils import * instead of the more specific (and recommended) from common.utils import get_integer.

The examples above follow the directory tree that was suggested. If you change the module’s location, you need to adapt the code accordingly. In alternative, you can add the path to the directories where your modules lies to sys.path as in the following example.

import sys
sys.path.append(r'path-to-folder') # folder that contains utils.py (e.g. `/workspaces/8834091/class_5/common`)
import utils
As explained on the recommended video, a python **package** is just a folder with modules and a special file named `__init__.py`
Pip install

Some modules, like sys, math or random, are part of the Python standard library: they come with Python and can be imported without installing anything. For instance, random provides a series of functions for sampling, shuffling, and extracting random numbers from a variety of probability distributions.

Many other packages are available at the Python Package Index, https://pypi.org/. They need to be installed before they can be imported, which is done in the terminal with pip. For instance, the package requests (used below to access APIs) is installed with

$ pip install requests

and then imported in your script with import requests. If you want to know the folder where a module is located, you can get that information with the attribute __file__, as in the following script. Compare the two folders: the standard library and the installed packages (site-packages) are in different places.

import random
import requests
print(random.__file__)
print(requests.__file__)

Suggestion: write a script to estimate the value of $\pi$ with a Monte Carlo algorithm that makes calls to random.uniform(-1, 1). One possible solution: https://www.geeksforgeeks.org/dsa/estimating-value-pi-using-monte-carlo/

  1. sys.argv
Previously, we used module `sys`, in particular functions `sys.exit()` and `sys.path`. Another useful function is `sys.argv`, that allows you to have access to what the user typed in at the command line `$` as in the following script. ```Python import sys print(len(sys.argv)) # returns the number of words in the command line after $python print(sys.argv[1]) # returns the 2nd word, i.e., the first word after $python myscript.py ``` For instance, the following script named `sum.py` prints the sum of two numbers that were specified in the command line.
sum.py
# sum.py
import sys
try:
    x,y = float(sys.argv[1]), float(sys.argv[2])
    print('the sum is',x+y)
except IndexError:
    print('missing argument')
except ValueError:
    print('The arguments are not numbers')
To run it, you can for instance execute the command `$python sum.py 1.2 4.3` in the terminal.
  1. APIs
Suggestion: watch https://cs50.harvard.edu/python/shorts/api_calls/ (13') *Application program interfaces* allow you to communicate with a remote server. For instance, `requests` is a package that allows your program to behave as a web browser would. Consider the following script `myrequest.py` that allows you to explore the *itunes* database (https://performance-partners.apple.com/search-api):
Example: iTunes
import requests
import sys
try:
    response = requests.get("https://itunes.apple.com/search?entity=song&limit=2&term=" + sys.argv[1])
    print(response.json())
except IndexError:
    sys.exit('Missing argument')
except requests.RequestException:
   sys.exit('Request failed')

You can then call the API from your terminal with $python myrequest.py 'name of my favorite band'. What you see printed on the terminal is a dictionary. How can you edit your script such that the output is just a list of track names (key trackName)? Notice that &limit=2 in the search sets the number of songs that are returned by the API.

Example: GBIF

You can easily adapt that code to access a different database. For instance if you want to explore the GBIF database https://data-blog.gbif.org/post/gbif-api-beginners-guide/, you can just replace the main line of code in myrequest.py with

response=requests.get('https://api.gbif.org/v1/species/match?name='+ sys.argv[1])

and execute it with, say, $python myrequest.py Tracheophyta in the terminal.

Example: open-meteo

Another example of a useful API for weather data is https://open-meteo.com/en/docs#api_documentation. You can find a customized requests package for open-meteo at https://pypi.org/project/openmeteo-requests/.

import openmeteo_requests

openmeteo = openmeteo_requests.Client()

# Make sure all required weather variables are listed here
# The order of variables in hourly or daily is important to assign them correctly below
url = "https://api.open-meteo.com/v1/forecast"
params = {
	"latitude": [38.74,41.15], # Lisbon, Porto
	"longitude": [-9.14,-8.6] ,
	"current": ["temperature_2m", "relative_humidity_2m","wind_speed_10m"],
}
responses = openmeteo.weather_api(url, params=params)

# Process first location. Add a for-loop for multiple locations or weather models
for i in range(len(responses)):
    response = responses[i]
    print(f"Coordinates: {response.Latitude()}°N {response.Longitude()}°E")
    print(f"Elevation: {response.Elevation()} m")

    # Process current data. The order of variables needs to be the same as requested.
    current = response.Current()
    print(f"Current temperature_2m: {current.Variables(0).Value()}")
    print(f"Current relative_humidity_2m: {current.Variables(1).Value()}")
    print(f"Current wind_spreed_10m: {current.Variables(2).Value()}")
    print()
  1. Suggested problem
Solve problem *Bitcoin price index* from CS50P [Problem_set_4](https://cs50.harvard.edu/python/2022/psets/4/). In particular, organize your code so the main function is the following: ``` def main(): x=read_command_line_input() price=get_bitcoin_price() print(f"${x*price:,.4f}") ```
---

Class 6 (October 16, 2026): virtual environments; file I/O with images

In this class we build a small project, image_project, that downloads and processes images of leaves. First we prepare a virtual environment with the packages the project needs (requests and Pillow), then we use it to read and write text and image files. At the end of the class, the project folder should look like this:

image_project/            <- all commands are run from here
|--- .venv/               <- the virtual environment (created by python -m venv .venv; not shared)
|--- .gitignore           <- tells Git to ignore .venv/
|--- requirements.txt     <- list of packages and versions (shared)
|--- images/              <- input images
|--- output/              <- files created by our scripts
|--- notes.py
|--- download_image.py
|--- explore_image.py
|--- process_images.py
    1. Why virtual environments?

    Suppose your script was written and tested with version 12.0.0 of Pillow, but a colleague has an older version installed that behaves differently, or another project of yours needs a different version. Installing every package in the same place (the base environment) quickly leads to conflicts. A virtual environment solves this problem. It is:

    • A folder that contains a Python interpreter and a set of packages, with given versions, for one project. It is isolated from other virtual environments and from the packages installed in the base environment.
    • Conventionally named .venv and placed at the root of the project folder. Tools such as VS Code detect it automatically.
    • Disposable: it should be simple to delete and re-create it from scratch. You never place your scripts or data inside .venv.
    • Not shared (e.g. not checked into Git) and not copied to another computer. Instead, one shares the list of packages (requirements.txt) and each person re-creates the environment.

    Let’s first look at the base environment. The following commands list the installed packages and show information about the package requests (version, location, dependencies):

    $ pip list
    $ pip show requests
    

    The commands below work in the CS50 codespace, which runs Linux. When a command is different in Windows, the Windows version is given in a comment.

    2. Create and activate a virtual environment
    1. Create the project folder and move into it. From now on, all commands are executed from image_project:
      $ mkdir image_project
      $ cd image_project
      
    2. Create the virtual environment. This creates the folder .venv inside image_project:
      image_project/ $ python -m venv .venv
      

      Since the name starts with a dot, .venv is a hidden folder: use ls -a to see it.

    3. Activate the environment:
      image_project/ $ source .venv/bin/activate      # Windows: .venv\Scripts\activate
      

      The prompt now starts with (.venv), which shows that the environment is active: the commands python and pip now refer to the Python and packages in .venv. To de-activate it, the command is deactivate.

    4. Check which Python is running and which packages are installed. A new environment has (almost) no packages:
      (.venv) image_project/ $ python -c "import sys; print(sys.prefix)"
      (.venv) image_project/ $ pip list
      

      sys.prefix is the folder of the Python installation in use: it should be the .venv folder of your project.

    3. Install packages and create requirements.txt
    1. With the environment active, install the packages for the project, specifying the versions:
      (.venv) image_project/ $ pip install pillow==12.0.0 requests==2.32.5
      

      Packages that these depend on (e.g. urllib3 for requests) are installed automatically. Compare the result of pip list and pip show requests with what you got in the base environment: the version and the location are different.

    2. Check where Python looks for packages:
      (.venv) image_project/ $ python -c "import sys; print(sys.path)"
      

      The site-packages folder inside .venv is listed, but the folder where the base environment packages are stored is not.

    3. Save the list of installed packages and versions to requirements.txt, at the root of the project:
      (.venv) image_project/ $ pip freeze > requirements.txt
      

      Open requirements.txt to see its contents: one line per package, such as pillow==12.0.0.

    4. Tell Git to ignore the .venv folder, by creating a file .gitignore with the line .venv/:
      (.venv) image_project/ $ echo ".venv/" > .gitignore
      

      If you share the project (e.g. on GitHub), you share the scripts, requirements.txt and .gitignore, but not .venv.

    4. Delete and re-create the environment

    Since .venv is disposable, we can delete it and re-create it from requirements.txt. This is also what a colleague does after downloading your project.

    (.venv) image_project/ $ deactivate
    image_project/ $ rm -rf .venv                     # Windows (PowerShell): Remove-Item -Recurse -Force .venv
    image_project/ $ python -m venv .venv
    image_project/ $ source .venv/bin/activate        # Windows: .venv\Scripts\activate
    (.venv) image_project/ $ pip install -r requirements.txt
    (.venv) image_project/ $ pip list
    

    Exercise: install one more package in your environment (say, emoji), update requirements.txt, and send the file to a colleague. Your colleague should create a new project folder with a .venv environment from your requirements.txt and check with pip list that both environments have the same packages and versions.

    5. Reading and writing text files

    First, create the folders for the input and output files:

    (.venv) image_project/ $ mkdir images output
    

    In this step we write a script notes.py, in the image_project folder, that writes some notes about the leaf to a text file and then reads them back. Add each piece of code below at the end of notes.py, and run the script after each addition with

    (.venv) image_project/ $ python notes.py
    

    Then check the contents of output/notes.txt (open it in the editor, or use cat output/notes.txt in the terminal).

    open is a function built into Python that allows you to open a file to read from it or to write to it. The second argument is the mode: "w" (write), "a" (append) or "r" (read, the default). In the code below, the file is opened in writing mode: if the file already exists, its previous contents are deleted. This is why running notes.py several times does not accumulate notes: each run starts with an empty file.

    # notes.py
    file = open("output/notes.txt", "w")
    file.write("olive leaf, upper surface dark green\n")
    file.close()
    

    Notice that write does not add a new line: we need to include "\n" at the end of the string. To add new contents at the end of an existing file, open it in mode "a" (append) instead.

    Instead of explicitly opening and closing a file, it’s simpler and safer to use a context manager, with the keyword with, which automatically closes the file at the end of the block, even if an error occurs. Add the following lines to notes.py, which append a second note to the file:

    with open("output/notes.txt", "a") as f:
        f.write("olive leaf, lower surface silvery\n")
    

    To read the file, open it in mode "r". The method readlines returns a list where each element is a line of the file (including the "\n" at the end). Add these lines to notes.py, so the script prints what it wrote:

    with open("output/notes.txt", "r") as f:
        lines = f.readlines()
    print(lines)
    

    It is also possible to read the file one line at a time, which is better for large files. Add this last block to notes.py and compare its output with the previous one:

    with open("output/notes.txt", "r") as f:
        for line in f:
            print(line.rstrip()) # rstrip() removes the "\n" at the end of the line
    
    6. Binary files: downloading and inspecting an image

    Files can be text files (human-readable, like notes.txt or a .py script) or binary files, like images. Binary files are opened with the modes "rb" and "wb", and their contents are bytes instead of str.

    The following script download_image.py downloads a photo of an olive leaf (upper and lower surface) from Wikimedia Commons, using the package requests (see class 5). The image is in response.content, which is a bytes object, so the file is opened in mode "wb". Wikimedia Commons rejects requests without a descriptive User-Agent (the name of the program that makes the request), so we add one in headers.

    # download_image.py
    import requests
    
    url = "https://upload.wikimedia.org/wikipedia/commons/b/b2/Olive-tree-leaf-0.jpg"
    headers = {"User-Agent": "greends-ipython-course/1.0"}
    response = requests.get(url, headers=headers)
    response.raise_for_status() # raises an exception if the download failed
    print(response.headers["Content-Type"], len(response.content), "bytes")
    
    with open("images/olive_leaf.jpg", "wb") as f:
        f.write(response.content)
    

    If the download fails, you can copy the same image from datasets/olive_leaf.jpg in this repository. (Photo: Rnbc at en.wikipedia, CC BY-SA 3.0, via Wikimedia Commons.)

    The first bytes of a file usually identify its format. Let’s read the first 4 bytes of the image:

    with open("images/olive_leaf.jpg", "rb") as f:
        first_bytes = f.read(4)
    print(first_bytes) # b'\xff\xd8\xff\xe0'
    

    All JPEG files start with b'\xff\xd8', and all PNG files start with b'\x89PNG'. Try to open the image in text mode ("r") and see what happens.

    7. Images with Pillow

    Reading the bytes of an image is not enough to work with its pixels: we need to decode the file format. The package Pillow (imported as PIL) does that for many formats. The following script explore_image.py opens the image, prints its properties and saves three new images in output.

    # explore_image.py
    from PIL import Image
    
    with Image.open("images/olive_leaf.jpg") as img:
        print(img.format, img.size, img.mode) # JPEG (1024, 768) RGB
    
        # each pixel of an RGB image is a tuple (red, green, blue) with values between 0 and 255
        print("upper surface:", img.getpixel((450, 220))) # (x, y) = (column, row), from the top-left corner
        print("lower surface:", img.getpixel((500, 580)))
    
        gray = img.convert("L") # "L" means grayscale: one value between 0 (black) and 255 (white) per pixel
        gray.save("output/olive_leaf_gray.png") # the format is given by the extension
    
        thumb = img.copy()
        thumb.thumbnail((256, 256)) # resizes in place, keeping the proportions
        thumb.save("output/olive_leaf_thumb.jpg")
    
        img.rotate(90, expand=True).save("output/olive_leaf_rotated.jpg")
    

    Questions:

    • Which surface of the leaf is brighter? Compare the values of gray.getpixel(...) at the same two points.
    • What is the size of the thumbnail? Why is it not (256, 256)?
    • Check the first bytes of output/olive_leaf_gray.png as in step 6: is it a PNG file?
    8. Exercise: process a folder of images

    Write a script process_images.py that creates a grayscale thumbnail of each image in a folder, and keeps a log of the processed files. The input and output folders are given in the command line (see sys.argv in class 5):

    (.venv) image_project/ $ python process_images.py images output
    

    For each image name.jpg (or .jpeg, .png) in images, the script saves output/name_thumb.png and appends a line name.jpg -> output/name_thumb.png to the file output/log.txt. Other files in the folder are ignored. Add a few more leaf images to images to test it.

    Use the template below. The module os (standard library) provides os.listdir(folder), which returns the list of file names in a folder, os.path.join(folder, name), which builds a path, and os.path.splitext(name), which splits a name such as "olive_leaf.jpg" into ("olive_leaf", ".jpg").

    import os
    import sys
    from PIL import Image
    
    def main():
        input_folder, output_folder = read_command_line()
        for file_name in os.listdir(input_folder):
            if is_image(file_name):
                input_path = os.path.join(input_folder, file_name)
                output_path = make_thumbnail(input_path, output_folder)
                write_log(output_folder, file_name, output_path)
    
    def read_command_line():
        '''returns the input and output folders; exits with a usage message if they are missing'''
        ...
    
    def is_image(file_name):
        '''returns True if the extension of file_name is .jpg, .jpeg or .png (in lower or upper case)'''
        ...
    
    def make_thumbnail(input_path, output_folder, size=(256, 256)):
        '''saves a grayscale thumbnail of the image as a png file in output_folder and returns its path'''
        name = os.path.splitext(os.path.basename(input_path))[0]
        output_path = os.path.join(output_folder, name + "_thumb.png")
        ...
        return output_path
    
    def write_log(output_folder, file_name, output_path):
        '''appends the line "file_name -> output_path" to output_folder/log.txt'''
        ...
    
    main()
    

    Suggested problem: CS50 P-Shirt from CS50P Problem set 6.

---

Class 7 (October 23, 2026): Jupyter notebooks; tabular data with pandas

The goal of this class is not to memorise pandas syntax, which is well documented, but to understand how pandas thinks about data, because that is what makes it a tool for data science. All the code is in the notebook class7/class7_pandas.ipynb:

  • Part 1 (expository): typed columns and vectorisation, boolean masks, the index.
  • Part 2 (guided): groupby, merge, tidy vs wide tables.

The exercises on rural fires in Portugal (INE), which were Part 3 of this class, are now in Class 8.

    Setup: a Jupyter notebook in the CS50 codespace

    A Jupyter notebook (.ipynb) mixes text cells (Markdown) and code cells. Code cells are run one at a time with Shift+Enter, and their results (tables, numbers, plots) appear right below. Variables stay in memory between cells, which makes notebooks convenient to explore data step by step.

    In the terminal of the CS50 codespace:

    1. Create a folder for the class and move into it:
      $ mkdir class7
      $ cd class7
      
    2. Install pandas, and ipykernel, which allows VS Code to run notebooks. Here we install them in the default Python of the codespace, without a virtual environment: the notebook’s kernel picker in cs50.dev does not easily find a .venv created inside a subfolder. If a virtual environment is active (its name appears in the prompt), run deactivate first.
      $ pip install pandas ipykernel
      
    3. Download the notebook and open it:
      $ wget https://raw.githubusercontent.com/isa-ulisboa/greends-ipython/main/class7/class7_pandas.ipynb
      $ code class7_pandas.ipynb
      
    4. Click Select Kernel (top right of the notebook) → Python Environments → choose the default Python 3 (not a .venv). If VS Code asks to install the Jupyter extension, accept.
    5. Run the first cell. If it shows the pandas version, you are ready.

    The datasets are read directly from the course repository, so there is no need to download them.

    If the notebook cannot be set up in the codespace, open it in Google Colab (File → Open notebook → GitHub, then paste the notebook’s URL).

    Part 1 — How pandas thinks

    1.1 From a list of rows to a table of columns. Until now, we stored records as lists of dictionaries, one per observation. A DataFrame stores them as columns, and each column (a Series) has a single data type (df.dtypes). A DataFrame can be thought of as a dictionary of columns that share the same row labels.

    1.2 Vectorisation: describe what, not how. Because a column has one type, an operation can be applied to the whole column at once, instead of looping over its elements:

    # the "Python lists" way
    temp_f = []
    for t in fires['temp']:
        temp_f.append(t * 9 / 5 + 32)
    # the pandas way
    fires['temp_f'] = fires['temp'] * 9 / 5 + 32
    

    The second version is shorter, and also much faster on large tables since the loop runs in compiled code.

    1.3 The forest fires dataset. The dataset datasets/forestfires.csv describes 517 fires in the Montesinho natural park, in Portugal (source). The variables are:

    • X, Y: cell in a 9×9 grid over the park map
    • month, day: month (jan to dec) and weekday (mon to sun); note that there is no year
    • FFMC, DMC, DC, ISI: indices of the Canadian Fire Weather Index (FWI) system
    • temp: temperature (°C); RH: relative humidity (%); wind: wind speed (km/h); rain: rain (mm/m²)
    • area: burned area (ha), from 0 to 1090.84

    The file is read with pd.read_csv and explored with .head(), .shape, .info() and .describe(). Look before you compute: the mean and the median of area are very different, because most fires burned (almost) nothing.

    1.4 Boolean masks: asking questions to the data. fires['temp'] > 25 is a column of True/False (a mask), and fires[mask] keeps the rows where the mask is True. Masks are combined with & (and), | (or), ~ (not), with each condition between parentheses, and .isin() checks membership in a list. Since True counts as 1, mask.sum() counts and mask.mean() gives a proportion.

    • Exercise 1.4: How many fires occurred on weekends? Is it more than expected if fires were spread evenly over the week?

    1.5 The index: labels, not positions. Every row of a DataFrame has a label (the index, shown on the left) and a position (0, 1, 2, …). Columns also have labels (the column names) and positions. There are two ways to select rows and columns:

      .loc[rows, columns] .iloc[rows, columns]
    selects by label position
    slice a:b includes b excludes b, like Python lists
    slice example fires.loc[0:4, 'temp':'wind']
    5 rows (labels 0 to 4), columns temp, RH, wind
    fires.iloc[0:4, 8:10]
    4 rows (positions 0 to 3), columns temp, RH (positions 8, 9)
    typical use fires.loc[fires['month'] == 'aug', ['temp', 'RH']] fires.iloc[:20, 4:8]

    When a CSV file is read, the index is 0, 1, 2, ..., so labels and positions coincide and loc and iloc seem to do the same thing. The difference appears as soon as rows are filtered, because the original labels are kept:

    hot = fires[fires['temp'] > 25]
    hot.iloc[0]      # first row of hot (by position)
    hot.loc[0]       # KeyError: there is no longer a row with label 0
    

    Labels do not need to be integers. set_index turns a column into the index:

    months = pd.DataFrame({'mth': ['jan', 'feb', 'mar'], 'name': ['January', 'February', 'March']})
    months = months.set_index('mth')
    months.loc['feb', 'name']    # 'February'
    

    Why does the index exist? Alignment. When two Series are combined, pandas matches their values by label, not by position. Labels that exist in only one of them give NaN (missing value), instead of a silent mismatch:

    a = pd.Series([1, 2, 3], index=['jan', 'feb', 'mar'])
    b = pd.Series([10, 20, 30], index=['mar', 'jan', 'feb'])
    a + b            # jan 21, feb 32, mar 13
    

    For instance, fires.loc[fires['area'] > 10, 'month'].value_counts() / fires['month'].value_counts() gives the proportion of large fires for each month, although the two counts have months in different orders.

    If needed, labels and positions can be converted into each other: fires.columns.get_indexer(['FFMC', 'DMC']) gives the positions of the columns, to use with iloc; fires.index[0:20] gives the labels of the first 20 rows, to use with loc.

    • Exercise 1.5: Use loc to select the FWI variables for the fires in August and September. Then use iloc to take the first 5 rows of the result. Why does .loc[0:4] not give the same result?
    Part 2 — Split, apply, combine; tidy and wide data

    2.1 groupby. fires.groupby('month')['area'].sum() splits the rows by month, applies sum to each group and combines the results in a new table. This replaces the “loop and accumulate in a dictionary” pattern that we used in Classes 2 and 3. Several summaries can be computed at once with .agg(...):

    by_month = fires.groupby('month').agg(n_fires=('area', 'size'), total_area=('area', 'sum'))
    

    After groupby, the group key becomes the index of the result, sorted alphabetically (apr, aug, dec, …). .reindex(labels) builds a new Series (or DataFrame) with exactly the given labels, in the given order: existing labels keep their values, new labels get NaN, and labels not in the list are dropped. The table months of section 1.5 (with all 12 months, in the notebook) has the months in calendar order as its index, so months.index is exactly the list of labels needed:

    by_month = by_month.reindex(months.index)   # rows from jan to dec
    

    .reset_index() turns the index back into an ordinary column (useful before exporting or merging).

    • Exercise 2.1: Group by day instead. On which weekday is the median burned area largest? And the number of fires?

    2.2 Joining tables: merge. merge combines two tables through a common key, like a database join. In the notebook, a table months with the full name and the season of each month is merged with fires, to compare the burned area by season.

    2.3 Tidy vs wide data. A table is tidy when (1) each variable is a column, (2) each observation is a row, and (3) each type of observational unit is a table (Wickham, 2014). The forest fires table is tidy: one row per fire, one column per variable.

    Two methods convert between the two shapes, and they are (roughly) inverse operations: pivot_table goes from tidy to wide (the values of one column become the row labels, index, the values of another column become the column names, columns, and each cell summarises a third column, values, with a function, aggfunc), and melt goes from wide to tidy (the column names become the values of a new column). “Roughly” because pivot_table aggregates: after melt we get back the totals per month and weekday, not the individual fires.

    A wide table spreads a variable across several columns. For instance, the total burned area by month and weekday (fill_value=0 fills the combinations without any fire):

    wide = fires.pivot_table(index='month', columns='day', values='area', aggfunc='sum', fill_value=0)
    

    Here, the weekday is a variable, but each weekday became a column. Wide tables are good for people (easy to read and compare, common in reports and in official statistics). Tidy tables are good for computing: filtering, groupby, merge and plotting all expect one variable per column. melt converts a wide table into a tidy (long) one:

    long = wide.reset_index().melt(id_vars='month', var_name='day', value_name='area')
    

    Rule of thumb: keep data tidy while working, and make it wide at the end, for a report.

    Reference: exporting to file

    Exporting is done with methods named .to_..., and reading with functions named pd.read_..., as listed in https://pandas.pydata.org/docs/user_guide/io.html. For instance:

    1. Export a dataframe as a CSV file with df.to_csv('filename.csv', index=False). Use index=False unless the index contains information (for instance, after groupby); otherwise, the labels 0, 1, 2, ... are written as an extra column.
    2. Export as an Excel spreadsheet with df.to_excel('filename.xlsx', sheet_name='fires', index=False) (requires pip install openpyxl).
    3. Read an Excel spreadsheet with pd.read_excel('filename.xlsx', sheet_name='fires').
---

Class 8 (November 6, 2026): pandas, continued: select, sort, group, create

This class continues Class 7, and focuses on the four operations that are needed in assignment #3:

question pandas
Which rows? masks: comparisons, & \| ~, .between, .isin, string methods .str...
In which order? sort_values (one or more columns), nlargest
What is the summary for each group? groupby + agg
How do I create a new variable? arithmetic on columns; apply with a function or a lambda

All the code is in the notebook class8/class8_pandas.ipynb:

  • Part 1 (guided): the four operations, on the forest fires dataset of Class 7.
  • Part 2 (on your own): cleaning and analysing official statistics on rural fires in Portugal (INE). This was Part 3 of Class 7: if you have already done some of the exercises, continue from where you stopped.

The notebook is set up as in Class 7 (see Setup in Class 7), in a folder class8:

$ mkdir class8
$ cd class8
$ wget https://raw.githubusercontent.com/isa-ulisboa/greends-ipython/main/class8/class8_pandas.ipynb
$ code class8_pandas.ipynb
    Part 1 — Select, sort, group, create

    1.1 Selecting rows. A condition on a column gives a mask (a column of True/False), and fires[mask] keeps the rows where the mask is True. Conditions are combined with & (and), | (or), ~ (not), and each condition needs its own parentheses. The Python words and and or do not work on whole columns (ValueError: The truth value of a Series is ambiguous).

    fires[(fires['temp'] > 25) & (fires['RH'] < 30)]
    fires[fires['temp'].between(20, 22)]                  # 20 <= temp <= 22
    fires[fires['month'].isin(['jun', 'jul', 'aug', 'sep'])]
    fires[~fires['month'].isin(['jun', 'jul', 'aug', 'sep'])]
    

    The methods of Python strings are applied to a whole text column through .str, for instance .str.upper(), .str.startswith('j'), .str.contains('Vila'), .str.replace('&', ''), .str.len() or .str[:2] (first two characters). fires.loc[mask, ['month', 'area']] selects rows and columns at the same time, and mask.sum() counts the rows.

    • Exercise 1.1: How many fires occurred in summer with wind above 5 km/h and no rain?

    1.2 Sorting. sort_values returns a new, sorted table. With a list of columns, rows are sorted by the first column, then by the second one for rows with the same value in the first column, and so on; ascending can be a list too:

    fires.sort_values('area', ascending=False)
    fires.sort_values(['month', 'area'], ascending=[True, False])   # within each month, largest fires first
    fires.nlargest(5, 'area')
    
    • Exercise 1.2: Sort the fires by day and then by temp (highest first), and show the 3 hottest fires of each weekday with .groupby('day').head(3).

    1.3 Group and aggregate. The pattern is table.groupby(KEY)[COLUMN].FUNCTION(). .agg computes several summaries at once, and named aggregation (new_name=(column, function)) chooses the names of the result. Operations can be chained: select, then group, then sort.

    fires.groupby('month')['area'].agg(['size', 'sum', 'mean', 'max'])
    fires.groupby('month').agg(n_fires=('area', 'size'), mean_temp=('temp', 'mean'), total_area=('area', 'sum'))
    fires[fires['area'] > 0].groupby('month')['area'].median().sort_values(ascending=False)
    fires.groupby(['month', 'day']).size().reset_index(name='n_fires')            # two keys
    
    • Exercise 1.3: For each weekday, compute the number of fires, the mean temperature and the total burned area, sorted by total burned area.

    1.4 New variables: apply and lambda. When the new variable is a formula, use arithmetic on columns (fires['area'] / 100). When the rule needs an if/else, or a function of your own, .apply(f) calls the function f on each value of the column. A lambda is a function without a name, written in one line: lambda a: expression is the same as def f(a): return expression. With axis=1, the function is applied to each row, and can use several columns.

    def size_class(a):
        if a == 0:
            return 'none'
        elif a < 10:
            return 'small'
        else:
            return 'large'
    
    fires['size_class'] = fires['area'].apply(size_class)                          # a function, without ()
    fires['burned'] = fires['area'].apply(lambda a: 'yes' if a > 0 else 'no')      # a lambda
    fires['season'] = fires['month'].apply(lambda m: season[m])                    # season is a dictionary
    fires['hot_dry'] = fires.apply(lambda row: row['temp'] > 25 and row['RH'] < 30, axis=1)   # each row
    fires.groupby('size_class')['temp'].mean()                                     # group by the new variable
    

    Rule of thumb: use arithmetic and masks when possible (they are much faster), and apply when you need a function of your own.

    • Exercise 1.4: Create a column wind_class (calm, moderate, strong) with apply, and compute the mean burned area for each class. Create a column weekend with apply and a lambda, and compare the proportion of fires that burned something on weekends and on weekdays.
    Part 2 — On your own: rural fires in Portugal (INE)

    The file datasets/rural_fires.csv was exported from INE, the Portuguese national statistics institute, and contains the number of rural fires in 2023 by geographic unit and by cause of fire. The geographic units follow the NUTS classification (version 2024), with hierarchical codes:

    code level examples
    PT country Portugal
    1 character NUTS I 1 Continente, 2 Açores, 3 Madeira
    2 characters NUTS II 11 Norte, 19 Centro, 1A Grande Lisboa, 1B Península de Setúbal, 1C Alentejo, 1D Oeste e Vale do Tejo, 15 Algarve, 20, 30
    3 characters NUTS III 111 Alto Minho, 112 Cávado, …
    7 characters municipality 1111601 Arcos de Valdevez

    The code of a municipality starts with the codes of its NUTS II (2 characters) and NUTS III (3 characters) regions. As with most official statistics, the file was prepared for people and not for computing:

    • the first 11 lines contain titles, and there are notes at the end of the file;
    • the separator is ;;
    • each number is followed by ` &, so pandas reads numbers as text (dtype is object, or str` in pandas 3);
    • the first column combines a code and a name (1111601: Arcos de Valdevez);
    • the rows mix different units (country, NUTS I, II, III regions, and municipalities);
    • the table is wide: the cause of the fire is spread over six columns.

    The notebook first shows the raw text of the file, and then reads only the data rows into a dataframe rf with pd.read_csv(..., sep=';', skiprows=11, nrows=347, header=None, usecols=range(8), names=[...]). The exercises follow a typical workflow: clean, then select, sort, create and group (Part 1), and finally report.

    Cleaning

    • 2.1 Convert the numeric columns to integers, either with string methods on whole columns (.str.replace, .str.strip, .astype(int)), or with apply and a lambda that converts one value.
    • 2.2 Split the first column into code and name with .str.split(': ', n=1, expand=True).

    Selecting rows

    • 2.3 Keep the municipalities (codes with 7 characters) in a dataframe mun, and add columns nuts2 and nuts3 with the first characters of the code.
    • 2.4 With masks: the number of fires in a list of cities (.isin), in the municipalities whose name starts with Vila, the municipalities without fires, and the municipalities of the Alentejo with more than 20 fires.

    Sorting

    • 2.5 The 10 municipalities with the most fires, and the 3 municipalities with the most fires in each NUTS II region (sort by two columns).

    Validating and creating new variables

    • 2.6 Validate the data: does the sum over municipalities equal the total for Portugal? Is the total of each municipality equal to the sum of the six causes?
    • 2.7 The percentage of fires due to negligence in each municipality.
    • 2.8 With apply: a class of municipality by number of fires (none, few, some, many), and the main cause of fire in each municipality (apply on rows with axis=1).

    Grouping

    • 2.9 For each NUTS II region: number of municipalities, total, mean and maximum number of fires; the region names with merge; and the number of municipalities by region and main cause (two keys).

    Challenge

    • 2.10 Convert the table into a tidy table with columns code, name, nuts2, cause, n_fires with .melt().
    • 2.11 Compute the percentage of each cause in each region, and present the result as a wide table (regions × causes).
    • 2.12 Export the tidy table with .to_csv('rural_fires_2023_tidy.csv', index=False) (see Reference: exporting to file in Class 7).
---

Class 9 (November 13, 2026): objects and classes; attributes and methods

  • CS50P, Lecture 8: Object-Oriented Programming. For this class, the first part of the lecture (classes, attributes, methods, raise, __str__) is enough.
  • CS50P. In particular, check the shorts on classes, methods and variables, in this order: Classes, Instance Variables, Instance Methods, and Class Methods and Class Variables.

The goal of this class is to understand what an object is, and to write your first classes. As an example, we model a plant nursery: plants grow in trays, all plants of a tray are of the same species, and plants can be added to or removed from a tray. We want to keep track of the trays and of the number of plants of each species.

  • Part 1 (expository): you have been using objects for weeks.
  • Part 2 (guided): from a dictionary to a class: __init__, self, attributes.
  • Part 3 (guided): methods with rules, __str__, objects that contain objects.
  • Part 4 (on your own): a nursery inventory.

Create a folder class9 in the CS50 codespace and write the code of Parts 2 and 3 in a script, for instance nursery.py, and run it with python nursery.py. Short tests can also be done in the Python REPL (type python in the terminal).

    Part 1 — You have been using objects

    1.1 Data and actions. Every value in Python is an object, and every object belongs to a class (its type). An object bundles data (its attributes) and actions on that data (its methods):

    s = "Quercus suber"
    type(s)          # <class 'str'>: s is an object (an instance) of class str
    s.upper()        # method: an action, with parentheses
    s.split()        # another method of class str
    
    values = [12, 3, 7]
    type(values)     # <class 'list'>
    values.append(5) # method: changes the list
    values.sort()
    

    In Class 7 we used both kinds with pandas: fires.shape and fires.columns are attributes (data, no parentheses), while fires.head() and fires.groupby('month') are methods (actions, with parentheses). The same happened with Pillow in Class 6 (img.size and img.rotate(90)) and with requests in Class 5 (response.status_code and response.json()).

    The function dir(s) lists all attributes and methods of an object.

    • Exercise 1.1: Create two strings a = "olive" and b = "cork oak". Do a.upper() and b.upper() run the same code? Do they return the same result? Why?

    The answer to exercise 1.1 is the main idea of this class: all objects of a class share the same methods (the class is the blueprint), but each object has its own data. Today we write our own classes.

    Part 2 — From a dictionary to a class

    2.1 A tray as a dictionary. With what we know from Classes 2 and 3, a tray could be a dictionary, and actions on trays could be functions that receive the dictionary as their first argument:

    def new_tray(species, n_plants):
        return {"species": species, "n_plants": n_plants}
    
    def add_plants(tray, n):
        tray["n_plants"] += n
    
    tray1 = new_tray("Pansy", 26)
    add_plants(tray1, 10)
    print(tray1["n_plants"])
    

    This works, but the data and the functions are separate: nothing says that add_plants is meant for trays, and any dictionary (or anything else) can be passed to it.

    2.2 The same tray as a class. A class puts the data and the functions together:

    class Tray:
        def __init__(self, species, n_plants):
            self.species = species
            self.n_plants = n_plants
    
        def add_plants(self, n):
            self.n_plants += n
    
    tray1 = Tray("Pansy", 26)
    tray1.add_plants(10)
    print(tray1.n_plants)
    

    Compare the two versions line by line:

    dictionary + functions class
    def new_tray(species, n_plants): def __init__(self, species, n_plants):
    tray1 = new_tray("Pansy", 26) tray1 = Tray("Pansy", 26)
    tray1["n_plants"] tray1.n_plants (an attribute)
    def add_plants(tray, n): def add_plants(self, n): (a method)
    add_plants(tray1, 10) tray1.add_plants(10)
    • Tray("Pansy", 26) creates a new, empty object of class Tray, calls __init__ to fill in its attributes, and returns the object. The object is called an instance of the class.
    • self is the object that the method works on, exactly like the argument tray in the dictionary version. When you write tray1.add_plants(10), Python runs Tray.add_plants(tray1, 10): try both and check that they do the same thing.
    • By convention, class names use CamelCase (Tray, PlantNursery), while variables, functions and methods use snake_case (n_plants, add_plants).

    2.3 Each instance has its own data.

    tray1 = Tray("Pansy", 26)
    tray2 = Tray("Petunia", 28)
    tray2.add_plants(5)
    print(tray1.n_plants, tray2.n_plants)
    
    • Exercise 2.3: Before running the code, predict what it prints. Then change __init__ so that n_plants is optional, with default value 0 (as for any function), and create a tray with Tray("Begonia").

    2.4 The most common errors. Each line marked # bug below is wrong. Run the code, read the error message, and fix it:

    class Tray:
        def __init__(self, species, n_plants):
            self.species = species
            self.n_plants = n_plants
    
        def add_plants(n):            # bug 1
            self.n_plants += n
    
        def remove_plants(self, n):
            n_plants -= n             # bug 2
    
    tray1 = Tray("Pansy", 26)
    tray1.add_plants(10)
    tray1.remove_plants(5)
    tray2 = Tray                      # bug 3
    tray2.remove_plants(5)
    
    Part 3 — Methods with rules, __str__, objects that contain objects

    3.1 Methods keep the object valid. A tray has room for at most 50 plants, and one cannot remove more plants than there are in the tray. If all changes to a tray go through its methods, the methods can enforce these rules, and raise an exception (Class 4) when a rule is broken:

    class Tray:
        MAX_PLANTS = 50   # class attribute: shared by all trays
    
        def __init__(self, species, n_plants=0):
            if n_plants < 0 or n_plants > Tray.MAX_PLANTS:
                raise ValueError(f"n_plants must be between 0 and {Tray.MAX_PLANTS}")
            self.species = species
            self.n_plants = n_plants
    
        def add_plants(self, n):
            if self.n_plants + n > Tray.MAX_PLANTS:
                raise ValueError(f"there is only room for {Tray.MAX_PLANTS - self.n_plants} more plants")
            self.n_plants += n
    
        def remove_plants(self, n):
            if n > self.n_plants:
                raise ValueError(f"the tray only has {self.n_plants} plants")
            self.n_plants -= n
    try:
        tray = Tray('Pansy', n_plants=60)
    except ValueError as e:
        print(e)
    else:
        print(tray.n_plants)   # runs only if the tray was created
    
    

    MAX_PLANTS is a class attribute: it is defined once, in the class, and is the same for all trays (Tray.MAX_PLANTS). self.species and self.n_plants are instance attributes: each tray has its own values.

    • Exercise 3.1: Create a tray with 40 plants and try to add 20 plants. Then catch the error with try/except ValueError and print the message of the exception.

    Note that Python does not prevent tray1.n_plants = -5: methods only protect the object if we use them. We will see in Class 11 how to prevent it with properties.

    3.2 Printing an object: __str__. Try print(tray1): the result looks like <__main__.Tray object at 0x7f...>, which is not very informative. When an object is printed, or converted with str(), or used in an f-string, Python calls its method __str__, which should return a string. Add this method to class Tray:

        def __str__(self):
            return f"{self.n_plants} {self.species} plants"
    

    Methods whose names start and end with double underscores, like __init__ and __str__, are called special methods: we do not call them directly, Python calls them for us (Tray(...) calls __init__, print(...) calls __str__).

    • Exercise 3.2: Change __str__ so that it also shows how many plants can still be added to the tray.

    3.3 Objects that contain objects. The nursery is also an object: it has a name and contains trays. Its attribute trays is a dictionary whose values are Tray objects, with the tray names as keys:

    class Inventory:
        def __init__(self, name):
            self.name = name
            self.trays = {}   # tray name -> Tray object
    
        def add_tray(self, tray_name, tray):
            self.trays[tray_name] = tray
    
        def get_tray(self, tray_name):
            return self.trays[tray_name]
    
        def __str__(self):
            return f"{self.name}: {len(self.trays)} trays"
    
    nursery = Inventory("ISA nursery")
    nursery.add_tray("A1", Tray("Pansy", 25))
    nursery.add_tray("A2", Tray("Petunia", 10))
    nursery.get_tray("A2").add_plants(10)
    print(nursery)
    print(nursery.get_tray("A2"))
    

    get_tray returns the tray object itself, not a copy, so nursery.get_tray("A2").add_plants(10) changes the tray that is in the inventory.

    3.4 Puzzle: a shared dictionary. In this version, the trays can be given when the inventory is created, with an empty dictionary as the default value:

    class Inventory:
        def __init__(self, name, trays={}):
            self.name = name
            self.trays = trays
    
        def add_tray(self, tray_name, tray):
            self.trays[tray_name] = tray
    
    north = Inventory("North")
    south = Inventory("South")
    north.add_tray("A1", Tray("Pansy", 25))
    print(len(south.trays))
    
    • Exercise 3.4: Predict what is printed, and run the code. Why?

    The default value {} is created only once, when def is executed, and not each time the function is called: all inventories created without the argument trays share the same dictionary. Rule: never use a list or a dictionary as a default value. Create it inside __init__ (as in 3.3), or use trays=None and then if trays is None: trays = {}.

    3.5 Looking ahead. A class can be built from another class: for instance, class MultipleUseTray(Tray): would inherit all attributes and methods of Tray, and only add or change what is different. This is called inheritance, and it is one of the main concepts of OOP that we will see in Class 11.

    Part 4 — On your own: a nursery inventory

    Start from class Tray (3.1, with __str__ from 3.2) and class Inventory (3.3) in the script nursery.py. Add the following methods to class Inventory:

    • 4.1 remove_tray(tray_name): removes a tray, and raises ValueError if there is no tray with that name.
    • 4.2 list_all_trays(): prints one line per tray, for instance A1: 25 Pansy plants.
    • 4.3 n_plants(): returns the total number of plants in the nursery.
    • 4.4 plants_by_species(): returns a dictionary with the number of plants of each species, for instance {"Pansy": 57, "Petunia": 10}.
    • 4.5 empty_trays(): returns a list with the names of the empty trays.
    • 4.6 total_value(prices): returns the value of all plants in the nursery, where prices is a dictionary with the price of each species.


    Some more challenging exercises:

    • 4.7 sell(species, n): removes n plants of the given species, taking them from the trays in order (first empty the first tray of that species, then the next one, and so on). If there are not enough plants, raise ValueError without changing any tray.
    • 4.8 load_csv(path): adds trays read from a CSV file with columns tray,species,n_plants (create a small file yourself; see Class 6 for reading files).
    • 4.9 to_dataframe(): returns a pandas DataFrame with one row per tray and columns tray, species, n_plants (see Class 7). Use it to sort the trays by number of plants.
---

Class 10 (November 20, 2026): field trip (no class)

There is no Python class this week: the masters students are on a field trip.

---

Class 11 (November 27, 2026): classes in practice: class methods, properties, when to use classes

This class revises and practises the basics of Class 9, adds a few tools that make classes safer and more convenient (class methods, properties, special methods), and discusses when classes are a good choice. We keep the plant nursery example. At the end of the class, you should be ready to solve the Cookie jar problem on your own.

  • Part 1 (guided): revision, and class methods.
  • Part 2 (guided): the leading underscore, and properties.
  • Part 3 (expository): when to use a class.
  • Part 4 (guided): inheritance, used sparingly, and composition.
  • Part 5 (on your own): exercises, and the Cookie jar assignment.

Create a folder class11 in the CS50 codespace, and start from the script nursery.py described in Setup below.

    Setup: the starting script nursery.py

    Your own nursery.py changed many times during the exercises of Class 9. To start Class 11 from the same point, download the script class11/nursery.py, which contains class Tray of Class 9 (3.1 and 3.2). In this class we only work with trays: class Inventory of Class 9 is not needed.

    $ mkdir class11
    $ cd class11
    $ wget https://raw.githubusercontent.com/isa-ulisboa/greends-ipython/main/class11/nursery.py
    $ code nursery.py
    

    The script only defines the class. To try it, add code at the end of the script, for instance:

    tray1 = Tray("Pansy", 25)
    tray2 = Tray("Petunia", 10)
    tray2.add_plants(15)
    print(tray1)
    print(tray2)
    

    and run it with python nursery.py. All the changes of Class 11 are made in this script.

    Part 1 — Revision, and class methods

    1.1 Revision. Watch the four CS50P shorts listed above, and find each idea in the class Tray of Class 9:

    short in class Tray
    Classes class Tray: is the blueprint; Tray("Pansy", 26) creates an instance
    Instance Variables self.species, self.n_plants: each tray has its own values
    Instance Methods add_plants(self, n), remove_plants(self, n), __str__(self): they receive the instance as self
    Class Methods and Class Variables MAX_PLANTS: one value shared by all trays (see 1.2). Class methods: see 1.3 and 1.4

    1.2. Class variable: set a variable to count the number of instances created in a class

    • Exercise 1.2: Add a class variable n_trays to class Tray, that counts how many trays have been created: it starts at 0, and __init__ increases it with Tray.n_trays += 1. Create a few trays, and print Tray.n_trays. Should a tray that could not be created (for instance Tray("Pansy", 60)) be counted? Where should the line Tray.n_trays += 1 be in __init__?

    1.3 Class methods. All the methods we have written so far are instance methods: they work on one object, which they receive as self, and they are called on that object (tray1.add_plants(10)). Sometimes a method is about the class as a whole, not about one tray in particular. A class method receives the class itself as its first argument, called cls by convention, and is called on the class (Tray.method()), without the need for an existing tray. It is marked with @classmethod on the line before def (a decorator: it changes how the method is called).

      instance method class method
    first argument self: one object cls: the class
    called on an object: tray1.add_plants(10) the class: Tray.from_csv_row("Pansy,25")
    typical use read or change the data of one object work with class variables; create objects

    A first example uses the class variable n_trays of 1.2:

    class Tray:
        MAX_PLANTS = 50
        n_trays = 0
        ...
        @classmethod
        def report(cls):
            print(f"{cls.n_trays} trays created, at most {cls.MAX_PLANTS} plants each")
    
    Tray.report()        # called on the class: no tray is needed
    

    Inside a class method, cls is the class Tray: cls.n_trays is the same as Tray.n_trays.

    1.4 Class methods to create objects. The most common use of class methods is to offer another way to create objects. For instance, the data of a nursery is often in a file, with one tray per line, such as Petunia,30 (species, number of plants). Without a class method, the line has to be split each time a tray is created:

    row = "Petunia,30"
    species, n_plants = row.split(",")
    tray = Tray(species.strip(), int(n_plants))
    

    Since this code is about creating trays, it belongs in class Tray. A class method can do it, and return the new tray:

    class Tray:
        ...
        @classmethod
        def from_csv_row(cls, row):
            species, n_plants = row.split(",")
            return cls(species.strip(), int(n_plants))   # cls(...) is Tray(...)
    
    tray = Tray.from_csv_row("Petunia,30")
    print(tray)
    

    cls(...) does the same as Tray(...): it creates a tray, calling __init__. This is the same idea as Student.get() in the CS50P notes.

    • Exercise 1.4: Add report and from_csv_row to class Tray, create a few trays with from_csv_row, and call Tray.report(). Why could from_csv_row not be an instance method? (Hint: which tray would self be?)
    Part 2 — Attributes and properties

    2.1 The problem. The methods of Tray check the rules of a tray (between 0 and 50 plants), but in Class 9 we noticed that nothing prevents:

    tray1 = Tray("Pansy", 20)
    tray1.n_plants = -5        # no error: the tray is now invalid
    

    The methods only protect the tray if we use them. We would like to tell whoever uses class Tray: you can read the number of plants, but only change it with add_plants and remove_plants.\

    2.2 The leading underscore. In Python, an attribute whose name starts with an underscore, such as _n_plants, is internal: by convention, it is only used by the methods of the class, and not by the code outside the class. In nursery.py, rename the attribute n_plants to _n_plants (only the attribute self.n_plants, not the argument n_plants of __init__):

    class Tray:
        MAX_PLANTS = 50
    
        def __init__(self, species, n_plants=0):
            if n_plants < 0 or n_plants > Tray.MAX_PLANTS:
                raise ValueError(f"n_plants must be between 0 and {Tray.MAX_PLANTS}")
            self.species = species
            self._n_plants = n_plants       # internal: only changed by the methods below
    
        def add_plants(self, n):
            if self._n_plants + n > Tray.MAX_PLANTS:
                raise ValueError(f"there is only room for {Tray.MAX_PLANTS - self._n_plants} more plants")
            self._n_plants += n
    
        def remove_plants(self, n):
            if n > self._n_plants:
                raise ValueError(f"the tray only has {self._n_plants} plants")
            self._n_plants -= n
    
        def __str__(self):
            return f"{self._n_plants} {self.species} plants"
    
    

    In this case, the argument n_plants of __init__ was not changed. Note that the underscore is only a convention, a message to other programmers: Python does not prevent tray1._n_plants = -5. But whoever writes it knows that they are using the class in a way it was not meant to be used.

    • Exercise 2.2: Create a tray, and print vars(tray), which shows the attributes of the tray. Then try print(tray.n_plants) and tray._n_plants = -5. Which line raises an error, and why?

    \

    2.3 A property. Now the number of plants is internal, but it is still useful to read it from outside the class, for instance to count the plants of the nursery. A property is a method that is used as if it were an attribute: it is marked with @property on the line before def (a decorator, like @classmethod), and it is read without parentheses. Add to class Tray:

        @property
        def n_plants(self):
            return self._n_plants
    

    and try:

    tray1 = Tray("Pansy", 20)
    print(tray1.n_plants)      # 20: runs the method n_plants, without parentheses
    tray1.add_plants(10)
    print(tray1.n_plants)      # 30
    tray1.n_plants = -5        # AttributeError: the property can be read, but not assigned
    

    The problem of 2.1 is solved: tray1.n_plants is read as before, but the number of plants can only be changed with add_plants and remove_plants, which check the rules. A property like this one is read-only. (Python can also define properties that can be assigned, with a setter, as house in the CS50P notes. We do not need them in this course.)\

    2.4 Trays of different sizes. In Class 9, all trays had at most MAX_PLANTS = 50 plants. Real trays come in a few standard sizes, for instance 24, 50 or 104 cells. The capacity is now different for each tray, so it becomes an instance variable, while the list of valid sizes is shared by all trays (a class variable). The capacity of a tray does not change once the tray is created, so it becomes a read-only property, like n_plants. The number of free cells is computed from the capacity and the number of plants: it becomes a property too. Replace class Tray by the following, and add from_csv_row of 1.4 at the end:

    class Tray:
        CAPACITIES = (24, 50, 104)          # class variable: standard tray sizes
    
        def __init__(self, species, n_plants=0, capacity=50):
            if capacity not in Tray.CAPACITIES:
                raise ValueError(f"capacity must be one of {Tray.CAPACITIES}")
            if n_plants < 0:
                raise ValueError("n_plants cannot be negative")
            if n_plants > capacity:
                raise ValueError(f"the tray only has {capacity} cells")
            self.species = species
            self._capacity = capacity
            self._n_plants = n_plants
    
        @property
        def capacity(self):                 # read-only
            return self._capacity
    
        @property
        def n_plants(self):                 # read-only
            return self._n_plants
    
        @property
        def free_cells(self):               # computed from the other attributes
            return self._capacity - self._n_plants
    
        def add_plants(self, n):
            if n > self.free_cells:
                raise ValueError(f"the tray only has {self.capacity} cells, and {self.free_cells} are free")
            self._n_plants += n
    
        def remove_plants(self, n):
            if n > self._n_plants:
                raise ValueError(f"the tray only has {self._n_plants} plants")
            self._n_plants -= n
    
        def __str__(self):
            return f"{self.n_plants} {self.species} plants ({self.free_cells} free cells)"
    
    • capacity and n_plants are read-only properties: their values are stored in the internal attributes _capacity and _n_plants, and only the methods of the class change them.
    • free_cells has no attribute of its own: it is computed each time it is read, so it is always up to date. Without a property, we would need an attribute free_cells, and we would have to update it in __init__, add_plants and remove_plants, and never forget to.
    • Inside the class, the methods can use the internal attributes (self._n_plants) or the properties (self.free_cells).

    2.5 Attribute or property? Outside the class, both are written in the same way, tray.species or tray.free_cells, without parentheses. The difference is in what happens behind:

      attribute property
    defined in __init__: self.species = species as a method, with @property
    the value is stored in the object is returned by the method, each time it is read
    read tray.species tray.free_cells (no parentheses)
    assign tray.species = "Begonia" works tray.free_cells = 0 raises AttributeError
    use it for data that can change freely data that should not be changed directly (capacity, n_plants), and values computed from other attributes (free_cells)

    A property is also an alternative to a method without arguments: free_cells could be a method, called with tray.free_cells(). A property is preferable for a simple value of the object, which takes no time to compute; a method is preferable when the object does something (add_plants), or when there are arguments.

    • Exercise 2.5: Check that Tray("Pansy", 30, capacity=24), Tray("Pansy", capacity=30), tray.add_plants(100), tray.n_plants = 10, tray.capacity = 104 and tray.free_cells = 0 all raise an exception. Which exception, in each case?
    • Exercise 2.6: Change Tray.from_csv_row (1.4) so that it also reads the capacity, for instance Petunia,24,24. Then create a list of trays from the list rows = ["Pansy,25,50", "Petunia,24,24", "Begonia,0,104"] with a list comprehension, and print each tray.
    • Exercise 2.7: Add a property is_full, that is True when the tray has no free cells. Then discuss: should species stay an attribute, or become a read-only property?
    Part 3 — When to use a class

    Classes are a tool, not a goal. A class is a good choice when the program has several objects of the same kind, each with its own data, and rules about how that data changes. The trays are a good example: a nursery has many trays, each tray has its own species and number of plants, and the methods keep every tray valid.

    Ask these questions before writing a class:

    1. Will there be several instances, each with its own data?
    2. Does the data change over time, and must it stay valid?
    3. Are there several operations on the same data?

    If the answer to all of them is no, a function (or a dictionary, for data without behaviour) is usually simpler. Two common signs of an unnecessary class:

    # a class with no data: these are just functions
    class Converter:
        def kg_to_tons(self, kg):
            return kg / 1000
        def ha_to_m2(self, ha):
            return ha * 10000
    
    c = Converter()
    c.kg_to_tons(2500)
    
    # a class with a single method, called once after creating the object
    class GerminationRate:
        def __init__(self, n_seeds, n_germinated):
            self.n_seeds = n_seeds
            self.n_germinated = n_germinated
        def compute(self):
            return self.n_germinated / self.n_seeds
    
    GerminationRate(100, 87).compute()
    

    Both are simpler as functions: kg_to_tons(2500) and germination_rate(100, 87). Functions that belong together can be grouped in a module (Class 5), for instance units.py, and imported with import units.

    • Exercise 3.1: Rewrite both examples as functions, in a module units.py, and use them from another script.
    • Exercise 3.2: For each case, would you write a class or a function? Why? (a) convert a temperature from °F to °C; (b) a greenhouse whose sensors store their readings, and raise an alarm when the temperature is too high; (c) the area of a field from its length and width; (d) a seed bank where seed lots are added, withdrawn, and expire.
    Part 4 — Inheritance, used sparingly, and composition

    4.1 You have been using inheritance. The exceptions of Python form a hierarchy: ValueError and KeyError are kinds of Exception. A new class can be built from an existing one: it inherits all its attributes and methods. A new exception for the nursery takes two lines:

    class CapacityError(ValueError):
        """Raised when a tray does not have room for the plants."""
    

    CapacityError is a child (subclass) of the parent ValueError. Raise it instead of ValueError when the plants do not fit in the tray, in __init__ and in add_plants. A CapacityError is a ValueError, so except ValueError still catches it, and except CapacityError catches only this error.

    • Exercise 4.1: Add CapacityError to nursery.py, and check what isinstance(e, ValueError) and isinstance(e, CapacityError) return for the exceptions raised by tray.add_plants(100) and by tray.remove_plants(100).

    4.2 A child class with super(). Seeds are sown in trays too, but only some of them germinate. A SeedTray is a Tray with an extra attribute and an extra method:

    class SeedTray(Tray):
        def __init__(self, species, n_seeds=0, capacity=50, germination_rate=0.8):
            super().__init__(species, n_seeds, capacity)   # runs __init__ of Tray
            self.germination_rate = germination_rate
    
        def expected_plants(self):
            return round(self.n_plants * self.germination_rate)
    
        def __str__(self):                                  # overrides __str__ of Tray
            return f"{super().__str__()}, {self.expected_plants()} expected to germinate"
    

    super() gives access to the methods of the parent: the child only adds or changes what is different, and the rules of Tray (capacity, number of plants) still apply. A method defined again in the child overrides the method of the parent.

    4.3 Do not abuse inheritance. In Class 9 (3.5) we suggested class MultipleUseTray(Tray), for plastic trays that can be reused. It is tempting to go on:

    Tray
    ├── PlasticTray
    │   ├── ReusablePlasticTray
    │   │   └── ReusablePlasticTrayWithLid
    │   └── SingleUsePlasticTray
    └── BiodegradableTray
    

    Each new feature (material, reuse, lid) multiplies the number of classes, and a change in a class affects all classes below it. When trays differ only in their data, a single class with more attributes is simpler: Tray("Pansy", 25, material="plastic", max_uses=5). Rules of thumb:

    • the objects differ only in data → attributes of a single class;
    • is a, with different behaviour → inheritance, with few levels (a SeedTray is a Tray that can predict germination);
    • has a → composition: an object stores other objects as attributes.

    4.4 Composition. You already used composition in Class 9: an Inventory is not a kind of tray, but it has trays, stored in its attribute trays. Here is a second example. In a greenhouse, trays are placed on benches. A bench is not a kind of tray, so class Bench(Tray) would make no sense: a bench has trays. In composition, an object stores other objects in its attributes, and its methods do their work by calling the methods of those objects:

    class Bench:
        def __init__(self, name):
            self.name = name
            self.trays = []                    # a bench has trays: composition
    
        def add_tray(self, tray):
            self.trays.append(tray)
    
        def total_plants(self):
            return sum(tray.n_plants for tray in self.trays)   # asks each tray
    
        def __str__(self):
            return f"bench {self.name}: {len(self.trays)} trays, {self.total_plants()} plants"
    
    
    bench = Bench("A1")
    bench.add_tray(Tray("Pansy", 25))
    bench.add_tray(Tray("Petunia", 10, capacity=24))
    print(bench)                               # bench A1: 2 trays, 35 plants
    

    Bench does not need to know how a tray stores its plants or checks its rules: it only uses what the trays offer (here, n_plants). Unlike Inventory, Bench keeps its trays in a list instead of a dictionary, since it does not need to find a tray by its name. Compare:

      inheritance composition
    relation a SeedTray is a Tray a Bench has trays
    in the code class SeedTray(Tray): self.trays = [] in __init__ of Bench
    the new class gets all the attributes and methods of the parent uses the methods of the objects it stores

    Composition is also the answer to the hierarchy of 4.3: features that can be combined freely are attributes (or objects stored in attributes), not new classes.

    • Exercise 4.4: Add class Bench to nursery.py, and a method trays_of(species), that returns the list of the trays of that species on the bench.


    4.5 Polymorphism. A bench can hold different kinds of trays:

    bench = Bench("B2")
    bench.add_tray(Tray("Pansy", 25))
    bench.add_tray(SeedTray("Basil", 50, germination_rate=0.7))
    bench.add_tray(Tray("Petunia", 10, capacity=24))
    for tray in bench.trays:
        print(tray)                     # each tray uses the __str__ of its own class
    print(bench.total_plants())         # every tray has n_plants
    

    The same call works on different kinds of objects, and each object responds in its own way: this is called polymorphism. Bench does not need to check which kind of tray each one is.

    • Exercise 4.5: Predict what the loop prints. Then add to Bench a method expected_plants(), that returns sum(tray.expected_plants() for tray in self.trays), and call it for bench B2. Why does it fail? Add a method expected_plants() to class Tray, that returns n_plants, so that the same call works for all trays.
    Part 5 — On your own

    Nursery

    • 5.1 __add__. In the CS50P notes, Vault.__add__ defines what + does for two vaults: a + b calls a.__add__(b). Define __add__ in class Tray, so that tray1 + tray2 returns a new tray with the plants of both trays, and the capacity of the larger one. Raise ValueError if the species are different. The original trays do not change. What happens when the plants do not fit in the new tray?
      merged = Tray("Pansy", 20, capacity=24) + Tray("Pansy", 30)
      print(merged)                    # 50 Pansy plants (0 free cells)
      Tray("Pansy") + Tray("Petunia")  # ValueError
      
    • 5.2 Write a function (not a class: see Part 3) load_trays(path), that reads a file with lines like Pansy,25,50 (species, number of plants, capacity), uses Tray.from_csv_row to create a tray for each line, and returns the list of trays. Create a small file yourself to test it (see Class 6 for reading files).
    • 5.3 Write the functions free_cells(trays), that returns the total number of free cells in a list of trays, and full_trays(trays), that returns the list of the trays with no free cells. Use them with the list of trays of 5.2.
    • 5.4 Add the attributes material and max_uses (default None, meaning no limit) to class Tray, and an attribute n_uses that counts how many times the tray was filled with add_plants. When a tray has been filled max_uses times, add_plants should raise ValueError (see 4.3).

    Assignment

    Jar Tray
    Jar(capacity=12), ValueError if the capacity is not valid Tray(species, n_plants, capacity) (2.4)
    jar.capacity, read-only tray.capacity (2.4)
    jar.size: number of cookies, read-only tray.n_plants (2.3)
    deposit(n), withdraw(n), ValueError if impossible add_plants(n), remove_plants(n)
    str(jar): one 🍪 per cookie __str__

    In Class 12, you will write tests for your class Jar in test_jar.py.

---

Class 12 (December 4, 2026): unit tests with pytest; packing and unpacking, lambda, map, filter

Until now, we checked our code by running it and looking at what it prints. This becomes tedious, and error prone, as the code grows: each change can break something that worked before. In the first half of this class, we write unit tests: small functions that check automatically that other functions and classes work as expected. We only need three tools: assert, pytest.approx (to compare floats with a tolerance), and with pytest.raises(...) (to check that the right error is raised). This is enough to write the tests of the Cookie jar assignment. In the second half, we see a few tools of Python that are not specific to classes: the packing and unpacking operators * and **, functions with any number of arguments (*args and **kwargs), and the functions lambda, map and filter, with the tree inventory of a park as example.

  • Part 1 (guided): assert, test functions, tolerance, and errors.
  • Part 2 (on your own): tests for the cookie jar.
  • Part 3 (guided): packing and unpacking, *args and **kwargs.
  • Part 4 (guided): lambda, map, filter, and key functions.
  • Part 5 (on your own): exercises.

Create a folder class12 in the CS50 codespace, and start from the script nursery.py described in Setup below.

    Setup: the starting script nursery.py

    Your own nursery.py changed many times during the exercises of Class 11. To start Class 12 from the same point, download the script class12/nursery.py, which contains class Tray of Class 11 (2.4): trays of different capacities, with the read-only properties capacity, n_plants and free_cells.

    $ mkdir class12
    $ cd class12
    $ wget https://raw.githubusercontent.com/isa-ulisboa/greends-ipython/main/class12/nursery.py
    $ code nursery.py
    

    The script only defines the class, and does not run any code: this is important, since nursery.py will be imported by the tests (see 1.6 for how to add code that runs only with python nursery.py). The tests are written in a second script, test_nursery.py, in the same folder. nursery.py only changes in the exercises where you break a method on purpose, to see a test fail.

    Check that pytest is installed with pytest --version, and otherwise install it with pip install pytest.

    Part 1 — Unit tests with pytest

    1.1 assert. The statement assert condition does nothing if the condition is True, and raises an AssertionError if it is False:

    from nursery import Tray
    
    tray = Tray("Pansy", 25)
    tray.add_plants(10)
    assert tray.n_plants == 35      # nothing happens: the method works
    assert tray.n_plants == 30      # AssertionError
    

    In a script, the first AssertionError stops the script, and the remaining checks are not run.

    1.2 Test functions. With pytest, the checks are written in test functions, whose names start with test_, in a separate file whose name starts with test_. pytest finds and runs all the test functions, and reports which ones pass and which ones fail, instead of stopping at the first error. Create the file test_nursery.py in the folder class12:

    from nursery import Tray
    
    
    def test_add_plants():
        tray = Tray("Pansy", 20)
        tray.add_plants(10)
        assert tray.n_plants == 30
        assert tray.free_cells == 20
    
    
    def test_remove_plants():
        tray = Tray("Petunia", 10, capacity=24)
        tray.remove_plants(4)
        assert tray.n_plants == 6
        assert tray.free_cells == 18
    

    and run, in the terminal:

    $ pytest test_nursery.py
    $ pytest -v test_nursery.py     # one line per test
    
    • from nursery import Tray imports the class from nursery.py (Class 5). This is why nursery.py should not contain code that runs when it is imported.
    • The test functions have no arguments, and return nothing: they only create objects, call methods, and assert the results.
    • test_nursery.py does not need import pytest: assert is part of Python, and pytest is the program that runs the file, from the terminal. The module pytest only needs to be imported when its functions are used in the tests, such as pytest.approx (1.3) and pytest.raises (1.4).
    • Each test creates its own trays, so that the tests do not depend on each other.

    • Exercise 1.2: Break remove_plants on purpose (replace -= by +=), run pytest test_nursery.py, and read the report: which test fails, and which values does pytest show? Then fix it.

    1.3 Floats: assert with a tolerance. Floats are rarely exact: in Python, 0.1 + 0.2 == 0.3 is False (try it). So floats should not be compared with ==. pytest.approx compares them with a tolerance. It is a function of the module pytest, so add import pytest at the top of test_nursery.py, and the new test at the end:

    import pytest
    from nursery import Tray
    
    ...
    
    def test_occupancy():
        tray = Tray("Petunia", 8, capacity=24)
        occupancy = tray.n_plants / tray.capacity           # 0.3333...
        assert occupancy == pytest.approx(1 / 3)            # default tolerance: relative, one in a million
        assert occupancy == pytest.approx(0.33, abs=0.01)   # absolute tolerance: between 0.32 and 0.34
        assert 0.1 + 0.2 == pytest.approx(0.3)
    
    • pytest.approx(x), without abs, accepts values very close to x (relative difference below 0.000001): use it when the exact value is known, but the float may be slightly off.
    • pytest.approx(x, abs=0.01) accepts any value between x - 0.01 and x + 0.01: use it when you only know the value to some decimal places.
    • Integers (such as tray.n_plants) and strings are exact: compare them with ==.

    1.4 Errors: with pytest.raises(...). Some calls must raise an exception, for instance creating a tray with too many plants. Add to test_nursery.py:

    def test_too_many_plants():
        with pytest.raises(ValueError):
            Tray("Pansy", 30, capacity=24)
    
    
    def test_add_too_many():
        tray = Tray("Pansy", 40)
        with pytest.raises(ValueError):
            tray.add_plants(20)
        assert tray.n_plants == 40            # after the error, the tray did not change
    
    
    def test_capacity_is_read_only():
        tray = Tray("Pansy")
        with pytest.raises(AttributeError):
            tray.capacity = 100
    
    • The code inside the with block must raise the error: the test passes if it raises ValueError, and fails if it raises no error (or a different one).
    • Put only the line that should raise the error inside the with block: the lines after it, inside the block, are not run.
    • After the with block, the test continues: it is a good place to check that the object did not change.

    • Exercise 1.4: Comment out the line raise ValueError(...) in remove_plants, and write a test that catches the bug.

    1.5 What to test. Good tests check:

    • the usual cases: add_plants(10) on a tray with 20 plants gives 30 plants;
    • the limits: an empty tray, a full tray (n_plants == capacity), removing all the plants;
    • the errors: the calls that must raise an exception, and that the object did not change after it.

    • Exercise 1.5: Write tests for the limits: Tray("Pansy", 0), Tray("Pansy", 24, capacity=24), and removing all the plants of a tray.

    1.6 Note: code that runs only when the script is run. nursery.py only defines the class, so that importing it runs no code. But it is often useful to keep, in the same script, some code that uses the class, for instance to try it with python nursery.py. Put that code in a function main(), and call it at the end of the script inside if __name__ == "__main__":

    class Tray:
        ...
    
    
    def main():
        tray = Tray("Pansy", 25)
        tray.add_plants(10)
        print(tray)
    
    
    if __name__ == "__main__":
        main()
    
    • When the script is run with python nursery.py, Python sets the variable __name__ to "__main__", so main() is called and the tray is printed.
    • When the script is imported, with from nursery import Tray in test_nursery.py, __name__ is "nursery" (the name of the module), so main() is not called, and the tests are not disturbed by prints or by errors in that code.
    • This is how the CS50P problems are organized since Lecture 0, and it is what allows pytest to import their functions and classes.

    • Exercise 1.6: Add main() and the if __name__ == "__main__": lines to nursery.py, and run python nursery.py and pytest -s test_nursery.py (-s shows what is printed during the tests). Then replace the two last lines by a single line main(), without indentation, and run both commands again: what changes?
    Part 2 — On your own: tests for the cookie jar
    • 2.1 The Cookie jar problem (Class 11, 5.5, https://cs50.harvard.edu/python/psets/8/jar/) also asks for a file test_jar.py. In the folder of jar.py, write at least four test functions, one for each of __init__, __str__, deposit and withdraw, and run them with pytest test_jar.py. Start from: ```python import pytest from jar import Jar

    def test_init(): jar = Jar() assert jar.capacity == 12 assert jar.size == 0 …

    def test_str(): jar = Jar() assert str(jar) == “” jar.deposit(1) assert str(jar) == “🍪” …

    For each method, test the usual cases, the limits (an empty jar, a full jar) and the errors, as in 1.5. For instance: does `Jar(-1)` raise `ValueError`? And depositing more cookies than fit in the jar? And withdrawing more cookies than there are in the jar?
    
    - **2.2** Check your tests: break `jar.py` on purpose (for instance, make `deposit` ignore the capacity), and check that at least one of your tests fails. Then fix `jar.py`, and submit both files.
    
    </details>
    
    <details markdown="block">
    <summary> Part 3 — Packing and unpacking: <code>*</code>, <code>**</code>, <code>*args</code>, <code>**kwargs</code> </summary>
    
    In the second half of the class, we use a new example: the tree inventory of a park. Download the script [class12/trees.py](/greends-ipython/class12/trees.py) into the folder `class12`:
    

    $ wget https://raw.githubusercontent.com/isa-ulisboa/greends-ipython/main/class12/trees.py $ code trees.py

    It contains a list `TREES`, with one tuple per tree: `(tree_id, species, dbh_cm, height_m)`, where *dbh* is the diameter at breast height (1.30 m), in centimetres. Write the code of Parts 3 and 4 in `main()`, or in a new script that starts with `from trees import TREES`.
    
    **3.1 Unpacking.** You have already unpacked tuples: the values of a tuple are assigned to several variables in one line.
    ```python
    tree_id, species, dbh, height = TREES[0]      # ("T01", "Quercus suber", 41.0, 11.5)
    for tree_id, species, dbh, height in TREES:   # unpacking in a for loop
        print(f"{tree_id}: {species}, {dbh} cm")
    

    The operator * packs the remaining values into a list, so that the number of variables does not have to match the number of values:

    tree_id, *measures = TREES[0]       # tree_id = "T01", measures = ["Quercus suber", 41.0, 11.5]
    dbhs = sorted(dbh for _, _, dbh, _ in TREES)
    smallest, *others, largest = dbhs   # first value, last value, and a list with the values in between
    

    (_ is the usual name for a value that is not used.)

    The packed values are an ordinary list, used as any other list:

    print(others)            # [21.0, 24.0, 25.5, 27.0, 29.0, 33.5, 35.5, 38.0, 41.0, 52.0]: one list
    print(*others)           # 21.0 24.0 25.5 27.0 29.0 33.5 35.5 38.0 41.0 52.0: ten values
    print(len(others))       # 10
    print(others[0])         # 21.0
    species, dbh, height = measures   # a packed list can be unpacked again
    

    print(others) and print(*others) show the same numbers, but they are not the same call. In print(others), print receives one argument, the list, and shows it with brackets and commas. In print(*others), the operator * unpacks the list (see 3.2), so print receives ten arguments, the numbers, and shows them separated by spaces. The variable others is the same list in both cases: only the call to print changes.

    3.2 Unpacking into a list, a dictionary, or a function call. In the other direction, * unpacks a list (or tuple) into its values, and ** unpacks a dictionary into its pairs key: value:

    cork_oaks = ["T01", "T02", "T05", "T11"]
    pines = ["T03", "T08"]
    selected = [*cork_oaks, *pines, "T12"]            # one list with all the values
    
    defaults = {"health": "unknown", "protected": False}
    measured = {"health": "good", "dbh_cm": 41.0}
    record = {**defaults, **measured}                 # {'health': 'good', 'protected': False, 'dbh_cm': 41.0}
    

    In record, the values of measured replace the values of defaults with the same key, since they come later. In a function call, * passes the values of a list as positional arguments, and ** passes a dictionary as named (keyword) arguments. For instance, with class Tray of Part 1:

    from nursery import Tray
    
    row = ("Pansy", 20, 24)
    tray1 = Tray(*row)              # the same as Tray("Pansy", 20, 24)
    
    data = {"species": "Petunia", "n_plants": 10, "capacity": 50}
    tray2 = Tray(**data)            # the same as Tray(species="Petunia", n_plants=10, capacity=50)
    
    print(*TREES, sep="\n")         # the same as print(TREES[0], TREES[1], ..., sep="\n")
    

    The last line is used in main() of trees.py: compare its output with print(TREES).

    3.3 *args: any number of positional arguments. print accepts any number of arguments. Our functions can do the same: in the definition of the function, *args packs all the positional arguments into a tuple. The basal area of a tree is the area of the cross-section of its trunk at 1.30 m, in m². The basal area of a group of trees is the sum of the basal areas of the trees:

    import math
    
    def basal_area(*dbhs):
        """Total basal area (m²) of trees with the given dbh (cm)."""
        total = 0
        for dbh in dbhs:                         # dbhs is a tuple
            total += math.pi * (dbh / 200) ** 2  # dbh in cm / 200 = radius in m
        return total
    
    print(basal_area(41.0))                      # one tree
    print(basal_area(41.0, 18.5, 33.5))          # three trees
    dbhs = [dbh for _, _, dbh, _ in TREES]
    print(basal_area(*dbhs))                     # all the trees: unpack the list (3.2)
    

    The name args is a convention: what matters is the *. Here, dbhs is clearer.

    3.4 **kwargs: any number of named arguments. In the definition of a function, **kwargs packs all the named arguments that do not match other parameters into a dictionary. This is useful to accept optional information that the function does not need to know in advance:

    def tree_record(tree_id, species, **details):
        """Return a dictionary with the data of a tree, and any other details."""
        return {"tree_id": tree_id, "species": species, **details}
    
    print(tree_record("T13", "Quercus suber"))
    print(tree_record("T14", "Pinus pinea", dbh_cm=30.5, health="good", notes="near the lake"))
    

    3.5 Order of the parameters. A function can combine all kinds of parameters, in this order: regular parameters, parameters with default values, *args, **kwargs. For instance, print is defined as print(*objects, sep=" ", end="\n", ...): all positional arguments are packed into objects, so sep and end can only be given by name.

    • Exercise 3.5: Write a function mean(*values) that returns the mean of any number of values, and raises ValueError if there are no values. Use it to compute the mean height of the trees, with mean(*heights).
    Part 4 — lambda, map, filter, and key functions

    4.1 Functions are values. A function can be passed as an argument to another function, as any other value. We use this function, for one tree:

    import math
    
    def tree_basal_area(tree):
        tree_id, species, dbh, height = tree
        return math.pi * (dbh / 200) ** 2
    

    4.2 map. map(function, values) applies the function to each value. It returns an iterator, which can be converted to a list with list:

    areas = list(map(tree_basal_area, TREES))       # one basal area per tree
    areas = [tree_basal_area(t) for t in TREES]     # the same, with a list comprehension (Class 4)
    

    Note that the function is passed without parentheses: map calls it for each tree.

    4.3 filter. filter(function, values) keeps the values for which the function returns True:

    def is_large(tree):
        return tree[2] >= 40                        # dbh of at least 40 cm
    
    large = list(filter(is_large, TREES))           # T01, T03, T06
    large = [t for t in TREES if is_large(t)]       # the same, with a list comprehension
    

    4.4 lambda. Functions such as is_large are short and used only once. A lambda defines a small function, with no name, in a single expression: lambda arguments: expression. The value of the expression is returned:

    large = list(filter(lambda t: t[2] >= 40, TREES))
    cork_oaks = list(filter(lambda t: t[1] == "Quercus suber", TREES))
    heights_ft = list(map(lambda t: t[3] * 3.281, TREES))     # heights in feet
    

    lambda t: t[2] >= 40 is the same as is_large, without the name. A lambda can only contain one expression: no if blocks, no loops, no return.

    4.5 key functions. The most common use of lambda is the argument key of sorted, min and max, which tells how the values are compared:

    by_height = sorted(TREES, key=lambda t: t[3], reverse=True)    # tallest first
    tallest = max(TREES, key=lambda t: t[3])                       # ("T06", "Platanus x hispanica", 61.0, 22.0)
    thinnest = min(TREES, key=lambda t: t[2])
    

    It also works with objects, for instance with the trays of Part 1: sorted(trays, key=lambda tray: tray.free_cells).

    4.6 Which one to use? A list comprehension is usually the most readable way to transform or select values, and it is the most common in Python code. map and filter are convenient when the function already exists (map(int, ["3", "5"])), and lambda is mostly used for short key functions. Do not give a name to a lambda (area = lambda t: ...): use def.

    • Exercise 4.6: Write the list of the species of the trees, without repetitions and sorted alphabetically, in one line. (Hint: set, then sorted.)
    Part 5 — On your own

    Tree inventory

    • 5.1 In the field, foresters often measure the circumference of the trunk at 1.30 m with a tape, and compute the diameter with dbh = circumference / π. Given the list circumferences = [128.8, 58.1, 163.4, 91.1] (cm), use map and lambda to compute the list of diameters, rounded to one decimal place.
    • 5.2 In Portugal, cork can only be harvested from cork oaks (Quercus suber) whose circumference at 1.30 m is at least 70 cm. Use filter and lambda to select, from TREES, the cork oaks from which cork can be harvested. Then write the same with a list comprehension.
    • 5.3 Use sorted with key to list the trees by species, and, for the same species, by decreasing dbh. (Hint: the key can return a tuple, such as (t[1], -t[2]).)
    • 5.4 Write a function count_species(*species) that returns a dictionary with the number of trees of each of the given species, for instance count_species("Pinus pinea", "Olea europaea") returns {"Pinus pinea": 2, "Olea europaea": 2}. With no arguments, it should count all the species.
    • 5.5 Write a function add_tree(trees, tree_id, species, dbh_cm, height_m, **details) that appends a tree to the list trees, and returns a dictionary with all its data, including the details (as tree_record in 3.4). Raise ValueError if dbh_cm or height_m are not positive.

    Tests (Part 1)

    • 5.6 Move basal_area (3.3), mean (3.5) and add_tree (5.5) to trees.py, and write test_trees.py with tests for them. Use pytest.approx for basal areas and means, and pytest.raises for mean() with no values and for add_tree with a negative dbh.

    Field productivity

    • 5.7 map can receive two lists, and then the function receives one value of each: map(lambda y, a: y / a, yields, areas). Given the yields (t) and areas (ha) of four fields, yields = [8.5, 9.1, 5.4, 10.0] and areas = [2.0, 2.5, 1.8, 3.0], compute the productivity (t/ha) of each field, rounded to two decimal places. Then use zip and filter to get the list of pairs ("Field 1", productivity) for the fields with productivity above 3.5 t/ha.
---