Scientific Python fundamentals
Why it matters
Clear variables, control flow and functions turn scientific reasoning into inspectable, testable calculations.
Prerequisites
Course 0.2; basic programming helpful; prior canonical classes where applicable.
Concept and explanation
Python executes statements in order. Names reference values; expressions produce values; conditions choose paths; loops repeat bounded work; functions package inputs, computation and returned outputs. Scientific code must preserve units, assumptions and tests.
Key terms
value; variable; type; expression; assignment; condition; loop; function; parameter; return; exception
Syntax / computational model
Use = for assignment and == for equality comparison. Indentation defines blocks. A function receives parameters and returns a value; print displays but does not return.
Learning objectives
- Use numeric values, variables, expressions, conditions, loops and functions.
- Distinguish syntax, runtime and logical/scientific errors through tests.
Code examples, expected output and validation
Code example 1
Problem / input: Convert Celsius to kelvin with a reusable tested function.
def celsius_to_kelvin(c):
return c + 273.15
t = celsius_to_kelvin(25.0)
print(round(t, 2))Expected output:
298.15Why the code works: The argument 25.0 binds to c; return produces a float. The conversion assumes Celsius input and kelvin output.
Test / validation / limitations: Check 0 °C → 273.15 K and reject physically impossible interpretations below absolute zero at the model boundary.
Code example 2
Problem / input: Compute the mean of three readings with a loop and count.
readings = [2.0, 2.5, 3.0]
total = 0.0
for value in readings:
total += value
mean = total / len(readings)
print(mean)Expected output:
2.5Why the code works: The accumulator starts at zero, adds each reading once and divides by the number of readings.
Test / validation / limitations: Hand-check 7.5/3=2.5; also test a single reading and define behavior for an empty list.
Common bug and counterexample
Bug: confusing assignment/comparison or returning nothing. A function that only prints yields None when another calculation expects a number.
Buggy code
def square(x):
print(x * x)
y = square(3) + 1 # TypeErrorCorrected and tested code
def square(x):
return x * x
y = square(3) + 1
assert y == 10Guided coding practice
- Read Example 1, predict its output and trace every variable, type and shape before comparing with the expected output.
- Modify one input or parameter, predict the consequence, design a test and explain the scientific limitation.
A/B/C/D coding practice
A · Foundation
Trace types and values in a temperature calculation.
B · Application
Write a tested function for a unit conversion.
C · Debugging and error detection
Debug indentation, NameError and missing return defects.
D · Challenge / transfer
Design boundary tests and explain scientific assumptions.
Knowledge check and summary
- Predict the output before checking the expected result.
- Trace values, types and shapes through each statement.
- Name one authentic bug and test its correction.
- Explain why successful execution is not scientific validation.
Mastery criterion: 4/4 correct plus one independently tested code artifact or equivalent verified trace. Correct each defect and complete a fresh equivalent check.
Related laboratory
Next class: NumPy arrays