continue w/ lesson 02
This commit is contained in:
@@ -59,6 +59,230 @@
|
||||
"source": [
|
||||
"Как можно заметить, мы потеряли дробную часть и осталась только целая, так как `int` - целочисленный тип данных."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1597b1ae-07ad-4459-813f-5d48ac849e38",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Посмотрим на еще один пример."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c19834b9-afa0-4fd5-a735-b1631d4b2743",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"age = 25\n",
|
||||
"\n",
|
||||
"print(f\"age's type before typecasting: {type(age)}\")\n",
|
||||
"print(f\"age's value before typecasting: {age}\")\n",
|
||||
"\n",
|
||||
"age = float(age) # приводим переменную типа int к типу float\n",
|
||||
"\n",
|
||||
"print(f\"age's type after typecasting: {type(age)}\")\n",
|
||||
"print(f\"age's value after typecasting: {age}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4378e9a5-d7b1-4a97-b359-fe1dd78fc392",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Как можно заметить, у значения `age` появилась дробная часть."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3aad7d13-d44a-44fa-81b2-10fa2a91cfdf",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"И еще один пример."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "779cdd3d-6bdd-4a02-8fc8-d75ab69aae6c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"age's type before typecasting: <class 'int'>\n",
|
||||
"age's value before typecasting: 25\n",
|
||||
"age's type after typecasting: <class 'str'>\n",
|
||||
"age's value after typecasting: 25\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"age = 25\n",
|
||||
"\n",
|
||||
"print(f\"age's type before typecasting: {type(age)}\")\n",
|
||||
"print(f\"age's value before typecasting: {age}\")\n",
|
||||
"\n",
|
||||
"age = str(age) # приводим переменную типа int к типу str\n",
|
||||
"\n",
|
||||
"print(f\"age's type after typecasting: {type(age)}\")\n",
|
||||
"print(f\"age's value after typecasting: {age}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7cfbb2d1-9655-4da1-ab27-3d79197356b2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Видно, что значение как будто не поменялось, но функция `type()` показывает, что тип переменной `age` стал другой. Забегая вперед, мы можем увидеть что `age` потеряла свои \"целочисленные свойства\", например сложив ее с каким-нибудь числом."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "968bb924-7a08-4c2d-9eef-72c73f5472d0",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"age + 1 = 26\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "TypeError",
|
||||
"evalue": "can only concatenate str (not \"int\") to str",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[6], line 14\u001b[0m\n\u001b[1;32m 8\u001b[0m age \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(age) \u001b[38;5;66;03m# приводим переменную типа int к типу str\u001b[39;00m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# print(f\"age's type after typecasting: {type(age)}\")\u001b[39;00m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# print(f\"age's value after typecasting: {age}\")\u001b[39;00m\n\u001b[1;32m 12\u001b[0m \n\u001b[1;32m 13\u001b[0m \u001b[38;5;66;03m# программа выдаст ошибку (мы не можем к строке прибавить число)\u001b[39;00m\n\u001b[0;32m---> 14\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mage + 1 = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[43mage\u001b[49m\u001b[38;5;250;43m \u001b[39;49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[38;5;250;43m \u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
|
||||
"\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"age = 25\n",
|
||||
"\n",
|
||||
"# print(f\"age's type before typecasting: {type(age)}\")\n",
|
||||
"# print(f\"age's value before typecasting: {age}\")\n",
|
||||
"\n",
|
||||
"print(f\"age + 1 = {age + 1}\") # все правильно (получаем 26)\n",
|
||||
"\n",
|
||||
"age = str(age) # приводим переменную типа int к типу str\n",
|
||||
"\n",
|
||||
"# print(f\"age's type after typecasting: {type(age)}\")\n",
|
||||
"# print(f\"age's value after typecasting: {age}\")\n",
|
||||
"\n",
|
||||
"# программа выдаст ошибку (мы не можем к строке прибавить число)\n",
|
||||
"print(f\"age + 1 = {age + 1}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "71070a79-5c22-44c1-b13e-3ed4708f300f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"То есть даже если визуальных различий нет, то это не значит, что переменные имеют одинаковый тип. Чаще всего тип той или иной переменной очевиден из контекста. Но давайте попробуем избавиться от ошибки и каким-нибудь образом прибавить `1` к age."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "52ae386b-d1db-4289-a5da-c965bd6e7b0e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"age + 1 = 26\n",
|
||||
"age + 1 = 251\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"age = 25\n",
|
||||
"\n",
|
||||
"# print(f\"age's type before typecasting: {type(age)}\")\n",
|
||||
"# print(f\"age's value before typecasting: {age}\")\n",
|
||||
"\n",
|
||||
"print(f\"age + 1 = {age + 1}\") # все правильно (получаем 26)\n",
|
||||
"\n",
|
||||
"age = str(age) # приводим переменную типа int к типу str\n",
|
||||
"\n",
|
||||
"# print(f\"age's type after typecasting: {type(age)}\")\n",
|
||||
"# print(f\"age's value after typecasting: {age}\")\n",
|
||||
"\n",
|
||||
"# используя кавычки мы говорим программе, что '1' - это тоже строка\n",
|
||||
"print(f\"age + 1 = {age + '1'}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d719f5bd-2faa-4264-b502-4724a6dfa18c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Видно, что во втором случае строка `age` просто \"приклеилась\" к строке `'1'` и получилось `251`. Эта операция \"сложения\" строк называется _конкатенация_."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "202ffb62-2806-4a8a-9660-c6694e7bc56b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Попробуем привести что-нибудь к типу `bool`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "c9bc2da5-6220-4b77-9626-10f42d1bd988",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"name's type before typecasting: <class 'str'>\n",
|
||||
"name's value before typecasting: vlad\n",
|
||||
"name's type after typecasting: <class 'bool'>\n",
|
||||
"name's value after typecasting: True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"name = \"vlad\"\n",
|
||||
"\n",
|
||||
"print(f\"name's type before typecasting: {type(name)}\")\n",
|
||||
"print(f\"name's value before typecasting: {name}\")\n",
|
||||
"\n",
|
||||
"name = bool(name) # приводим от str к bool\n",
|
||||
"\n",
|
||||
"print(f\"name's type after typecasting: {type(name)}\")\n",
|
||||
"print(f\"name's value after typecasting: {name}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "943d648a-4ebb-48a2-a194-43cf6dc7b320",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Мы получили `True`. На самом деле здесь все не так сложно. Просто любое ненулевое значение приводится к `True`, а нулевое к `False`. Для строки нулевое значение - пустая строка `\"\"`. Посмотрим на примерах."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "7c4e5ebd-aece-4d81-bfc1-21a1c6559b55",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(f\"\") # \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
Reference in New Issue
Block a user