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.
* 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</details> ---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 thisand many other anecdotes about Python.</blockquote>
---
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
See CS50P lecture 3, in particular the short on handling exceptions:
try,except,ValueError,KeyError.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
- 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) ```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") ```
- 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) ```
- 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
See lecture https://cs50.harvard.edu/python/weeks/4/
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.As explained on the recommended video, a python **package** is just a folder with modules and a special file named `__init__.py`Create and import your own module
Exercise: Create file named
utils.pyand filemain.pyin 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.pyThe contents of
utils.pyare typically functions or constants that you can re-use in different contexts. Let’s suppose thatutils.pyhas 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.pyis the following file:main.py (1st version)
import common.utils def main(): x=common.utils.get_integer() print(x) main()Run
main.pyfrom inside theclass_5folder (cd class_5, thenpython main.py), so that Python can find thecommonfolder.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.pathas 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 utilsPip install
Some modules, like
sys,mathorrandom, are part of the Python standard library: they come with Python and can be imported without installing anything. For instance,randomprovides 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 packagerequests(used below to access APIs) is installed with$ pip install requestsand 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/
sys.argvPreviously, 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.To run it, you can for instance execute the command `$python sum.py 1.2 4.3` in the terminal.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')
- 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 (keytrackName)? Notice that&limit=2in 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.pywithresponse=requests.get('https://api.gbif.org/v1/species/match?name='+ sys.argv[1])and execute it with, say,
$python myrequest.py Tracheophytain 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
requestspackage 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()
- 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
- For virtual environments, watch How to Use Virtual Environments with the Built-In venv Module (17’) and see the official documentation https://docs.python.org/3/library/venv.html
- For file I/O and images with Pillow, see lecture https://cs50.harvard.edu/python/weeks/6/ (sections on
open,withand Binary Files and PIL). CSV files will be discussed later with Pandas.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 (requestsandPillow), 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
.venvand 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 requestsThe 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
- Create the project folder and move into it. From now on, all commands are executed from
image_project:$ mkdir image_project $ cd image_project- Create the virtual environment. This creates the folder
.venvinsideimage_project:image_project/ $ python -m venv .venvSince the name starts with a dot,
.venvis a hidden folder: usels -ato see it.- Activate the environment:
image_project/ $ source .venv/bin/activate # Windows: .venv\Scripts\activateThe prompt now starts with
(.venv), which shows that the environment is active: the commandspythonandpipnow refer to the Python and packages in.venv. To de-activate it, the command isdeactivate.- 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.prefixis the folder of the Python installation in use: it should be the.venvfolder of your project.3. Install packages and create requirements.txt
- 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.5Packages that these depend on (e.g.
urllib3forrequests) are installed automatically. Compare the result ofpip listandpip show requestswith what you got in the base environment: the version and the location are different.- Check where Python looks for packages:
(.venv) image_project/ $ python -c "import sys; print(sys.path)"The
site-packagesfolder inside.venvis listed, but the folder where the base environment packages are stored is not.- Save the list of installed packages and versions to
requirements.txt, at the root of the project:(.venv) image_project/ $ pip freeze > requirements.txtOpen
requirements.txtto see its contents: one line per package, such aspillow==12.0.0.- Tell Git to ignore the
.venvfolder, by creating a file.gitignorewith the line.venv/:(.venv) image_project/ $ echo ".venv/" > .gitignoreIf you share the project (e.g. on GitHub), you share the scripts,
requirements.txtand.gitignore, but not.venv.4. Delete and re-create the environment
Since
.venvis disposable, we can delete it and re-create it fromrequirements.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 listExercise: install one more package in your environment (say,
emoji), updaterequirements.txt, and send the file to a colleague. Your colleague should create a new project folder with a.venvenvironment from yourrequirements.txtand check withpip listthat 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 outputIn this step we write a script
notes.py, in theimage_projectfolder, 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 ofnotes.py, and run the script after each addition with(.venv) image_project/ $ python notes.pyThen check the contents of
output/notes.txt(open it in the editor, or usecat output/notes.txtin the terminal).
openis 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 runningnotes.pyseveral 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
writedoes 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 tonotes.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 methodreadlinesreturns a list where each element is a line of the file (including the"\n"at the end). Add these lines tonotes.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.pyand 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 line6. Binary files: downloading and inspecting an image
Files can be text files (human-readable, like
notes.txtor a.pyscript) or binary files, like images. Binary files are opened with the modes"rb"and"wb", and their contents arebytesinstead ofstr.The following script
download_image.pydownloads a photo of an olive leaf (upper and lower surface) from Wikimedia Commons, using the packagerequests(see class 5). The image is inresponse.content, which is abytesobject, 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 inheaders.# 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 withb'\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 asPIL) does that for many formats. The following scriptexplore_image.pyopens the image, prints its properties and saves three new images inoutput.# 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.pngas in step 6: is it a PNG file?8. Exercise: process a folder of images
Write a script
process_images.pythat 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 (seesys.argvin class 5):(.venv) image_project/ $ python process_images.py images outputFor each image
name.jpg(or.jpeg,.png) inimages, the script savesoutput/name_thumb.pngand appends a linename.jpg -> output/name_thumb.pngto the fileoutput/log.txt. Other files in the folder are ignored. Add a few more leaf images toimagesto test it.Use the template below. The module
os(standard library) providesos.listdir(folder), which returns the list of file names in a folder,os.path.join(folder, name), which builds a path, andos.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
- Getting started with pandas: https://pandas.pydata.org/docs/getting_started/intro_tutorials/index.html
- Videos: overview by Rob Mulla, Learning Pandas for Data Analysis? Start Here. (22’); course by Corey Schafer, Pandas tutorial (11 videos)
- Tidy data: H. Wickham (2014), Tidy Data, Journal of Statistical Software 59(10)
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:
- Create a folder for the class and move into it:
$ mkdir class7 $ cd class7- Install
pandas, andipykernel, 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.venvcreated inside a subfolder. If a virtual environment is active (its name appears in the prompt), rundeactivatefirst.$ pip install pandas ipykernel- Download the notebook and open it:
$ wget https://raw.githubusercontent.com/isa-ulisboa/greends-ipython/main/class7/class7_pandas.ipynb $ code class7_pandas.ipynb- 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.- 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
DataFramestores them as columns, and each column (aSeries) has a single data type (df.dtypes). ADataFramecan 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 + 32The 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 mapmonth,day: month (jantodec) and weekday (montosun); note that there is no yearFFMC,DMC,DC,ISI: indices of the Canadian Fire Weather Index (FWI) systemtemp: temperature (°C);RH: relative humidity (%);wind: wind speed (km/h);rain: rain (mm/m²)area: burned area (ha), from 0 to 1090.84The file is read with
pd.read_csvand explored with.head(),.shape,.info()and.describe(). Look before you compute: the mean and the median ofareaare very different, because most fires burned (almost) nothing.1.4 Boolean masks: asking questions to the data.
fires['temp'] > 25is a column ofTrue/False(a mask), andfires[mask]keeps the rows where the mask isTrue. Masks are combined with&(and),|(or),~(not), with each condition between parentheses, and.isin()checks membership in a list. SinceTruecounts as 1,mask.sum()counts andmask.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
DataFramehas 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:bincludes bexcludes b, like Python listsslice example fires.loc[0:4, 'temp':'wind']
5 rows (labels 0 to 4), columnstemp,RH,windfires.iloc[0:4, 8:10]
4 rows (positions 0 to 3), columnstemp,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 andlocandilocseem 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 0Labels do not need to be integers.
set_indexturns 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
Seriesare combined, pandas matches their values by label, not by position. Labels that exist in only one of them giveNaN(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 13For 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 withiloc;fires.index[0:20]gives the labels of the first 20 rows, to use withloc.
- Exercise 1.5: Use
locto select the FWI variables for the fires in August and September. Then useilocto 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, appliessumto 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 newSeries(orDataFrame) with exactly the given labels, in the given order: existing labels keep their values, new labels getNaN, and labels not in the list are dropped. The tablemonthsof section 1.5 (with all 12 months, in the notebook) has the months in calendar order as its index, somonths.indexis 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
dayinstead. On which weekday is the median burned area largest? And the number of fires?2.2 Joining tables:
merge.mergecombines two tables through a common key, like a database join. In the notebook, a tablemonthswith the full name and the season of each month is merged withfires, 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_tablegoes 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), andmeltgoes from wide to tidy (the column names become the values of a new column). “Roughly” becausepivot_tableaggregates: aftermeltwe 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=0fills 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,mergeand plotting all expect one variable per column.meltconverts 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 namedpd.read_..., as listed in https://pandas.pydata.org/docs/user_guide/io.html. For instance:
- Export a dataframe as a CSV file with
df.to_csv('filename.csv', index=False). Useindex=Falseunless the index contains information (for instance, aftergroupby); otherwise, the labels0, 1, 2, ...are written as an extra column.- Export as an Excel spreadsheet with
df.to_excel('filename.xlsx', sheet_name='fires', index=False)(requirespip install openpyxl).- Read an Excel spreadsheet with
pd.read_excel('filename.xlsx', sheet_name='fires').---
Class 8 (November 6, 2026): pandas, continued: select, sort, group, create
- pandas user guide: indexing and selecting data, working with text data, group by: split-apply-combine
- Videos: course by Corey Schafer, Pandas tutorial, in particular the videos on filtering, sorting, and grouping and aggregating
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),nlargestWhat is the summary for each group? groupby+aggHow do I create a new variable? arithmetic on columns; applywith a function or alambdaAll 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), andfires[mask]keeps the rows where the mask isTrue. Conditions are combined with&(and),|(or),~(not), and each condition needs its own parentheses. The Python wordsandandordo 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, andmask.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_valuesreturns 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;ascendingcan 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
dayand then bytemp(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()..aggcomputes 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:
applyandlambda. When the new variable is a formula, use arithmetic on columns (fires['area'] / 100). When the rule needs anif/else, or a function of your own,.apply(f)calls the functionfon each value of the column. A lambda is a function without a name, written in one line:lambda a: expressionis the same asdef f(a): return expression. Withaxis=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 variableRule of thumb: use arithmetic and masks when possible (they are much faster), and
applywhen you need a function of your own.
- Exercise 1.4: Create a column
wind_class(calm,moderate,strong) withapply, and compute the mean burned area for each class. Create a columnweekendwithapplyand alambda, 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 PTcountry Portugal 1 character NUTS I 1Continente,2Açores,3Madeira2 characters NUTS II 11Norte,19Centro,1AGrande Lisboa,1BPenínsula de Setúbal,1CAlentejo,1DOeste e Vale do Tejo,15Algarve,20,303 characters NUTS III 111Alto Minho,112Cávado, …7 characters municipality 1111601Arcos de ValdevezThe 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 (dtypeisobject, orstr` 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
rfwithpd.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 withapplyand alambdathat converts one value.- 2.2 Split the first column into
codeandnamewith.str.split(': ', n=1, expand=True).Selecting rows
- 2.3 Keep the municipalities (codes with 7 characters) in a dataframe
mun, and add columnsnuts2andnuts3with 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 withVila, 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 (applyon rows withaxis=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_fireswith.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
class9in the CS50 codespace and write the code of Parts 2 and 3 in a script, for instancenursery.py, and run it withpython nursery.py. Short tests can also be done in the Python REPL (typepythonin 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.shapeandfires.columnsare attributes (data, no parentheses), whilefires.head()andfires.groupby('month')are methods (actions, with parentheses). The same happened with Pillow in Class 6 (img.sizeandimg.rotate(90)) and withrequestsin Class 5 (response.status_codeandresponse.json()).The function
dir(s)lists all attributes and methods of an object.
- Exercise 1.1: Create two strings
a = "olive"andb = "cork oak". Doa.upper()andb.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_plantsis 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 classTray, calls__init__to fill in its attributes, and returns the object. The object is called an instance of the class.selfis the object that the method works on, exactly like the argumenttrayin the dictionary version. When you writetray1.add_plants(10), Python runsTray.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 thatn_plantsis optional, with default value 0 (as for any function), and create a tray withTray("Begonia").2.4 The most common errors. Each line marked
# bugbelow 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 objects3.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_PLANTSis a class attribute: it is defined once, in the class, and is the same for all trays (Tray.MAX_PLANTS).self.speciesandself.n_plantsare 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 ValueErrorand 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__. Tryprint(tray1): the result looks like<__main__.Tray object at 0x7f...>, which is not very informative. When an object is printed, or converted withstr(), or used in an f-string, Python calls its method__str__, which should return a string. Add this method to classTray: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
traysis a dictionary whose values areTrayobjects, 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_trayreturns the tray object itself, not a copy, sonursery.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, whendefis executed, and not each time the function is called: all inventories created without the argumenttraysshare the same dictionary. Rule: never use a list or a dictionary as a default value. Create it inside__init__(as in 3.3), or usetrays=Noneand thenif 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 ofTray, 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 classInventory(3.3) in the scriptnursery.py. Add the following methods to classInventory:
- 4.1
remove_tray(tray_name): removes a tray, and raisesValueErrorif there is no tray with that name.- 4.2
list_all_trays(): prints one line per tray, for instanceA1: 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, wherepricesis a dictionary with the price of each species.
Some more challenging exercises:
- 4.7
sell(species, n): removesnplants 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, raiseValueErrorwithout changing any tray.- 4.8
load_csv(path): adds trays read from a CSV file with columnstray,species,n_plants(create a small file yourself; see Class 6 for reading files).- 4.9
to_dataframe(): returns a pandasDataFramewith one row per tray and columnstray,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
- CS50P, Lecture 8: Object-Oriented Programming, and its notes: this class covers the rest of the lecture (properties, class methods, inheritance, operator overloading).
- CS50P shorts, to revise Class 9: Classes, Instance Variables, Instance Methods, Class Methods and Class Variables (on the Lecture 8 page).
- Video: Avoid These BAD Practices in Python OOP, on unnecessary classes and deep inheritance hierarchies.
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
class11in the CS50 codespace, and start from the scriptnursery.pydescribed in Setup below.
Setup: the starting script
nursery.pyYour own
nursery.pychanged many times during the exercises of Class 9. To start Class 11 from the same point, download the script class11/nursery.py, which contains classTrayof Class 9 (3.1 and 3.2). In this class we only work with trays: classInventoryof Class 9 is not needed.$ mkdir class11 $ cd class11 $ wget https://raw.githubusercontent.com/isa-ulisboa/greends-ipython/main/class11/nursery.py $ code nursery.pyThe 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
Trayof Class 9:
short in class TrayClasses class Tray:is the blueprint;Tray("Pansy", 26)creates an instanceInstance Variables self.species,self.n_plants: each tray has its own valuesInstance Methods add_plants(self, n),remove_plants(self, n),__str__(self): they receive the instance asselfClass Methods and Class Variables MAX_PLANTS: one value shared by all trays (see 1.2). Class methods: see 1.3 and 1.41.2. Class variable: set a variable to count the number of instances created in a class
- Exercise 1.2: Add a class variable
n_traysto classTray, that counts how many trays have been created: it starts at 0, and__init__increases it withTray.n_trays += 1. Create a few trays, and printTray.n_trays. Should a tray that could not be created (for instanceTray("Pansy", 60)) be counted? Where should the lineTray.n_trays += 1be 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, calledclsby convention, and is called on the class (Tray.method()), without the need for an existing tray. It is marked with@classmethodon the line beforedef(a decorator: it changes how the method is called).
instance method class method first argument self: one objectcls: the classcalled 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_traysof 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 neededInside a class method,
clsis the classTray:cls.n_traysis the same asTray.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 asTray(...): it creates a tray, calling__init__. This is the same idea asStudent.get()in the CS50P notes.
- Exercise 1.4: Add
reportandfrom_csv_rowto classTray, create a few trays withfrom_csv_row, and callTray.report(). Why couldfrom_csv_rownot be an instance method? (Hint: which tray wouldselfbe?)Part 2 — Attributes and properties
2.1 The problem. The methods of
Traycheck 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 invalidThe 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 withadd_plantsandremove_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. Innursery.py, rename the attributen_plantsto_n_plants(only the attributeself.n_plants, not the argumentn_plantsof__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_plantsof__init__was not changed. Note that the underscore is only a convention, a message to other programmers: Python does not preventtray1._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 tryprint(tray.n_plants)andtray._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
@propertyon the line beforedef(a decorator, like@classmethod), and it is read without parentheses. Add to classTray:@property def n_plants(self): return self._n_plantsand 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 assignedThe problem of 2.1 is solved:
tray1.n_plantsis read as before, but the number of plants can only be changed withadd_plantsandremove_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, ashousein 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 = 50plants. 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, liken_plants. The number of free cells is computed from the capacity and the number of plants: it becomes a property too. Replace classTrayby the following, and addfrom_csv_rowof 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)"
capacityandn_plantsare read-only properties: their values are stored in the internal attributes_capacityand_n_plants, and only the methods of the class change them.free_cellshas 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 attributefree_cells, and we would have to update it in__init__,add_plantsandremove_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.speciesortray.free_cells, without parentheses. The difference is in what happens behind:
attribute property defined in __init__:self.species = speciesas a method, with @propertythe value is stored in the object is returned by the method, each time it is read read tray.speciestray.free_cells(no parentheses)assign tray.species = "Begonia"workstray.free_cells = 0raisesAttributeErroruse 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_cellscould be a method, called withtray.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 = 104andtray.free_cells = 0all 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 instancePetunia,24,24. Then create a list of trays from the listrows = ["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 isTruewhen the tray has no free cells. Then discuss: shouldspeciesstay 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:
- Will there be several instances, each with its own data?
- Does the data change over time, and must it stay valid?
- 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)andgermination_rate(100, 87). Functions that belong together can be grouped in a module (Class 5), for instanceunits.py, and imported withimport 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:
ValueErrorandKeyErrorare kinds ofException. 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."""
CapacityErroris a child (subclass) of the parentValueError. Raise it instead ofValueErrorwhen the plants do not fit in the tray, in__init__and inadd_plants. ACapacityErroris aValueError, soexcept ValueErrorstill catches it, andexcept CapacityErrorcatches only this error.
- Exercise 4.1: Add
CapacityErrortonursery.py, and check whatisinstance(e, ValueError)andisinstance(e, CapacityError)return for the exceptions raised bytray.add_plants(100)and bytray.remove_plants(100).4.2 A child class with
super(). Seeds are sown in trays too, but only some of them germinate. ASeedTrayis aTraywith 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 ofTray(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 └── BiodegradableTrayEach 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
SeedTrayis aTraythat can predict germination);- has a → composition: an object stores other objects as attributes.
4.4 Composition. You already used composition in Class 9: an
Inventoryis not a kind of tray, but it has trays, stored in its attributetrays. Here is a second example. In a greenhouse, trays are placed on benches. A bench is not a kind of tray, soclass 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
Benchdoes not need to know how a tray stores its plants or checks its rules: it only uses what the trays offer (here,n_plants). UnlikeInventory,Benchkeeps 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 SeedTrayis aTraya Benchhas traysin the code class SeedTray(Tray):self.trays = []in__init__ofBenchthe 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
Benchtonursery.py, and a methodtrays_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_plantsThe same call works on different kinds of objects, and each object responds in its own way: this is called polymorphism.
Benchdoes not need to check which kind of tray each one is.
- Exercise 4.5: Predict what the loop prints. Then add to
Bencha methodexpected_plants(), that returnssum(tray.expected_plants() for tray in self.trays), and call it for benchB2. Why does it fail? Add a methodexpected_plants()to classTray, that returnsn_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 + bcallsa.__add__(b). Define__add__in classTray, so thattray1 + tray2returns a new tray with the plants of both trays, and the capacity of the larger one. RaiseValueErrorif 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 likePansy,25,50(species, number of plants, capacity), usesTray.from_csv_rowto 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, andfull_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
materialandmax_uses(defaultNone, meaning no limit) to classTray, and an attributen_usesthat counts how many times the tray was filled withadd_plants. When a tray has been filledmax_usestimes,add_plantsshould raiseValueError(see 4.3).Assignment
- 5.5 Solve the Cookie jar problem described at https://cs50.harvard.edu/python/psets/8/jar/, and check it with
check50 cs50/problems/2022/python/jar. ClassJaris very close to classTray:
JarTrayJar(capacity=12),ValueErrorif the capacity is not validTray(species, n_plants, capacity)(2.4)jar.capacity, read-onlytray.capacity(2.4)jar.size: number of cookies, read-onlytray.n_plants(2.3)deposit(n),withdraw(n),ValueErrorif impossibleadd_plants(n),remove_plants(n)str(jar): one 🍪 per cookie__str__In Class 12, you will write tests for your class
Jarintest_jar.py.---
Class 12 (December 4, 2026): unit tests with pytest; packing and unpacking, lambda, map, filter
- CS50P short on pytest (on the Lecture 5 page). Lecture 5: Unit Tests and its notes go further, if you are interested.
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), andwith 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 (*argsand**kwargs), and the functionslambda,mapandfilter, 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,
*argsand**kwargs.- Part 4 (guided):
lambda,map,filter, andkeyfunctions.- Part 5 (on your own): exercises.
Create a folder
class12in the CS50 codespace, and start from the scriptnursery.pydescribed in Setup below.
Setup: the starting script
nursery.pyYour own
nursery.pychanged many times during the exercises of Class 11. To start Class 12 from the same point, download the script class12/nursery.py, which contains classTrayof Class 11 (2.4): trays of different capacities, with the read-only propertiescapacity,n_plantsandfree_cells.$ mkdir class12 $ cd class12 $ wget https://raw.githubusercontent.com/isa-ulisboa/greends-ipython/main/class12/nursery.py $ code nursery.pyThe script only defines the class, and does not run any code: this is important, since
nursery.pywill be imported by the tests (see 1.6 for how to add code that runs only withpython nursery.py). The tests are written in a second script,test_nursery.py, in the same folder.nursery.pyonly changes in the exercises where you break a method on purpose, to see a test fail.Check that
pytestis installed withpytest --version, and otherwise install it withpip install pytest.Part 1 — Unit tests with
pytest1.1
assert. The statementassert conditiondoes nothing if the condition isTrue, and raises anAssertionErrorif it isFalse: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 # AssertionErrorIn a script, the first
AssertionErrorstops 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 withtest_, in a separate file whose name starts withtest_.pytestfinds and runs all the test functions, and reports which ones pass and which ones fail, instead of stopping at the first error. Create the filetest_nursery.pyin the folderclass12: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 == 18and run, in the terminal:
$ pytest test_nursery.py $ pytest -v test_nursery.py # one line per test
from nursery import Trayimports the class fromnursery.py(Class 5). This is whynursery.pyshould 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
assertthe results.test_nursery.pydoes not needimport pytest:assertis part of Python, andpytestis the program that runs the file, from the terminal. The modulepytestonly needs to be imported when its functions are used in the tests, such aspytest.approx(1.3) andpytest.raises(1.4).Each test creates its own trays, so that the tests do not depend on each other.
- Exercise 1.2: Break
remove_plantson purpose (replace-=by+=), runpytest test_nursery.py, and read the report: which test fails, and which values doespytestshow? Then fix it.1.3 Floats:
assertwith a tolerance. Floats are rarely exact: in Python,0.1 + 0.2 == 0.3isFalse(try it). So floats should not be compared with==.pytest.approxcompares them with a tolerance. It is a function of the modulepytest, so addimport pytestat the top oftest_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), withoutabs, accepts values very close tox(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 betweenx - 0.01andx + 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 totest_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
withblock must raise the error: the test passes if it raisesValueError, and fails if it raises no error (or a different one).- Put only the line that should raise the error inside the
withblock: the lines after it, inside the block, are not run.After the
withblock, 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(...)inremove_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.pyonly 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 withpython nursery.py. Put that code in a functionmain(), and call it at the end of the script insideif __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__", somain()is called and the tray is printed.- When the script is imported, with
from nursery import Trayintest_nursery.py,__name__is"nursery"(the name of the module), somain()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
pytestto import their functions and classes.- Exercise 1.6: Add
main()and theif __name__ == "__main__":lines tonursery.py, and runpython nursery.pyandpytest -s test_nursery.py(-sshows what is printed during the tests). Then replace the two last lines by a single linemain(), 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 ofjar.py, write at least four test functions, one for each of__init__,__str__,depositandwithdraw, and run them withpytest test_jar.py. Start from: ```python import pytest from jar import Jardef 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)andprint(*others)show the same numbers, but they are not the same call. Inprint(others),print(*others), the operator*unpacks the list (see 3.2), soothersis the same list in both cases: only the call to3.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 pairskey: 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 ofmeasuredreplace the values ofdefaultswith 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 classTrayof 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()oftrees.py: compare its output withprint(TREES).3.3
*args: any number of positional arguments.*argspacks 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
argsis a convention: what matters is the*. Here,dbhsis clearer.3.4
**kwargs: any number of named arguments. In the definition of a function,**kwargspacks 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(*objects, sep=" ", end="\n", ...): all positional arguments are packed intoobjects, sosepandendcan only be given by name.
- Exercise 3.5: Write a function
mean(*values)that returns the mean of any number of values, and raisesValueErrorif there are no values. Use it to compute the mean height of the trees, withmean(*heights).Part 4 —
lambda,map,filter, andkeyfunctions4.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) ** 24.2
map.map(function, values)applies the function to each value. It returns an iterator, which can be converted to a list withlist: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:
mapcalls it for each tree.4.3
filter.filter(function, values)keeps the values for which the function returnsTrue: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 comprehension4.4
lambda. Functions such asis_largeare short and used only once. Alambdadefines 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] >= 40is the same asis_large, without the name. Alambdacan only contain one expression: noifblocks, no loops, noreturn.4.5
keyfunctions. The most common use oflambdais the argumentkeyofsorted,minandmax, 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.
mapandfilterare convenient when the function already exists (map(int, ["3", "5"])), andlambdais mostly used for shortkeyfunctions. Do not give a name to alambda(area = lambda t: ...): usedef.
- Exercise 4.6: Write the list of the species of the trees, without repetitions and sorted alphabetically, in one line. (Hint:
set, thensorted.)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 listcircumferences = [128.8, 58.1, 163.4, 91.1](cm), usemapandlambdato 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
filterandlambdato select, fromTREES, the cork oaks from which cork can be harvested. Then write the same with a list comprehension.- 5.3 Use
sortedwithkeyto list the trees by species, and, for the same species, by decreasing dbh. (Hint: thekeycan 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 instancecount_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 listtrees, and returns a dictionary with all its data, including thedetails(astree_recordin 3.4). RaiseValueErrorifdbh_cmorheight_mare not positive.Tests (Part 1)
- 5.6 Move
basal_area(3.3),mean(3.5) andadd_tree(5.5) totrees.py, and writetest_trees.pywith tests for them. Usepytest.approxfor basal areas and means, andpytest.raisesformean()with no values and foradd_treewith a negative dbh.Field productivity
- 5.7
mapcan 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]andareas = [2.0, 2.5, 1.8, 3.0], compute the productivity (t/ha) of each field, rounded to two decimal places. Then usezipandfilterto get the list of pairs("Field 1", productivity)for the fields with productivity above 3.5 t/ha.