Exercises

Select and repair ranges, trace mutation and text transformation, align pairs, verify summaries, expand compact forms, and order labelled readings.

Select and Build Ordered Values

Q1. Predict indexes and a slice

Enter the first item, last item, and the two-item middle slice in order.

readings = [18, 21, 20, 23]

Compute it first, then check your number.

HintRead the slice boundaries

readings[1:3] includes positions 1 and 2.

SolutionSelect the endpoints and middle

readings[0] is 18, readings[-1] is 23, and readings[1:3] is [21, 20].

Not attempted
Review

Not marked done.

Q2. Distinguish one item from a range

Which expression returns the list [21, 20] rather than one number?

Choose one

Select one choice, then check.

HintLook for the colon

A slice contains start and stop boundaries.

SolutionUse a slice

readings[1:3] includes positions 1 and 2 and returns [21, 20].

Not attempted
Review

Not marked done.

Q3. Repair an invalid index

Repair the selection so the program displays 23 for any non-empty list.

Editable Python

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

Ready to run.

HintCount from the end

The last item has index -1.

SolutionSelect the last item
print(readings[-1])
Not attempted
Review

Not marked done.

Q4. Keep the changed list

Repair the program so it displays [18, 20] rather than None.

Editable Python

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

Ready to run.

HintSeparate the change from a returned value

Write available.append(20) as its own statement.

SolutionLet append mutate the list
Not attempted
Review

Not marked done.

Transform Text and Align Values

Q5. Trace a short text transformation

What final string is produced?

Answer it first, then check.

HintWrite every intermediate value

The word list is ["mean", "temperature"].

SolutionJoin the two lowercase words

"-".join(["mean", "temperature"]) returns "mean-temperature".

Not attempted
Review

Not marked done.

Q6. Unpack one labelled reading

Which assignment gives label the value "sensor-a" and reading the value 21?

Choose one

Select one choice, then check.

HintKeep the positions aligned

The label is first in the pair.

SolutionUnpack left to right

label, reading = ("sensor-a", 21) assigns both roles in order.

Not attempted
Review

Not marked done.

Q7. Trace enumerate and zip

Which statement is correct?

Choose one

Select one choice, then check.

HintCount the available pairs

labels contains only two items.

SolutionPositions and lengths determine the pairs

Enumerating includes (2, 20). Zipping produces ("a", 18) and ("b", 21), then stops.

Not attempted
Review

Not marked done.

Summarize, Transform, and Order

Q8. Compare a loop with sum

Complete the check showing that the loop and sum produce the same total.

Editable Python

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

Ready to run.

HintCheck equivalent results

Add assert loop_total == sum(readings) before the print.

SolutionCompare the two totals
assert loop_total == sum(readings)
Not attempted
Review

Not marked done.

Q9. Expand a comprehension into a loop

Complete the loop equivalent of [reading + 1 for reading in readings if reading is not None].

Editable Python

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

Ready to run.

HintPreserve the comprehension order

Use an if reading is not None block, then append reading + 1.

SolutionFilter before transforming
Not attempted
Review

Not marked done.

Q10. Read a generator expression

What value is returned?

sum(reading * 2 for reading in [4, 6, 5])

Compute it first, then check your number.

HintExpand before summarizing

The equivalent list is [8, 12, 10].

SolutionSum the produced values

8 + 12 + 10 is 30.

Not attempted
Review

Not marked done.

Q11. Order with a named key

Complete the named key and sorted call. Preserve the source order and display reports by reading, then label.

Editable Python

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

Ready to run.

HintName the comparison order

Return (reading, label), then call sorted(reports, key=reading_then_label).

SolutionOrder by value and break ties by label
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.