COMP90059 Chap.1 Programming Foundations
Programming Foundations
This chapter establishes the execution model used throughout the subject. A Jupyter notebook contains markdown cells for explanation and code cells for Python. Running a cell can bind names, import modules or mutate objects that later cells use, so page order and execution order are not the same thing. A clean restart followed by Run All exposes hidden dependencies.
The chapter also separates statements from expressions, assignment from display, and conversion from formatting. These distinctions make a first program traceable: external text enters through input, explicit conversion creates the intended numeric type, an expression computes a value, assignment binds a name, and formatted output presents the result. The aim is not to memorise notebook buttons.
It is to treat every cell as a small reproducible experiment whose state and result can be explained.
What this chapter covers
- 01
Code cells execute Python and can change notebook state
- 02
Markdown cells record reasoning, units, assumptions and headings
- 03
Python 3 is the language environment used in the subject
- 04
Expressions evaluate to values while statements perform actions
- 05
Assignment evaluates the right side before binding the left name
- 06
Input text requires explicit conversion before numeric operations
- 07
Print produces human-facing output while the notebook can echo representations
- 08
A clean kernel run tests whether the notebook is reproducible
- 09
Tracebacks identify the exception family and the failing operation
- 10
Small experiments isolate one uncertain language rule at a time
- 11
Type checks explain why visually similar values behave differently
- 12
Reproducible notebooks state assumptions beside the code that uses them
Worked example · free
Convert input and format a duration
- 1Convert the count with int so it becomes the integer 18.
- 1Convert the duration with float so it becomes 2.5.
- 1Multiply the numeric values to obtain 45.0 minutes.
- 1Use an f-string with one decimal place to display Total: 45.0 min.
- 1Explain that formatting changes presentation, not the stored float.
Key terms
- Code cell
- A notebook cell whose Python is executed by the current kernel and whose result or output appears beneath it.
- Markdown cell
- A notebook cell for formatted explanation, headings, equations and reasoning rather than executable Python.
- Expression
- A combination of values, names, calls and operators that Python evaluates to one value.
- Statement
- An instruction such as assignment or import that performs an action and normally does not echo a value.
- Kernel state
- The current collection of names, imports and objects created by code that has already run.
- Explicit conversion
- A deliberate call such as int or float that creates a value of the required type.
- Traceback
- Python's report of the active call path, exception family and failing source line; read it from the final line upward before changing code.
- Reproducible execution
- A run whose result can be regenerated from a fresh kernel by executing documented cells in order with no hidden state.
- Type inspection
- Checking the runtime class of a value to explain which operators and conversions are valid at that point in a program.
Programming Foundations FAQ
Why can a notebook work in the wrong order?
The kernel remembers cells that ran earlier, even when they appear later on the page. Restarting and running from top to bottom reveals any value that was being borrowed from stale state and confirms whether the notebook is reproducible.
What is the difference between assignment and output?
Assignment evaluates an expression and binds its value to a name. Output is a separate action, usually through print or the notebook display, that presents a value to a reader.
Why must input usually be converted?
Input returns text. Numeric operators need numeric operands, so a program should convert the text near the input boundary and handle a failed conversion deliberately.
How should comments be used?
A useful comment explains a decision, assumption or non-obvious constraint. Comments that merely repeat the visible syntax add noise without improving the reader's reasoning trace.
What should I inspect before fixing an exception?
Read the exception family and final traceback line, then inspect the values and types used by that operation. Reproduce the failure in the smallest cell possible so a change tests one hypothesis rather than masking several causes.
How can I make a notebook submission easy to audit?
Keep the problem statement, assumptions and units near the code that depends on them. Use short cells with one purpose, choose descriptive names and preserve the output needed to support a conclusion. Before submission, restart the kernel and run all cells in order; then inspect the complete execution for errors, hidden dependencies and unexplained results.
Exam move
Build one tiny notebook from a clean kernel. Use a markdown cell to state a problem and its units, then a code cell to receive or define input, a second cell to convert and compute, and a final cell to format the result. Predict the value and type produced by each expression before running it.
Then deliberately create four failures: use text in arithmetic, refer to an undefined name, enter nonnumeric input and run cells out of order. Record the error family and the broken assumption. End by restarting the kernel and running every cell in sequence. The notebook should reproduce the same result without manual repair.
This short routine joins explanation, execution state, conversion, assignment and output in one auditable workflow.
Working through Programming Foundations in COMP90059? Sia is AskSia’s AI Computer Science tutor — ask any COMP90059 Programming Foundations question and get a clear, step-by-step explanation grounded in how COMP90059 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.