{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b0db29bc",
   "metadata": {},
   "source": [
    "# 08-03 · Индексы, срезы и сравнение строк\n",
    "\n",
    "Практика к разделам [«Доступ к символам строки»](../../site/chapters/glava-08/08-03-dostup-k-simvolam.html) и [«Истина? Ложь?»](../../site/chapters/glava-08/08-05-istina-lozh.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ab1e4ea",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить индексы, отрицательные индексы, срезы и сравнение строк."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "985f9c2f",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ccb31666",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:47.304014Z",
     "iopub.status.busy": "2026-08-14T20:53:47.303843Z",
     "iopub.status.idle": "2026-08-14T20:53:47.325516Z",
     "shell.execute_reply": "2026-08-14T20:53:47.325080Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P\n",
      "n\n",
      "Pyt\n",
      "nohtyP\n"
     ]
    }
   ],
   "source": [
    "word = \"Python\"\n",
    "print(word[0])\n",
    "print(word[-1])\n",
    "print(word[0:3])\n",
    "print(word[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc406323",
   "metadata": {},
   "source": [
    "## Эксперимент 1\n",
    "\n",
    "Проверьте, что `word[::-1]` действительно разворачивает строку для нескольких разных слов."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "50d1c1d5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:47.326668Z",
     "iopub.status.busy": "2026-08-14T20:53:47.326549Z",
     "iopub.status.idle": "2026-08-14T20:53:47.328891Z",
     "shell.execute_reply": "2026-08-14T20:53:47.328448Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python -> nohtyP\n",
      "Cartesian -> naisetraC\n",
      "мандала -> аладнам\n"
     ]
    }
   ],
   "source": [
    "for w in [\"Python\", \"Cartesian\", \"мандала\"]:\n",
    "    print(w, \"->\", w[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "248e4e63",
   "metadata": {},
   "source": [
    "## Эксперимент 2 — сравнение и in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6b4c7678",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:47.329823Z",
     "iopub.status.busy": "2026-08-14T20:53:47.329715Z",
     "iopub.status.idle": "2026-08-14T20:53:47.331947Z",
     "shell.execute_reply": "2026-08-14T20:53:47.331560Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "language = \"Python\"\n",
    "print(language == \"python\")\n",
    "print(\"thon\" in language)\n",
    "print(\"Java\" in language)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d39df5bb",
   "metadata": {},
   "source": [
    "## Типичная ошибка\n",
    "\n",
    "Индекс за пределами строки вызывает `IndexError`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "24e880b6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:47.332917Z",
     "iopub.status.busy": "2026-08-14T20:53:47.332814Z",
     "iopub.status.idle": "2026-08-14T20:53:47.359702Z",
     "shell.execute_reply": "2026-08-14T20:53:47.359126Z"
    },
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mIndexError\u001b[39m                                Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m word = \u001b[33m\"Python\"\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m word[\u001b[32m10\u001b[39m]\n",
      "\u001b[31mIndexError\u001b[39m: string index out of range"
     ]
    }
   ],
   "source": [
    "word = \"Python\"\n",
    "word[10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1b97180",
   "metadata": {},
   "source": [
    "## Исправление\n",
    "\n",
    "Проверяйте длину строки через `len()`, если не уверены в границах."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c6ba0d45",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:47.361043Z",
     "iopub.status.busy": "2026-08-14T20:53:47.360923Z",
     "iopub.status.idle": "2026-08-14T20:53:47.363334Z",
     "shell.execute_reply": "2026-08-14T20:53:47.362869Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "Индекс 10 вне диапазона — строка короче.\n"
     ]
    }
   ],
   "source": [
    "word = \"Python\"\n",
    "print(len(word))\n",
    "if len(word) > 10:\n",
    "    print(word[10])\n",
    "else:\n",
    "    print(\"Индекс 10 вне диапазона — строка короче.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71d4e3df",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Достаньте первые три и последние три символа слова «Cartesian» срезами."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5e5aa5ba",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:47.364322Z",
     "iopub.status.busy": "2026-08-14T20:53:47.364195Z",
     "iopub.status.idle": "2026-08-14T20:53:47.366330Z",
     "shell.execute_reply": "2026-08-14T20:53:47.365911Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Car\n",
      "ian\n"
     ]
    }
   ],
   "source": [
    "word = \"Cartesian\"\n",
    "print(word[:3])\n",
    "print(word[-3:])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b4773cf",
   "metadata": {},
   "source": [
    "## Проверка результата"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "672aa0e1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:47.367229Z",
     "iopub.status.busy": "2026-08-14T20:53:47.367124Z",
     "iopub.status.idle": "2026-08-14T20:53:47.369590Z",
     "shell.execute_reply": "2026-08-14T20:53:47.369166Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Оба среза верны.\n"
     ]
    }
   ],
   "source": [
    "assert \"Cartesian\"[:3] == \"Car\"\n",
    "assert \"Cartesian\"[-3:] == \"ian\"\n",
    "print(\"Оба среза верны.\")"
   ]
  }
 ],
 "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
}
