Science SchoolAdministration

E-TRADE TOGETHER GLOBAL ACADEMY

Python for Scientific Computing

Level 7 · Scientific Computing · Prerequisite: Course 0.2; basic programming helpful · Connects to Computing & Software Engineering

ACADEMICALLY & TECHNICALLY VERIFIED · PYTHON RUNTIME NOT VERIFIED · MOBILE QA ENVIRONMENT BLOCKED

7 canonical classes · 4 laboratories · 3 module assessments · programming project · final

Administrator review mode

No payment, enrollment or prerequisite restriction. Review content is available only to authorized administrators.

UI/UX authority: current Mathematical Foundations implementation. Rendered-content verification requires format parity.

MODULE 1

Python & Array Computing

Build readable, testable scientific Python calculations and reason explicitly about array values, types, shapes, axes and vectorized operations.

Execution mode: READ → PREDICT → TRACE → MODIFY → DEBUG → TEST → VALIDATE → EXPLAIN. No verified Python runtime is available.

7.1.1 · v1 · 90 minutes

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.15

Why 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.5

Why 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  # TypeError

Corrected and tested code

def square(x):
    return x * x
y = square(3) + 1
assert y == 10

Guided coding practice

  1. Read Example 1, predict its output and trace every variable, type and shape before comparing with the expected output.
  2. 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

  1. Predict the output before checking the expected result.
  2. Trace values, types and shapes through each statement.
  3. Name one authentic bug and test its correction.
  4. 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