{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Decisions, Loops, and Iteration\n",
        "\n**Opening question:** How can a script apply a rule repeatedly while remaining easy to inspect?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "returns = [0.02, -0.01, 0.0]\n",
        "for r in returns:\n",
        "    if r > 0:\n",
        "        label = \"gain\"\n",
        "    elif r < 0:\n",
        "        label = \"loss\"\n",
        "    else:\n",
        "        label = \"flat\"\n",
        "    print(label)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The zero case is handled explicitly rather than being absorbed into gain or loss.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "months = [\"Jan\", \"Feb\", \"Mar\"]\n",
        "inflation = [2.9, 2.8, 2.7]\n",
        "for position, (month, rate) in enumerate(zip(months, inflation), start=1):\n",
        "    print(position, month, rate)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. zip assumes the sequences align. In real data, joins based on keys are usually safer than positional alignment.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(label)\n",
        "print(position, month, rate)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\ngain loss flat\n```\n\n```text\n1 Jan 2.9 2 Feb 2.8 3 Mar 2.7\n```\n\n```text\ngain\nloss\nflat\n```\n\n```text\n1 Jan 2.9\n2 Feb 2.8\n3 Mar 2.7\n```\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3"
    },
    "ceteris_lab": {
      "course_slug": "fundamentals-python-econometrics",
      "source_derived": true,
      "course_title": "Fundamentals of Python for Financial Econometrics",
      "chapter": 5,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
