Quant AtlasHomeMarketsAcademyAccount
Quant Atlas · quant-atlas.com
HomeMarketsAcademyAccount
Level II
  1. Academy
  2. Level II
  3. Intermediate Python for Research

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

  1. Python · Reliable building blocks

Choose a section · All are open
  1. Python · Reliable building blocks

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.

Loading the full section…

Make cost conventions visible

This toy function uses fractional returns, nonnegative traded-notional/capital turnover, and cost per traded dollar. A negative gross return is valid; a negative cost input is rejected.

    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

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

    A mistake to avoid

    Type hints do not replace runtime validation or a clear financial convention.

    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

    def f(x, history=[]): history.append(x); return history. What does the second call f(2) return, after f(1)?

    Quick check · Python · Reliable building blocks

    A function is def net(gross, turnover, cost). Which change best prevents callers from swapping turnover and cost?

    Further reading and documentation
    • Python tutorial: functions and control flow ↗
    • Python tutorial: modules ↗
    • Python tutorial: exceptions ↗
    • Python reference: dataclasses ↗
    • NumPy: array fundamentals ↗
    • NumPy: broadcasting ↗
    • NumPy: random generators and reproducibility ↗
    • pandas: combining tables and merge validation ↗
    • pandas: merge_asof availability-aware matching ↗
    • pandas: time series ↗
    • pandas: rolling and expanding windows ↗
    • pandas: Copy-on-Write ↗

    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.

    Enroll in Level II · $9.99All three levels · $24.99Already enrolled? Log in

    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 →