Skip to Academy content

Level II · Optional · Open anytime

Intermediate Python for Research

Build on Level I syntax and pandas basics to make small research workflows reliable. Read each self-contained example, predict its output, then use the quick checks to test your reasoning. We move from functions and NumPy to labeled data, time-aware joins, validation, and reproducibility. No installation is needed to read or answer; optional execution uses your own Python environment with NumPy and pandas. Examples target Python 3.10+ and public APIs compatible with pandas 2.3/3.0; Copy-on-Write discussion identifies pandas 3.0 behavior.

Study any section in any order. These checks do not affect your core course or assessments. Choice answers and elective progress are tracked in this browser.

Need a refresher? Review Level I python in a new tab.

Try six coding tasks with separate solutions

Read the code, predict the result, then check your reasoning. Each example has revealable output. To run or edit the snippets, use your own Python environment.

Optional practice · Browser progress0/4 checks answered
Python elective0% complete
0%

0/2 sections viewed · 0/4 exercises answered

Python · Reliable building blocks · Section 1 of 2

Design functions with explicit contracts

A research function should state its inputs, output, units, and invalid cases. A type annotation such as list[float] documents intent and helps tools, but Python does not automatically enforce it at runtime. A docstring can explain conventions such as fractional returns and cost per traded dollar.

Keyword-only parameters follow * in a signature. They make calls such as net_return(0.02, turnover=0.5, cost=0.001) easier to review than a row of unexplained numbers. A pure calculation returns a result without changing external state or its input containers.

Avoid mutable default arguments such as history=[] because the default object is shared across calls. Use None and create a new container inside the function. Validate nonfinite or out-of-domain inputs deliberately instead of silently converting them into apparently valid output.

Python example · read it, predict the output, then reveal or run it
import math

def net_return(gross: float, *, turnover: float, cost: float) -> float:
    """Return gross minus traded-notional costs, in fractional units."""
    if not all(math.isfinite(x) for x in (gross, turnover, cost)):
        raise ValueError("inputs must be finite")
    if turnover < 0 or cost < 0:
        raise ValueError("turnover and cost must be nonnegative")
    return gross - turnover * cost

print(f"{net_return(0.02, turnover=0.5, cost=0.001):.4f}")
Reveal the expected output
0.0195
Loading the full section…

Keep in mind: A small explicit function is easier to test and review than a hidden calculation.

Link to this sectionSearchGlossary & formulas ↗

Pause and try two quick checks.

You can move on at any time. Each answer explains the reasoning.

Quick check · Python · Reliable building blocks

Does a parameter annotation x: float automatically reject a string at runtime?

Quick check · Python · Reliable building blocks

Why should you avoid a default argument history=[]?

Further reading and documentation

You are previewing 2 of 22 sections

The full elective is included with Level II enrollment, together with the core course and the official examination.

Go further · Optional resources

Build your mathematics, statistics, and Python toolkit.

Open these resources at any time. They are separate from the core modules and do not count toward examination eligibility, practice scores, or certification. Programming remains optional.

Ready to explore

Calculus I & II

Study single-variable calculus with worked steps, visual explorations, and immediate-feedback checks: from limits and derivatives to integrals, series, and approximation error, closing with mental math techniques.

Open the optional resource →

Ready to explore

Applied Descriptive Statistics

Compare research samples using weighted summaries, robust location and scale, within- and between-group dispersion, conditional tables, and consistent time windows.

Open the optional resource →

Ready to explore

Intermediate Python for Research

Build reliable research workflows with explained code examples, revealable output, and interactive checks on NumPy, pandas, timing, validation, and reproducibility.

Open the optional resource →