{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e52bc111",
   "metadata": {},
   "source": [
    "# 11-08 · Словари\n",
    "\n",
    "Практика к разделу [«Словари»](../../site/chapters/glava-11/11-08-slovari.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5166627",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить создание, изменение и перебор словарей."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a49afe1f",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dcf034ca",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T14:44:28.651210Z",
     "iopub.status.busy": "2026-08-17T14:44:28.651103Z",
     "iopub.status.idle": "2026-08-17T14:44:28.676564Z",
     "shell.execute_reply": "2026-08-17T14:44:28.676162Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cartesian\n"
     ]
    }
   ],
   "source": [
    "student = {\"name\": \"Cartesian\", \"age\": 12, \"city\": \"Москва\"}\n",
    "print(student[\"name\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c04301e",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — изменение"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6de5b7c9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T14:44:28.677755Z",
     "iopub.status.busy": "2026-08-17T14:44:28.677641Z",
     "iopub.status.idle": "2026-08-17T14:44:28.680146Z",
     "shell.execute_reply": "2026-08-17T14:44:28.679733Z"
    }
   },
   "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": "82a11ae8",
   "metadata": {},
   "source": [
    "## Эксперимент 2 — перебор"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "126cc0de",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T14:44:28.681092Z",
     "iopub.status.busy": "2026-08-17T14:44:28.680992Z",
     "iopub.status.idle": "2026-08-17T14:44:28.683091Z",
     "shell.execute_reply": "2026-08-17T14:44:28.682692Z"
    }
   },
   "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": "8554dca7",
   "metadata": {},
   "source": [
    "## Типичная ошибка\n",
    "\n",
    "Обращение к несуществующему ключу вызывает KeyError."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e1501df0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T14:44:28.683975Z",
     "iopub.status.busy": "2026-08-17T14:44:28.683875Z",
     "iopub.status.idle": "2026-08-17T14:44:28.709108Z",
     "shell.execute_reply": "2026-08-17T14:44:28.708677Z"
    },
    "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": "1e190ade",
   "metadata": {},
   "source": [
    "## Исправление\n",
    "\n",
    "`.get()` безопасно возвращает None вместо ошибки."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "498d76e8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T14:44:28.710388Z",
     "iopub.status.busy": "2026-08-17T14:44:28.710276Z",
     "iopub.status.idle": "2026-08-17T14:44:28.712530Z",
     "shell.execute_reply": "2026-08-17T14:44:28.712071Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "не указан\n"
     ]
    }
   ],
   "source": [
    "print(student.get(\"phone\"))\n",
    "print(student.get(\"phone\", \"не указан\"))  # с значением по умолчанию"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f17eda39",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Создайте словарь с тремя любимыми фильмами и годами их выхода, выведите каждую пару."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8121ab29",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T14:44:28.713551Z",
     "iopub.status.busy": "2026-08-17T14:44:28.713443Z",
     "iopub.status.idle": "2026-08-17T14:44:28.715780Z",
     "shell.execute_reply": "2026-08-17T14:44:28.715351Z"
    }
   },
   "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
}
