{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Python Data Structures: Lists, Tuples, Dictionaries, and Sets\n",
        "\n",
        "This is part 2 of the Python Fundamentals series. Complete the preceding tutorial before starting.\n",
        "\n",
        "Run each cell in order."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Lists\n",
        "\n",
        "Lists are ordered, mutable collections that allow duplicate values. Use append to add an item and remove to delete the first matching value."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "scores = [0.8, 0.9, 0.85]\n",
        "scores.append(0.95)\n",
        "scores.remove(0.8)\n",
        "print(scores)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Indexing and slicing\n",
        "\n",
        "Index zero selects the first element; negative indices count from the end. A slice includes its start but excludes its stop. The third slice argument controls the step."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "values = [10, 20, 30, 40, 50]\n",
        "print(values[0], values[-1])\n",
        "print(values[1:4], values[::2], values[::-1])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Mutability and copying\n",
        "\n",
        "Assignment does not copy a list. Two names can refer to the same object. A shallow copy creates a new outer list, but nested mutable objects remain shared."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "original = [1, 2, 3]\n",
        "alias = original\n",
        "copy = original.copy()\n",
        "alias.append(4)\n",
        "print(original, copy)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Tuples\n",
        "\n",
        "Tuples are ordered collections whose element references cannot be reassigned. A single-element tuple needs a trailing comma. Unpacking assigns elements to separate names."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "shape = (512, 512)\n",
        "height, width = shape\n",
        "print(height, width, (1,))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Dictionaries\n",
        "\n",
        "Dictionaries associate unique hashable keys with values. They preserve insertion order in modern Python. Access a known key with brackets or use get with a default for missing keys."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "model = {\"name\": \"baseline\", \"accuracy\": 0.91}\n",
        "model[\"accuracy\"] = 0.93\n",
        "print(model[\"name\"], model.get(\"loss\", \"Unavailable\"))\n",
        "print(list(model.items()))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Sets\n",
        "\n",
        "Sets store unique hashable elements and do not support positional indexing. Union combines elements, intersection finds common elements, and difference finds elements exclusive to the first set."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "train = {1, 2, 3}\n",
        "test = {3, 4, 5}\n",
        "print(sorted(train | test))\n",
        "print(sorted(train & test))\n",
        "print(sorted(train - test))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Comprehensions\n",
        "\n",
        "Comprehensions construct collections from iterable values. An optional condition filters the input. The expression before for determines each output value."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "values = [1, 2, 3, 4, 5]\n",
        "print([x ** 2 for x in values if x % 2 == 0])\n",
        "print({x: x ** 2 for x in values})"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Practical example: dataset records\n",
        "\n",
        "A list of dictionaries can represent a small dataset. Each dictionary stores one observation. Extract a column using a comprehension and calculate its mean."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "records = [{\"id\": 1, \"score\": 0.8}, {\"id\": 2, \"score\": 0.9}]\n",
        "scores = [row[\"score\"] for row in records]\n",
        "print(sum(scores) / len(scores))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Practice exercises\n",
        "\n",
        "Try these before viewing the solutions."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Remove duplicate values from [1, 2, 2, 3] and return a sorted list."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create a dictionary mapping numbers 1 through 4 to their squares."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Exercise solutions"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "print(sorted(set([1, 2, 2, 3])))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "print({x: x ** 2 for x in range(1, 5)})"
      ],
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}