Exercises

Choose missing-key rules, build counts and groups, compare membership sets, trace aliases and reassignment, and predict flat or nested shallow copies.

Look Up, Count, and Group

Q1. Choose a meaningful key

Which structure directly retrieves the east reading by the label "east"?

Choose one

Select one choice, then check.

HintLook at the brackets used for retrieval

A list uses a position. A dictionary can use a string key.

SolutionUse keyed lookup

The dictionary expression retrieves 21 through the key "east".

Not attempted
Review

Not marked done.

Q2. Choose a valid missing-key policy

The program must reject a measurement record when its required "reading" field is absent. Which expression represents that requirement?

Choose one

Select one choice, then check.

HintDo not invent a measurement

Ask whether either fallback is a truthful reading.

SolutionKeep required data required

Use record["reading"]. A missing key should remain visible because neither zero nor None is a valid replacement measurement here.

Not attempted
Review

Not marked done.

Q3. Trace a frequency dictionary

What are the final counts for "low", "ordinary", and "high", in that order?

Compute it first, then check your number.

HintStart each unseen key at zero

ordinary appears three times; the other labels each appear once.

SolutionCount every occurrence

The final dictionary contains {"ordinary": 3, "low": 1, "high": 1}, so the requested answer is 1, 3, 1.

Not attempted
Review

Not marked done.

Q4. Build groups without overwriting

Complete the program so it displays {'north': [18, 20], 'east': [21]}.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintSeparate initialization from accumulation

Test whether the label is absent, create its list once, and append afterward.

SolutionKeep every reading under its key
Not attempted
Review

Not marked done.

Q5. Read items in a stated order

Complete the report so it displays east 1 followed by north 2.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintAsk for pairs, then sort them

Iterate with for label, count in sorted(counts.items()).

SolutionUnpack sorted items
Not attempted
Review

Not marked done.

Track Presence and Shared State

Q6. Compare two sets of labels

Enter the number of shared labels, labels missing from the second run, and new labels in the second run.

Compute it first, then check your number.

HintKeep the difference direction visible

Use first & second, first - second, and second - first.

SolutionCompare membership groups

The intersection has two labels. Each directional difference has one label, giving 2, 1, 1.

Not attempted
Review

Not marked done.

Q7. Predict an aliasing change

What do original and working display?

Choose one

Select one choice, then check.

HintCount objects, not names

working = original contains no list construction or copy operation.

SolutionBoth names observe the mutation

There is one list object with two names, so both display [18, 21, 20].

Not attempted
Review

Not marked done.

Q8. Distinguish mutation from reassignment

What does original display at the end?

Choose one

Select one choice, then check.

HintDraw the second list

The list literal [20, 23] creates another object for working.

SolutionThe original list remains unchanged

original still refers to the first list, so it displays [18, 21].

Not attempted
Review

Not marked done.

Copy Deliberately

Q9. Predict a flat shallow copy

What do original["north"] and working["north"] contain at the end?

Compute it first, then check your number.

HintA copy creates another outer dictionary

Replacing a number under one copied key does not edit the first dictionary.

SolutionOnly the copied mapping changes

original["north"] remains 18 and working["north"] becomes 19.

Not attempted
Review

Not marked done.

Q10. Predict a nested shallow copy

What does original["north"] contain at the end?

Choose one

Select one choice, then check.

HintDraw two outer boxes and one inner list

Follow the value stored under "north" in each outer dictionary.

SolutionThe nested list remains shared

Appending through working changes the one shared inner list, so original["north"] is [18, 20, 22].

Not attempted
Review

Not marked done.

Q11. Return a corrected copy

Complete the function so it returns {'north': 19, 'east': 21} without changing latest.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintGive the function a collection it owns

Begin with corrected = readings.copy() and update corrected.

SolutionCopy before changing
Not attempted
Review

Not marked done.

Pause and reflect

Which exercises were difficult, what mistake pattern did you notice, and what should you practice again? The note stays with this exercise set.

0 of 11 exercises marked done

Review

Not marked done.