{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6fa703a2",
   "metadata": {},
   "source": [
    "# 04-04 · Преобразование типов чисел\n",
    "\n",
    "Практика к разделу [«Преобразование типов чисел»](../../site/chapters/glava-04/04-04-preobrazovanie-tipov.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06e67e18",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить `int()`, `float()`, `str()` и их подводные камни."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bc9af87",
   "metadata": {},
   "source": [
    "## Что нужно знать\n",
    "\n",
    "Каждый числовой тип — одновременно и функция-преобразователь."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5aa63b78",
   "metadata": {},
   "source": [
    "## Краткое напоминание\n",
    "\n",
    "`int()` **отбрасывает** дробную часть, а не округляет."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14f862c4",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fcb2641a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:15.128365Z",
     "iopub.status.busy": "2026-08-14T20:52:15.128222Z",
     "iopub.status.idle": "2026-08-14T20:52:15.145429Z",
     "shell.execute_reply": "2026-08-14T20:52:15.145014Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "7.0\n",
      "42 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(int(3.99))\n",
    "print(float(7))\n",
    "print(str(42), type(str(42)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9580766f",
   "metadata": {},
   "source": [
    "## Эксперимент 1\n",
    "\n",
    "Сравните `int(3.99)` и `int(-3.99)` — куда «отбрасывается» дробная часть у отрицательного числа?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "400ffedf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:15.146651Z",
     "iopub.status.busy": "2026-08-14T20:52:15.146527Z",
     "iopub.status.idle": "2026-08-14T20:52:15.148863Z",
     "shell.execute_reply": "2026-08-14T20:52:15.148412Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "-3\n"
     ]
    }
   ],
   "source": [
    "print(int(3.99))\n",
    "print(int(-3.99))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "094c45ff",
   "metadata": {},
   "source": [
    "## Эксперимент 2\n",
    "\n",
    "Преобразуйте текст `\"25\"` в число и прибавьте к нему 5."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f0856d7c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:15.150181Z",
     "iopub.status.busy": "2026-08-14T20:52:15.150054Z",
     "iopub.status.idle": "2026-08-14T20:52:15.152748Z",
     "shell.execute_reply": "2026-08-14T20:52:15.152265Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n"
     ]
    }
   ],
   "source": [
    "age_text = \"25\"\n",
    "age = int(age_text)\n",
    "print(age + 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03612544",
   "metadata": {},
   "source": [
    "## Типичная ошибка\n",
    "\n",
    "Не любой текст можно превратить в число."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "59affa36",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:15.153722Z",
     "iopub.status.busy": "2026-08-14T20:52:15.153610Z",
     "iopub.status.idle": "2026-08-14T20:52:15.182510Z",
     "shell.execute_reply": "2026-08-14T20:52:15.181972Z"
    },
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'десять'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mValueError\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 int(\u001b[33m\"десять\"\u001b[39m)\n",
      "\u001b[31mValueError\u001b[39m: invalid literal for int() with base 10: 'десять'"
     ]
    }
   ],
   "source": [
    "int(\"десять\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25204358",
   "metadata": {},
   "source": [
    "## Исправление\n",
    "\n",
    "Преобразовывать можно только текст, который действительно выглядит как число."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "13ed83bc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:15.183720Z",
     "iopub.status.busy": "2026-08-14T20:52:15.183595Z",
     "iopub.status.idle": "2026-08-14T20:52:15.187019Z",
     "shell.execute_reply": "2026-08-14T20:52:15.186557Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(\"10\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "504f424b",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Исправьте ошибку: соберите строку `\"Итого: 100\"` из текста `\"Итого: \"` и числа `100` — сначала через `str()`, затем через f-строку."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8434ce30",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:52:15.187928Z",
     "iopub.status.busy": "2026-08-14T20:52:15.187820Z",
     "iopub.status.idle": "2026-08-14T20:52:15.190254Z",
     "shell.execute_reply": "2026-08-14T20:52:15.189577Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Итого: 100\n",
      "Итого: 100\n"
     ]
    }
   ],
   "source": [
    "total = 100\n",
    "print(\"Итого: \" + str(total))\n",
    "print(f\"Итого: {total}\")"
   ]
  }
 ],
 "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
}
