IS5312 Chap.5 Functions, Sets and Dictionaries
Functions, Sets and Dictionaries
This chapter treats functions as testable contracts with inputs, local state and returned outputs. It covers parameters and arguments, local and global scope, mutable versus immutable arguments, nested calls, default values and keyword arguments. The collection section distinguishes lists from sets and dictionaries by invariants: order, uniqueness, mapping and permitted mutation.
Set operations and dictionary views make common comparisons and summaries direct.
What this chapter covers
- 01
Function parameters and return values
- 02
Local scope and function calls
- 03
Mutable arguments and side effects
- 04
Set union, intersection and difference
- 05
Dictionary keys, values and items
Worked example · free
Summarise a score dictionary
- 2Select the dictionary values.
- 3Calculate the average.
- 3Connect the minimum to its key.
Key terms
- Local Scope
- The region where a function parameter or local variable can be accessed.
- Set Difference
- The items in one set that are absent from another.
- Dictionary Item
- A key and its associated value treated as a pair.
Functions, Sets and Dictionaries FAQ
Why should a function return instead of print?
A returned result can be tested, composed and formatted by the caller, while printed output is harder to reuse. Keeping presentation outside also lets several callers reuse the same calculated value differently.
When should I use a set?
Use a set when uniqueness or membership relations are the central requirement and order is not needed. Union, intersection and difference then express the required relationships directly and remove duplicates intentionally.
What is dangerous about mutable arguments?
A function can change the same object that the caller and other variables still reference, creating a hidden side effect. State that mutation in the contract or return a separate collection so callers are not surprised.
Assessment move
Prepare functions by writing contracts rather than memorising definitions. For each function, state the accepted input types, parameter meaning, returned object, possible mutation and exceptional cases. Then write tests that call the function directly, without input or print, so calculation is isolated from interaction.
Compare positional, keyword and default arguments with one three-parameter function; deliberately swap two positional values, then repair the call with names to see when keyword arguments improve clarity. Draw a call frame containing parameters and local variables, and cross it out when the function returns.
Use this drawing to explain why a local name is unavailable to the caller and why two functions may use the same local name without sharing state. For mutation, call one function with an integer and another with a list. Record the caller value before and after each call. If a function changes a collection, make that effect explicit in the name and contract; otherwise return a new collection.
Revise sets through questions rather than syntax. Given two small groups, calculate membership, union, intersection and both directional differences by hand, then verify them. Note which result loses order and duplicates. Revise dictionaries by choosing the correct view. Use keys when identifiers matter, values for numeric summaries and items when the association must remain visible.
Build a nested dictionary one level at a time, check both keys before deletion and decide what an empty inner mapping means. Add comprehension practice only after the ordinary loop is correct. Expand every comprehension into initialisation, iteration, optional condition and insertion. Compare a list result with a set result so uniqueness is an intentional consequence.
Finish with a small record manager whose functions add a value, remove a named value, compute an average and display sorted outer keys. Test creation of a missing record, replacement of an existing value, deletion of a missing key, an empty average and display order. The final review question for any function is whether another part of the program can use its result without reading the screen.
If not, return the useful object and leave presentation to the caller. Add a contract review in which one function is tested without any printing. Supply normal input, an empty collection and a value at the accepted boundary, then compare the returned object and any changed argument with the written contract.
Use a second function to demonstrate a default parameter, calling it once with the default and once with an explicit keyword so the two binding paths are clear. For set work, create two groups with one shared item and one duplicate in the original lists; predict union, intersection and both differences before conversion.
For dictionaries, distinguish a missing key from a key whose value is an empty collection, because those states often need different handling. Trace nested access one key at a time and write a safe membership guard before deletion. Finish by connecting the topics: write a function that accepts a mapping of names to lists, returns the set of distinct values and leaves the mapping unchanged.
Test the contract by comparing the mapping before and after the call as well as checking the returned set.