{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "59e6b43f",
   "metadata": {},
   "source": [
    "# 11-08 · Словари\n",
    "\n",
    "Практика к разделу [«Словари»](../../site/chapters/glava-11/11-08-slovari.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deccf711",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить создание, изменение и перебор словарей."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51da5dea",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "11436217",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:55:47.236765Z",
     "iopub.status.busy": "2026-08-14T20:55:47.236582Z",
     "iopub.status.idle": "2026-08-14T20:55:47.253623Z",
     "shell.execute_reply": "2026-08-14T20:55:47.253165Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cartesian\n"
     ]
    }
   ],
   "source": [
    "student = {\"name\": \"Cartesian\", \"age\": 12, \"city\": \"Москва\"}\n",
    "print(student[\"name\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c52cd67",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — изменение"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7794f6cc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:55:47.255062Z",
     "iopub.status.busy": "2026-08-14T20:55:47.254926Z",
     "iopub.status.idle": "2026-08-14T20:55:47.257263Z",
     "shell.execute_reply": "2026-08-14T20:55:47.256837Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Cartesian', 'age': 13, 'city': 'Москва', 'grade': '7 класс'}\n"
     ]
    }
   ],
   "source": [
    "student[\"age\"] = 13\n",
    "student[\"grade\"] = \"7 класс\"\n",
    "print(student)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2aa3ff75",
   "metadata": {},
   "source": [
    "## Эксперимент 2 — перебор"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b6b31201",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:55:47.258370Z",
     "iopub.status.busy": "2026-08-14T20:55:47.258151Z",
     "iopub.status.idle": "2026-08-14T20:55:47.260502Z",
     "shell.execute_reply": "2026-08-14T20:55:47.260141Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name: Cartesian\n",
      "age: 13\n",
      "city: Москва\n",
      "grade: 7 класс\n"
     ]
    }
   ],
   "source": [
    "for key, value in student.items():\n",
    "    print(f\"{key}: {value}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be988920",
   "metadata": {},
   "source": [
    "## Типичная ошибка\n",
    "\n",
    "Обращение к несуществующему ключу вызывает KeyError."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3afee648",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:55:47.261627Z",
     "iopub.status.busy": "2026-08-14T20:55:47.261514Z",
     "iopub.status.idle": "2026-08-14T20:55:47.287492Z",
     "shell.execute_reply": "2026-08-14T20:55:47.286980Z"
    },
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'phone'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mKeyError\u001b[39m                                  Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m student[\u001b[33m\"phone\"\u001b[39m]\n",
      "\u001b[31mKeyError\u001b[39m: 'phone'"
     ]
    }
   ],
   "source": [
    "student[\"phone\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b8fee72",
   "metadata": {},
   "source": [
    "## Исправление\n",
    "\n",
    "`.get()` безопасно возвращает None вместо ошибки."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ed7edca1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:55:47.288583Z",
     "iopub.status.busy": "2026-08-14T20:55:47.288472Z",
     "iopub.status.idle": "2026-08-14T20:55:47.290839Z",
     "shell.execute_reply": "2026-08-14T20:55:47.290361Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "не указан\n"
     ]
    }
   ],
   "source": [
    "print(student.get(\"phone\"))\n",
    "print(student.get(\"phone\", \"не указан\"))  # с значением по умолчанию"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61c42b8b",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Создайте словарь с тремя любимыми фильмами и годами их выхода, выведите каждую пару."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "46366140",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:55:47.291829Z",
     "iopub.status.busy": "2026-08-14T20:55:47.291717Z",
     "iopub.status.idle": "2026-08-14T20:55:47.294585Z",
     "shell.execute_reply": "2026-08-14T20:55:47.294101Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Матрица — 1999\n",
      "Начало — 2010\n",
      "Интерстеллар — 2014\n"
     ]
    }
   ],
   "source": [
    "films = {\"Матрица\": 1999, \"Начало\": 2010, \"Интерстеллар\": 2014}\n",
    "for name, year in films.items():\n",
    "    print(f\"{name} — {year}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
