Python for a Coding Assessment
A refresher path through the Python graph for someone who has written Python before and has an assessment coming: run and read, rebuild the core, then work the way an assessment expects.
I can catch the exceptions a piece of code can actually raise, let the rest propagate, and raise my own with a message that tells the caller what to do.
Deciding what a piece of code should do when the thing it depends on fails, and saying so in code.
In scope: catching specific exceptions, letting others propagate, else and finally, raising with
a useful message, and defining an exception type when the standard ones do not fit. Out of scope:
logging configuration and retry strategies.
An except clause is a claim that you know what to do about that failure. If you cannot answer it
here, let it travel: the caller may know, and if nobody does, the traceback is a better outcome than
silence.
That makes the specific exception type the whole point. except FileNotFoundError says the file
might be missing and here is the fallback. except Exception says nothing, and it will also swallow
the typo in the line below — the bug you most wanted to see.
else and finally are actually forelse runs only if the try block did not raise. It keeps the risky line alone in the try,
so the success path is not accidentally inside the protection.finally runs whatever happened, including on the way out via an exception or a return. It
is for releasing what must be released. A with statement is usually the better version of the
same intent.An exception is a message to whoever called you. raise ValueError(f"expected a positive count, got {count}") tells them what was wrong and what value did it. Returning None
instead pushes the failure downstream, where it surfaces as an AttributeError far from the cause.
Define your own exception class when callers need to distinguish your failure from everyone else’s.
One class inheriting from Exception is usually enough; a hierarchy is rarely earned.
finally runs after return, so it cannot matter.” It runs during the return, and can
change what is returned. That is a trap worth seeing once.Give learners a script wrapped in try: ... except: pass that silently produces a wrong answer, and
ask them why it is wrong before showing the fix. The habit to build is asking “what would I do about
this?” before writing an except.
| Resource | Kind | Cost | Provider |
|---|---|---|---|
| The Python Tutorial: errors and exceptions | reading | free | Python Software Foundation |
| Automate the Boring Stuff, chapter 3: functions, including exception handling | reading | free | Al Sweigart |
A refresher path through the Python graph for someone who has written Python before and has an assessment coming: run and read, rebuild the core, then work the way an assessment expects.
A timed take-home in the shape real hiring uses: read messy data, compute a result, test it, and hand over a repository someone else can run.
The holder can take an unfamiliar problem, write a tested Python solution someone else can run, and explain what it costs.
Nothing to show yet. The publisher last published on 2026-09-20, and no aggregate for this outcome has reached the 50 learners a number needs. Open Degree does not track learners. About learner evidence.