Skip to content

Instantly share code, notes, and snippets.

@mahenzon
Created April 3, 2025 07:36
Show Gist options
  • Save mahenzon/d79ddb5e29194c82844fcc5c14eb7a4a to your computer and use it in GitHub Desktop.
Save mahenzon/d79ddb5e29194c82844fcc5c14eb7a4a to your computer and use it in GitHub Desktop.
Python Basics | Lerna × Suren
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "0a63baac-0bd9-4a6a-b62a-d4fdc482cd50",
"metadata": {},
"source": [
"# Пример №1 - Условия"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f7c3384d-1b88-4892-a63f-32921a7ba365",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 > 2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f94023f7-6ade-4113-86a0-2407741eca98",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 is greater than 2\n"
]
}
],
"source": [
"if 5 > 2:\n",
" print(\"5 is greater than 2\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5adac3d0-a54f-43c4-b641-078251d32c09",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 is greater than 5 or equal to\n"
]
}
],
"source": [
"if 5 >= 5:\n",
" print(\"5 is greater than 5 or equal to\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "44fbbcb7-db2b-42c9-933d-d8035ac4c764",
"metadata": {},
"outputs": [],
"source": [
"if 1 > 2:\n",
" print(\"1 is greater than 2\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "82aefdef-1ca4-4627-99b5-c1cbb5907ee1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ok\n"
]
}
],
"source": [
"if 1 > 2:\n",
" print(\"1 is greater than 2\")\n",
"print(\"ok\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "27c97233-1410-4a28-889e-66c8918c05c8",
"metadata": {},
"outputs": [],
"source": [
"if 1 > 2:\n",
" print(\"1 is greater than 2\")\n",
" print(\"ok\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2f827816-d4ec-47de-ae35-cbcad07b7330",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 is not greater than 2\n"
]
}
],
"source": [
"if 1 > 2:\n",
" print(\"1 is greater than 2\")\n",
"else:\n",
" print(\"1 is not greater than 2\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "1769f536-b39f-4af6-80ff-a55cc446cdf8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 is not greater than 2\n"
]
}
],
"source": [
"if 2 > 2:\n",
" print(\"2 is greater than 2\")\n",
"else:\n",
" print(\"2 is not greater than 2\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e0497cee-26c1-4ef9-8d61-deccda1a6f4c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"excellent!\n",
"great!\n"
]
}
],
"source": [
"score = 90\n",
"if score >= 90:\n",
" print(\"excellent!\")\n",
"if score >= 80:\n",
" print(\"great!\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "4f9615f0-5cb4-4ff9-a995-919fdd2093a6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"excellent!\n"
]
}
],
"source": [
"score = 90\n",
"if score >= 90:\n",
" print(\"excellent!\")\n",
"elif score >= 80:\n",
" print(\"great!\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d8785396-7228-4331-8cf9-e652d76cdb9f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"great!\n"
]
}
],
"source": [
"score = 80\n",
"if score >= 90:\n",
" print(\"excellent!\")\n",
"elif score >= 80:\n",
" print(\"great!\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "663a3135-04d4-409f-891a-f086cbe93354",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"great!\n"
]
}
],
"source": [
"score = 80\n",
"if score >= 90:\n",
" print(\"excellent!\")\n",
"elif score >= 80:\n",
" print(\"great!\")\n",
"elif score >= 70:\n",
" print(\"good\")\n",
"else:\n",
" print(\"you could do better\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "10c324a7-d5c3-4bd1-be43-b5d95cab74a5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"good\n"
]
}
],
"source": [
"score = 70\n",
"if score >= 90:\n",
" print(\"excellent!\")\n",
"elif score >= 80:\n",
" print(\"great!\")\n",
"elif score >= 70:\n",
" print(\"good\")\n",
"else:\n",
" print(\"you could do better\")"
]
},
{
"cell_type": "markdown",
"id": "9af1fc0a-54ed-477f-a50d-68e353e035aa",
"metadata": {},
"source": [
"# Пример №2 - Функции"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "630227b0-7057-4fad-86f8-30c51fb062af",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ok\n"
]
}
],
"source": [
"def hello():\n",
" print(\"Hello World!\")\n",
"print(\"ok\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "f486ea89-b7cd-4211-9f91-f35394277fcd",
"metadata": {},
"outputs": [],
"source": [
"def hello():\n",
" print(\"Hello World!\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "bede021a-334f-42cd-ad50-d19e507917e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World!\n"
]
}
],
"source": [
"hello()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "ee0e26c4-6978-4972-a690-9f6fc719c3be",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World!\n"
]
}
],
"source": [
"hello()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "d3193e4e-710e-4724-a037-29e394b4342c",
"metadata": {},
"outputs": [],
"source": [
"def review_score():\n",
" score = 70\n",
" if score >= 90:\n",
" print(\"excellent!\")\n",
" elif score >= 80:\n",
" print(\"great!\")\n",
" elif score >= 70:\n",
" print(\"good\")\n",
" else:\n",
" print(\"you could do better\")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "e018d091-2b8c-451c-a43b-074e86b16129",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"good\n"
]
}
],
"source": [
"review_score()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "7c2c3b1e-2802-4cff-9e36-356ed0299fef",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"good\n"
]
}
],
"source": [
"review_score()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "1c193c24-5cfe-4078-af92-5bebd6f30afc",
"metadata": {},
"outputs": [],
"source": [
"def review_score(score):\n",
" if score >= 90:\n",
" print(\"excellent!\")\n",
" elif score >= 80:\n",
" print(\"great!\")\n",
" elif score >= 70:\n",
" print(\"good\")\n",
" else:\n",
" print(\"you could do better\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "507d9017-d4f4-47fe-a9fe-d74ce5d58871",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"great!\n"
]
}
],
"source": [
"review_score(80)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "935ed1e6-ce38-41c1-938f-32cda2eb884d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"great!\n"
]
}
],
"source": [
"review_score(89)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "3976c096-5022-4967-b845-b5633e6dfdba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"excellent!\n"
]
}
],
"source": [
"review_score(90)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "96497253-5120-4162-a0ca-f7ccca1de615",
"metadata": {},
"outputs": [],
"source": [
"def hello():\n",
" return 'hello'"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "fc8e34e0-3e94-4a45-84f9-ffeaff3eb672",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hello'"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hello()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "fc592bc7-d80e-41e7-a83c-03c3d6cd028b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
}
],
"source": [
"print(hello())"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "595dee5b-8760-4161-b6ee-333af7ee5dd0",
"metadata": {},
"outputs": [],
"source": [
"def greet(name, greeting=\"Hello\"):\n",
" return f\"{greeting}, {name}!\""
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "eec485f8-1c52-49d6-89e4-7e66bc252139",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello, Suren!'"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"greet(\"Suren\")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "4acc9498-8a9e-4c53-9209-d6cb40464d1c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, Suren!\n"
]
}
],
"source": [
"print(greet(\"Suren\"))"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "b99814f7-3ee5-4d65-afc3-ed0931a4b99e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hi, Bob!\n"
]
}
],
"source": [
"print(greet(\"Bob\", \"Hi\"))"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "60ffb00d-81a3-4404-b256-ddcc8abadad2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Good morning, Bob!\n"
]
}
],
"source": [
"print(greet(\"Bob\", greeting=\"Good morning\"))"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "2c1ea6f1-f75c-4411-be30-411ba2285e97",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"7 % 2"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "dab55526-b336-4d7e-9c69-95b253d5000a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"8 % 2"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "b934ef32-22f0-4f66-9ba1-060a560c15b6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(7 % 2)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "5597cda5-1207-4615-8d87-d02c45300c65",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(8 % 2)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "da4ac535-8c64-4ad2-8351-d073dfb5c28d",
"metadata": {},
"outputs": [],
"source": [
"def is_odd(num):\n",
" if num % 2:\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "9b7998f9-3741-4395-adb5-0913046261bb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_odd(7)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "6f4ad364-391b-4ce7-922e-668bc879dd09",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_odd(8)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "c9915c1b-70bf-4fbf-9a96-c5868e4b2ad3",
"metadata": {},
"outputs": [],
"source": [
"def is_odd(num):\n",
" return num % 2"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "f238047a-203f-44fe-b9a0-c8644694b022",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_odd(9)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "676671d1-06cb-4776-9bbc-910343775dc1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_odd(6)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "741d2f33-5be6-4e32-b577-69246c3c2ce1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7 is odd\n"
]
}
],
"source": [
"number = 7\n",
"if is_odd(number):\n",
" print(number, \"is odd\")"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "3ecfb5de-ad0c-4567-8d38-71ba870d7ee1",
"metadata": {},
"outputs": [],
"source": [
"def is_odd(num):\n",
" return bool(num % 2)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "f693b738-d759-4d03-bbe4-49f3f95d54a0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_odd(5)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "cfabed45-a858-42d0-848c-c6abb968b204",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_odd(4)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "b0ce7ce0-8d56-4cd1-8f5f-673433333dd9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(\"asdfads\")"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "09d18769-2fda-444a-b847-9102fa40664b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool('')"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "40393926-76c8-454a-a988-8e2ce0110b5f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool('0')"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "67b130a9-a79b-4055-8e42-3ef469af9942",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(0)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "533bf2ef-8c43-47b7-9307-ba34b37cfba0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(-1)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "95662624-2f2a-475d-a7f3-b323bc93346a",
"metadata": {},
"outputs": [],
"source": [
"def is_even(num):\n",
" return num % 2 == 0"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "900d953d-f9a2-406b-909a-3cf63343cd09",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_even(2)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "090fff69-69a5-428a-9aa0-379f086db534",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_even(5)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "6f4680f3-a667-4e27-bd38-40d82b90db0b",
"metadata": {},
"outputs": [],
"source": [
"def is_even(num):\n",
" return not num % 2"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "78aee04c-a1ba-4ca0-aaa6-c4d21c280d77",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not True"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "7ac27b7f-728d-400c-ad7a-3201c17d8409",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not 1"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "fcb64835-8ec8-4d7f-a8df-fe0d3650d7f1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not 0"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "159df018-b850-45c6-a8eb-d2fe228e8444",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not True"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "7ed5222b-2e65-45fb-9c3c-69603a38a7fe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_even(1)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "71271881-2e06-402b-ac73-9326ae6ee609",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_even(2)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "6e8cbb59-71a2-4f1c-a369-723890d8729f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print(True)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "d99198da-0cea-4267-916d-3ac1c3cf6929",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_even(1)\n",
"is_even(2)\n",
"is_even(3)\n",
"is_even(4)"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "6a356d94-8227-4e1c-bacd-33c70c2a025d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n",
"False\n",
"True\n"
]
}
],
"source": [
"print(is_even(1))\n",
"print(is_even(2))\n",
"print(is_even(3))\n",
"print(is_even(4))"
]
},
{
"cell_type": "markdown",
"id": "d42b74b2-2b68-4faf-b771-353bd463f8a8",
"metadata": {},
"source": [
"# Пример №3 - Списки"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "6bf5d1da-ef53-4dfb-a177-fa3a39ec5923",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['foo', 'bar']"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[\"foo\", \"bar\"]"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "cf3087bf-a690-4483-b13f-16a4d686129e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['foo', 'bar']"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"['foo', 'bar']"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "a85b04e7-1dbf-40d5-97a5-890fe490e334",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[123,\n",
" True,\n",
" 'abc',\n",
" [],\n",
" [1, 2],\n",
" <function __main__.hello()>,\n",
" <function __main__.greet(name, greeting='Hello')>,\n",
" bool]"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[123, True, \"abc\", [], [1, 2], hello, greet, bool]"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "a3595ad1-f467-4831-afb7-7bdf1f56fb93",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['apple', 'banana', 'orange']\n"
]
}
],
"source": [
"fruits = [\"apple\", \"banana\", \"orange\"]\n",
"print(fruits)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "be076235-99b7-4a02-ae74-38f3bf55b4f6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'apple'"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fruits[0]"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "ea4a5c13-6738-49dc-b80b-77a98bee4269",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'banana'"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fruits[1]"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "94d9b7ae-6b13-47f2-a65e-e55d748e8877",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"apple\n",
"banana\n",
"orange\n"
]
}
],
"source": [
"print(fruits[0])\n",
"print(fruits[1])\n",
"print(fruits[2])"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "30dc807d-c79b-45a8-8375-033db8a31512",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['apple', 'banana', 'orange']"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fruits"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "81deae9f-fc28-4d0d-8424-580eb6947fe2",
"metadata": {},
"outputs": [],
"source": [
"fruits.append('cherry')"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "a6776bc2-1c20-4abb-a236-3fb394621a20",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['apple', 'banana', 'orange', 'cherry']"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fruits"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "7e29679b-672e-4630-a978-32e20ffb79b3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'The Office'"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"the office\".title()"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "6f70616a-287f-4df9-b69b-4f8afae3a431",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'FOO BAR'"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"foo bar\".upper()"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "ffee250b-a32c-4da0-8d7d-bb1e0f63cd30",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"ABC\".isupper()"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "bdba39f2-44c9-4005-9dd0-40bc0672720f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"ABc\".isupper()"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "851894d4-eaca-4f6c-af35-d57489aa3500",
"metadata": {},
"outputs": [],
"source": [
"fruits.insert(1, 'lemon')"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "b3a97029-3aa7-4e3d-9af4-ab9a28149240",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['apple', 'lemon', 'banana', 'orange', 'cherry']"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fruits"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "03dd4f7c-8782-427c-a961-8a7933777d7c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"apple\n",
"lemon\n",
"banana\n",
"orange\n",
"cherry\n"
]
}
],
"source": [
"for fruit in fruits:\n",
" print(fruit)"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "e84b2aba-3b5e-4c5d-92d2-006fab84471a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"- apple\n",
"- lemon\n",
"- banana\n",
"- orange\n",
"- cherry\n"
]
}
],
"source": [
"for fruit in fruits:\n",
" print('-', fruit)"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "b435fd35-d3ca-4641-b8c6-80f080d98b9f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"• apple\n",
"• lemon\n",
"• banana\n",
"• orange\n",
"• cherry\n"
]
}
],
"source": [
"for fruit in fruits:\n",
" print('•', fruit)"
]
},
{
"cell_type": "code",
"execution_count": 89,
"id": "6a783d9b-6904-4e0f-ad79-549128355899",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"324 is even? True\n",
"7 is even? False\n",
"14 is even? True\n",
"4768 is even? True\n",
"8 is even? True\n",
"25 is even? False\n"
]
}
],
"source": [
"for num in [324, 7, 14, 4768, 8, 25]:\n",
" print(num, \"is even?\", is_even(num))"
]
},
{
"cell_type": "code",
"execution_count": 90,
"id": "fac0571c-3482-4062-9686-83423ccc8267",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"- 3425 is odd\n",
"3425 % 3 = 2\n",
"- 3245 is odd\n",
"3245 % 3 = 2\n"
]
}
],
"source": [
"for num in [3425,3245,4236,26,652]:\n",
" if is_even(num):\n",
" continue\n",
" print(\"-\", num, \"is odd\")\n",
" print(num, \"% 3 =\", num % 3)"
]
},
{
"cell_type": "markdown",
"id": "2805ec23-c2b9-4bd9-be36-e571aff92dfd",
"metadata": {},
"source": [
"# Пример с досрочным выходом"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "67d39c3b-bda9-41d6-a654-aae9d547f5dc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"- 0\n",
"- 1\n",
"- 2\n",
"- 3\n",
"- 4\n",
"- 5\n",
"- 6\n",
"- 7\n",
"- 8\n",
"- 9\n"
]
}
],
"source": [
"for i in range(10):\n",
" print(\"-\", i)"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "f3023fe4-44fb-4657-8109-540ae43b24b8",
"metadata": {},
"outputs": [],
"source": [
"def is_prime(number):\n",
" if number < 2:\n",
" return False\n",
" if number == 2:\n",
" return True\n",
" for num in range(2, number):\n",
" if not number % num:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "d4ee24c3-4644-4952-a0bb-6a56a6a0b2ef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_prime(7)"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "19073a00-f5c3-43fd-985b-f7eb9637cc9f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_prime(2)"
]
},
{
"cell_type": "code",
"execution_count": 95,
"id": "8fa9a63b-3fcc-490e-a064-a0d5b2f0f46d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_prime(1)"
]
},
{
"cell_type": "code",
"execution_count": 96,
"id": "0c62294f-7ec1-46da-a832-ef9ea26d65be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_prime(10)"
]
},
{
"cell_type": "code",
"execution_count": 97,
"id": "0e91716b-0e53-49a1-aa07-67ecd592f4c0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_prime(11)"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "368c496b-721a-4665-b52c-8e31ca3e93a0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_prime(1000**1000)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "616ad39e-6d9b-4f9f-89f1-edafe4d20b1e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_prime(510011771)"
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "1554650a-f701-4cbe-8564-c3267e9fe97a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%timeit\n",
"is_prime(510011771)"
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "95c6da7b-fc03-4548-9f77-aa308f1c656e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 16.2 s, sys: 191 ms, total: 16.4 s\n",
"Wall time: 16.7 s\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%time is_prime(510011771)"
]
},
{
"cell_type": "code",
"execution_count": 103,
"id": "6f6dcf98-b915-42b4-9ebe-c11b6649e860",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_prime(510011772)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "41f59284-c695-43f3-b4ff-6baf3ed4e60e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 3 μs, sys: 1 μs, total: 4 μs\n",
"Wall time: 4.05 μs\n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%time is_prime(510011772)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "334fe316-7631-4255-86b5-887c83475d74",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
anyio==4.9.0
appnope==0.1.4 ; sys_platform == 'darwin'
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
arrow==1.3.0
asttokens==3.0.0
async-lru==2.0.5
attrs==25.3.0
babel==2.17.0
beautifulsoup4==4.13.3
bleach==6.2.0
certifi==2025.1.31
cffi==1.17.1
charset-normalizer==3.4.1
colorama==0.4.6 ; sys_platform == 'win32'
comm==0.2.2
debugpy==1.8.13
decorator==5.2.1
defusedxml==0.7.1
executing==2.2.0
fastjsonschema==2.21.1
fqdn==1.5.1
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
idna==3.10
ipykernel==6.29.5
ipython==9.0.2
ipython-pygments-lexers==1.1.1
isoduration==20.11.0
jedi==0.19.2
jinja2==3.1.6
json5==0.11.0
jsonpointer==3.0.0
jsonschema==4.23.0
jsonschema-specifications==2024.10.1
jupyter-client==8.6.3
jupyter-core==5.7.2
jupyter-events==0.12.0
jupyter-lsp==2.2.5
jupyter-server==2.15.0
jupyter-server-terminals==0.5.3
jupyterlab==4.3.6
jupyterlab-pygments==0.3.0
jupyterlab-server==2.27.3
markupsafe==3.0.2
matplotlib-inline==0.1.7
mistune==3.1.3
nbclient==0.10.2
nbconvert==7.16.6
nbformat==5.10.4
nest-asyncio==1.6.0
notebook==7.3.3
notebook-shim==0.2.4
overrides==7.7.0
packaging==24.2
pandocfilters==1.5.1
parso==0.8.4
pexpect==4.9.0 ; sys_platform != 'emscripten' and sys_platform != 'win32'
platformdirs==4.3.7
prometheus-client==0.21.1
prompt-toolkit==3.0.50
psutil==7.0.0
ptyprocess==0.7.0 ; os_name != 'nt' or (sys_platform != 'emscripten' and sys_platform != 'win32')
pure-eval==0.2.3
pycparser==2.22
pygments==2.19.1
python-dateutil==2.9.0.post0
python-json-logger==3.3.0
pywin32==310 ; platform_python_implementation != 'PyPy' and sys_platform == 'win32'
pywinpty==2.0.15 ; os_name == 'nt'
pyyaml==6.0.2
pyzmq==26.3.0
referencing==0.36.2
requests==2.32.3
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rpds-py==0.24.0
send2trash==1.8.3
setuptools==78.1.0
six==1.17.0
sniffio==1.3.1
soupsieve==2.6
stack-data==0.6.3
terminado==0.18.1
tinycss2==1.4.0
tornado==6.4.2
traitlets==5.14.3
types-python-dateutil==2.9.0.20241206
typing-extensions==4.13.0
uri-template==1.3.0
urllib3==2.3.0
wcwidth==0.2.13
webcolors==24.11.1
webencodings==0.5.1
websocket-client==1.8.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment