{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "277a996c-5674-451b-b22f-647b7766c5d3",
   "metadata": {},
   "source": [
    "3) вещественное число с плавающей точкой `e` и `E`\n",
    "\n",
    "`print('текст %e текст' %значение)`\n",
    "\n",
    "`print('текст %[кол-во позиций.кол-во цифр после запятой в мантиссе]e текст' %значение)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "271df5aa-28a1-4419-a633-de66d17602d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 2.362156226366"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4ee8c1c3-1a60-4177-8742-ae1a5a65b08c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.362156e+00\n"
     ]
    }
   ],
   "source": [
    "print('%e' %x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e867330c-acdb-4236-b62d-8b2c260b46b8",
   "metadata": {},
   "source": [
    "В Python мантисса принимает занчения в $[1; 10)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "26b230be-d9c7-4abd-9f13-46fa925ce0b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    2.36e+00\n"
     ]
    }
   ],
   "source": [
    "print('%12.2e' %x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "32333e0a-42f7-4fad-9b4a-2b4dc7028329",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     2.4e+00\n"
     ]
    }
   ],
   "source": [
    "print('%12.1e' %x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "41ddf693-e26d-4fd6-a15f-c5e08ea3819b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-12 45.89\n",
      "-12 : 45.89\n"
     ]
    }
   ],
   "source": [
    "# опция sep (разделитель); sep=' ' по умолчание\n",
    "\n",
    "a, z = -12, 45.89\n",
    "print(a, z)\n",
    "print(a, z, sep=' : ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d7aded5d-c3cb-4c5d-8481-30c22d432068",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-12\n",
      "45.89\n"
     ]
    }
   ],
   "source": [
    "# опция end (конец печати); end='\\n' по умолчание\n",
    "\n",
    "print(a)\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f58d74a0-6191-4a4f-9b57-974037e6c47b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-12Hello!45.89\n"
     ]
    }
   ],
   "source": [
    "print(a, end='Hello!')\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d661bd6-8024-4bec-83a9-1cbff2dc5243",
   "metadata": {},
   "source": [
    "4) форматированный вывод строки `s`\n",
    "\n",
    "`print('текст %s текст' %значение)`\n",
    "\n",
    "`print('текст %[кол-во позиций]s текст' %значение)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2743148c-582f-4055-9316-ec3c85d5627e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Мики Маус\n"
     ]
    }
   ],
   "source": [
    "s1 = 'Мики'\n",
    "s2 = 'Маус'\n",
    "\n",
    "print('%s %s' %(s1, s2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9da10da1-b97a-4250-80d5-efc4aa33f6b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Мики     Маус\n"
     ]
    }
   ],
   "source": [
    "print('%5s %8s' %(s1, s2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e55e150c-07e7-4511-9549-032d51dd944d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "t = 4 > 5\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f54c2481-ae3a-492c-a594-a34c4dd76c45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " False\n"
     ]
    }
   ],
   "source": [
    "print('%6s' %t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1b537a58-bb75-4555-a217-e9f64d3eee38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Регист не важен в Python?   False\n"
     ]
    }
   ],
   "source": [
    "r = 'e' == 'E'\n",
    "print('Регист не важен в Python? %7s' %r)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55a6626f-4fac-4b2d-a8dd-7c299e68ac60",
   "metadata": {},
   "source": [
    "# Функции округления\n",
    "\n",
    "1) `round(x[, n])`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "06a5f6ab-2553-4891-96b9-42c93a445a56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(2.0392145)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f0ae3c63-9155-46e8-8c7f-43423f1a2b2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(2.92145)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "dbf0287e-25b5-4a8f-aba7-744f0806b1d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(2.5)  # банковское округление "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "bb59499a-3329-4ed4-aaa4-9c33f4610244",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.04"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(2.0392145, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6b60fb5a-d399-4982-969c-dcfd51ef1395",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.04"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(2.035, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e932e046-5708-4c57-90b6-72ff1df4282f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.02"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(2.025, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2f33b871-dae2-4fcd-b1ed-ebd8e96a0038",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(2.0392145, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c73748cb-e7cb-4048-9517-a99445bdf633",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1090.0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(1092.0392145, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4f31c361-d2f7-4977-9c62-52ed18cfe4ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1100.0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(1092.0392145, -2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f13563df-be41-46b4-b624-bb4a1e3ffba9",
   "metadata": {},
   "source": [
    "2) `int()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5d68836d-acce-45ba-895d-0d7d086742b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(2.01283)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "3718f651-fd44-4062-b613-cddbb6d999ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(2.901283)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbec0bd3-7986-453a-b572-cf91addd928c",
   "metadata": {},
   "source": [
    "3) `math.trunc()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "074a6b10-80a0-45ae-8eb9-90da9c14e2a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-4"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "math.trunc(-4.56)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "383255b2-7264-4a3e-9341-ef97c62faf71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-11"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.trunc(-11.34)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c17e0ad-c616-499c-b263-1df40e068f86",
   "metadata": {},
   "source": [
    "4) `math.ceil()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "803dfaad-cf3b-45b5-a849-2b5ffb4b867c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.ceil(3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e63497f2-631e-4d98-b7a1-5fd3e9c3a90f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-6"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.ceil(-6.7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "add345c8-853a-4bf7-b90b-dcdc8be6311f",
   "metadata": {},
   "source": [
    "5) `math.floor()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "2fdc30e7-83f4-4c77-ba3a-b97d94eeccc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "56"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.floor(56.65)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b8e01a40-0942-40ac-add7-24572873cbf7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.floor(0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a90963b7-cd16-4122-8ec6-cc253fa0856c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.30000000000000004"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# пример \"некорректного\" вычисления\n",
    "\n",
    "0.1 + 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "0bd63833-5e96-464d-b2d2-253d6e5aba75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.3\n"
     ]
    }
   ],
   "source": [
    "from decimal import Decimal as D\n",
    "\n",
    "x = D('0.1')\n",
    "y = D('0.2')\n",
    "s = x + y\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a752464d-e47e-40d4-beca-15c0043bbafc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "decimal.Decimal"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "57f6b6fe-07de-4f3d-b604-cfdb5c1ef082",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "decimal.Decimal"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "76a808b9-ff9f-4110-b67b-0f13af2fb811",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.3\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "s2 = float(s)\n",
    "print(s2)\n",
    "print(type(s2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9b6541cd-9342-4d16-badd-93d2b13db5c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "q = -17.13652351200452587\n",
    "p = D(str(q))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "274bc6b6-4237-4e0b-a940-4e0e35caa359",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-17.1\n"
     ]
    }
   ],
   "source": [
    "print(p.quantize(D('0.0')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f715880c-e65e-44e9-9485-ba217f75a5d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-17.14\n"
     ]
    }
   ],
   "source": [
    "print(p.quantize(D('0.00')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "e25bdba5-40c1-4cdf-819e-8e75315dda66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-17.137\n"
     ]
    }
   ],
   "source": [
    "print(p.quantize(D('0.000')))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
