Skip to content

Instantly share code, notes, and snippets.

@okwrtdsh
Last active November 5, 2023 05:28
Show Gist options
  • Save okwrtdsh/a2c557d50030e43afa1babef9913dd6a to your computer and use it in GitHub Desktop.
Save okwrtdsh/a2c557d50030e43afa1babef9913dd6a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "1623277e-29ed-4ff0-8e60-dad03de677c7",
"metadata": {},
"source": [
"# Python チートシート"
]
},
{
"cell_type": "markdown",
"id": "836a3f22-34f6-4508-ab10-e7a329ffd64b",
"metadata": {},
"source": [
"## 変数\n",
"`変数名 = 値` という形式で、変数を \"定義\" する (宣言するとも言う)\n",
"\n",
"`変数名 = 値` は、左辺の `変数名` に右辺`値` を \"代入\" する (`変数名` という箱に `値` を格納するイメージ)\n",
"\n",
"```python\n",
"# 変数 x に 123 を代入する場合\n",
"x = 123\n",
"# 123 = x とすることはできない (数学と異なり、 \"=\" の左辺と右辺は等価でない)\n",
"\n",
"y = x\n",
"z = y\n",
"# この時と以下のように書き換え可能\n",
"z = x\n",
"\n",
"y = x\n",
"print(y)\n",
"# この時と以下のように書き換え可能\n",
"print(x)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d912b6f1-2c9e-433f-9776-157fe3e53818",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(34, 12)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 12\n",
"b = 34\n",
"a, b = b, a # a を b に、 b を a に同時に代入する\n",
"a, b"
]
},
{
"cell_type": "markdown",
"id": "0e068e08-48fa-45a3-a997-edcb4799a95d",
"metadata": {},
"source": [
"## 数値"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2e2955bb-f1c7-44bf-851f-10506ded2b18",
"metadata": {},
"outputs": [],
"source": [
"n = 10"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "fe39e428-0d86-4af3-9507-57f3eb051bd5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"13"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n + 3 # 足し算"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a8cbac0a-9237-47bf-9dcb-da89970ec930",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n - 3 # 引き算"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7e054dec-e05c-4352-b66d-8f03adfde432",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"30"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n * 3 # 掛け算"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "22f2cd8d-afb5-4be9-aff2-5c4dad975e38",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.3333333333333335"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n / 3 # 割り算"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1177a1bf-2b32-4d37-bd5b-8a77890459d2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n // 3 # 割り算 (小数点以下切り捨て)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f34732d4-6720-4179-b852-bfb029b83dd2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n % 3 # 剰余"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1469b5e2-4aeb-41f3-ab03-2c100510afd1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1000"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n ** 3 # 指数"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "eea2d97f-04cb-4ae1-93b9-4b2323d1a397",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"13"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n += 3\n",
"n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "482c1c38-665b-4e89-a2ee-29e8d66a22c9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n -= 3\n",
"n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ec9dea23-0dab-451a-b198-6b7350af92e5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"30"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n *= 3\n",
"n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a6467e62-97e7-40ee-a7dc-785581390bee",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10.0"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n /= 3\n",
"n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "74fbe04e-85de-4b0c-a00d-97ad763ac730",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.0"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n //= 3\n",
"n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "91cceb1c-4a76-476c-a257-33cfa96ab974",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(n) # 整数値型 int"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "8d59b7cc-86d8-4d6b-bf47-652e5aa73796",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n = 1.5\n",
"type(n) # 浮動小数点型 (小数点が含まれる数値) float"
]
},
{
"cell_type": "markdown",
"id": "4539549e-dbfd-4517-ba5f-cb6c3c246aee",
"metadata": {},
"source": [
"## 文字列\n",
"`'文字列'` または `\"文字列\"` といった形式で記述する\n",
"\n",
"複数行の文字列は以下の形式で記述する\n",
"\n",
"```python\n",
"'''\n",
"複数行の\n",
"文字列\n",
"'''\n",
"\n",
"\"\"\"\n",
"複数行の\n",
"文字列\n",
"\"\"\"\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "5a8f726c-f74d-4e5c-8c54-53ffe66c06e7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abc'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = 'abc'\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "5f20ba10-c9a5-4cf8-bc9a-edc634fd1ae9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"''"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = \"\"\n",
"s # 空文字列"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "35121322-6a59-4d91-8719-8d750ab02bc5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\na\\nb\\nc\\n'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = \"\"\"\n",
"a\n",
"b\n",
"c\n",
"\"\"\"\n",
"s # \\n は改行コード"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "2b5b3c95-5b54-4486-895b-6e33edfa1f12",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"a\n",
"b\n",
"c\n",
"\n"
]
}
],
"source": [
"print(s) # 改行を表示"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "907494a1-c648-456f-94f5-14fc2e9ef7f9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abcdef'"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = 'abc'\n",
"s + 'def' # 文字列の結合"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "8131633c-94da-43d8-97fc-b695b0ebd8de",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abcxyz'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s += 'xyz' # s に 'xyz' を結合して代入\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "80961f20-3ffd-4c40-9f14-8ed887f6dede",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abcabcabc'"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = 'abc'\n",
"s * 3 # * n は n 回分文字列を繰り返す"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "74fea698-5195-4c40-a7e4-f0ea7df683d2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abcabc'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s *= 2 # s に s を  2 回繰り返したものを代入\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "b24052de-6288-498c-a89b-43e4e6be7968",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'a'"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = 'abcdef'\n",
"s[0] # 最初の要素"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "06888e34-85d2-489d-960d-46fbbb4c2c2a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'f'"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[-1] # 最初の要素"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "ff4c9e47-46d5-4680-91a5-960f65b72b94",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(s) # 文字列の長さ"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "f2bfee94-dc40-448c-876d-c1452604cd99",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = 'abcde'\n",
"'c' in s # 変数 s に 'c' が含まれるか"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "12f92bf6-bf29-499f-ae09-b784e7eae7e3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(s) # 文字列型 str"
]
},
{
"cell_type": "markdown",
"id": "85ab6626-f79f-4684-a7b6-e313f51cca98",
"metadata": {},
"source": [
"## リスト\n",
"`[` と `]` で記述する"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "b8a4d4ea-e594-4392-9a58-074ab29a3d4f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 2, 1]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [6, 2, 1]\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "dad477fd-406f-4323-ab72-f3127011412c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [] # 空リスト\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "3ef11a52-36ba-4692-abf2-ad9cb97cbcf3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [6, 2, 1]\n",
"l[0] # 最初の要素"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "1eaba4e5-4ea2-4610-bc52-5bdc8c7b1721",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l[-1] # 最初の要素"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "78196849-41cb-42e5-b299-8daa0788e3c1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(l) # 変数 l の要素数 (長さ)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "26d7ac0b-08c4-40c1-9ec7-8a408feac05a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(l) # 変数 l の合計"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "e67a089f-4174-430d-af64-8dd0daba2382",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 2, 1, 3]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l.append(3) # 末尾に要素を追加\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "46b77843-38ce-42d5-bfbc-2d423d982a21",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l.pop() # 末尾の要素を取り出す"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "de1cd871-368b-4f10-bc63-73028123a47c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 2, 1]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "5017db45-47e8-48a6-b10f-883af89d462a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l.pop(0) # 先頭の要素を取り出す"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "ddd1587d-de49-4638-900c-351fea957ec9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 1]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "eb7b3878-722d-4786-a7de-f2f126842127",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 1]"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [6, 2, 1]\n",
"l[0] = 0 # リストの要素に代入\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "fde3fd35-e9fe-419c-8003-bc87481ad938",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[7, 2, 1]"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [6, 2, 1]\n",
"l[0] += 1 # リストの要素に +1 して再代入\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "5c7dffdd-8d29-4140-a8df-027008324fa6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [6, 2, 1, 6]\n",
"l.index(6) # 6 が存在する最初のインデックス番号"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "891ca816-2475-4724-bdf6-de52f61cb11d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [6, 2, 1]\n",
"2 in l # 変数 l に 2 が含まれるか"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "08300f2b-29f9-412f-827f-d056c0231f25",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(l) # リスト型 list"
]
},
{
"cell_type": "markdown",
"id": "a0357989-c1f8-4803-9170-782631c16382",
"metadata": {},
"source": [
"## タプル\n",
"`(` と `,` と `)` で記述する\n",
"\n",
"一度定義したタプルは変更できない"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "d49eeaf0-76d7-4957-b7c3-3e9aac824974",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(7, 'foo')"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = (7, 'foo')\n",
"t"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "60500544-b509-4804-bc22-12ce0654d2bd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(7,)"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = (7,)\n",
"t"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "96ea8d76-5fad-48fe-8fd9-c3f6c9abc2da",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = (7, 'foo', [6, 2, 1])\n",
"t[0] # 最初の要素"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "bc3d8a7e-57f9-498b-bf94-2bc047fc9830",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 2, 1]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t[-1] # 最後の要素"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "42f5da82-8978-40bb-b4ec-b1833c8a30ff",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(t) # 変数 t の要素数 (長さ)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "12af080f-52cb-40b1-90df-5ded6e6e7ae4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = (6, 2, 1)\n",
"sum(t) # 変数 t の合計 (要素が全て数値型の場合)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "a27e39b0-ceb9-4e53-8c63-a4fa58038b94",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = (6, 2, 1)\n",
"2 in t # 変数 t に 2 が含まれるか"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "844919f1-623e-4d4c-81cf-6bbf4d9762af",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 'foo', [6, 2, 1])"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = (7, 'foo', [6, 2, 1])\n",
"# t[0] = 1 # 一度定義したタプルは変更できない\n",
"# t[0] += 1 # 一度定義したタプルは変更できない\n",
"t = (1, 'foo', [6, 2, 1]) # 再定義することは可能\n",
"t"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "f77774d8-75c7-4319-a5ee-9132b234a553",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 'foo', [7, 2, 1, 7])"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t[-1].append(7) # 要素内のリストオブジェクトは操作可能\n",
"t[-1][0] += 1\n",
"t"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "8ad3d295-9551-4cba-a201-bdde51b457f9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = (7, 'foo', [6, 2, 1])\n",
"a, b, c = t # 要素数と同じ数の変数へ代入可能 (Unpacking)\n",
"# a, b = t 要素数未満ではできない\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "86441499-7fa9-4412-833f-387b7faeb36a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(t) # タプル型 tuple"
]
},
{
"cell_type": "markdown",
"id": "c0c097de-6be9-44cd-87f8-01eb2cddb56f",
"metadata": {},
"source": [
"## 辞書\n",
"`{key: value}` の形式で記述する\n"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "37f8898c-6415-4ed1-9cd7-47ade0bb0a6c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'foo': 7, 'bar': 'abc', 'baz': [6, 2, 1], 8: (3, 4), (1, 2): {'key': 'value'}}"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {\n",
" \"foo\": 7,\n",
" \"bar\": \"abc\",\n",
" \"baz\": [6, 2, 1],\n",
" 8: (3, 4),\n",
" (1, 2): {'key': 'value'}, # 要素にリストを含まないタプルはキーに使用可能\n",
"\n",
"}\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "7a524baf-b16d-411f-864c-5bfeda9a36d5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{}"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {} # 空辞書\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "95f52986-bb54-4912-9d8e-a443726c2e20",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {\n",
" \"foo\": 7,\n",
" \"bar\": \"abc\",\n",
" \"baz\": [6, 2, 1],\n",
" 8: (3, 4),\n",
" (1, 2): {'key': 'value'},\n",
"}\n",
"d[\"foo\"] # キーから対応する値を取得"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "2aab9e71-5ebf-4420-80d4-2a5bf55e97d0",
"metadata": {},
"outputs": [],
"source": [
"# d[\"hoge\"] # 存在しないキーでアクセスするとエラー\n",
"d.get(\"hoge\") # 存在しない可能性のあるキーでアクセスする場合は get() メソッドを使用する"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "98a9f9e0-09a0-408b-ac94-258eb9a948c6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'not found'"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.get(\"hoge\", 'not found') # 存在しない場合の default 値の設定が可能"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "a55e8439-4f89-4788-8a62-0573dd6c10a6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.get(\"foo\", 'not found') # 存在する場合は default 値ではなく、キーに対応する値が取得される"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "c2ba0031-3d28-40a3-8284-c34a614ff611",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'foo': 7, 'bar': 'def', 'baz': [6, 2, 1], 8: (3, 4), (1, 2): {'key': 'value'}}"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d[\"bar\"] = \"def\" # 既存の要素へ代入\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "a1533140-1f9b-41a8-9bcb-f78f30612dd0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'foo': 7,\n",
" 'bar': 'def',\n",
" 'baz': [6, 2, 1],\n",
" 8: (3, 4),\n",
" (1, 2): {'key': 'value'},\n",
" 'hoge': 123}"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d[\"hoge\"] = 123 # 要素を追加\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "bce998d2-c316-422c-adcc-db22ad82afc7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'foo': 7,\n",
" 'bar': 'def',\n",
" 'baz': [6, 2, 1],\n",
" 8: (3, 4),\n",
" (1, 2): {'key': 'value'},\n",
" 'hoge': 123,\n",
" 'fuga': 456,\n",
" 'piyo': [4, 5, 6]}"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.update({\n",
" 'fuga': 456,\n",
" 'piyo': [4, 5, 6],\n",
"})\n",
"d # 要素をまとめて追加"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "d2c7dc2f-54d8-43fd-b593-e487148a3906",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['foo', 'bar', 'baz', 8, (1, 2)])"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {\n",
" \"foo\": 7,\n",
" \"bar\": \"abc\",\n",
" \"baz\": [6, 2, 1],\n",
" 8: (3, 4),\n",
" (1, 2): {'key': 'value'},\n",
"}\n",
"\n",
"d.keys() # キーのリスト"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "6a941d9c-1d73-4695-9be0-68114699d1fc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([7, 'abc', [6, 2, 1], (3, 4), {'key': 'value'}])"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.values() # 値のリスト"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "01f1ef5e-7ef4-4c3a-8823-1d493b6f51db",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_items([('foo', 7), ('bar', 'abc'), ('baz', [6, 2, 1]), (8, (3, 4)), ((1, 2), {'key': 'value'})])"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.items() # キーと値のペアのタプルが要素のリスト"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "ca22a7a2-e30d-490f-8955-c6f5aa42dd19",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'foo' in d.keys() # 'foo' が変数 d のキーに含まれるか"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "f1509322-3163-4a61-9353-86632e8ca4fa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'foo' in d # in d.keys() は in d と省略可能"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "2d8abf29-2c5e-41e3-9f36-80bddd3810fe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'abc' in d.values() # 'abc' が変数 d の値に含まれるか"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "679ccdf8-d16f-4453-ab27-6d4a7738003c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(d) # 辞書型 dict"
]
},
{
"cell_type": "markdown",
"id": "e5e4814e-0e88-4a63-93c1-3cd3bdf6bfd2",
"metadata": {},
"source": [
"## 集合型\n",
"`{` と `}` で記述\n",
"\n",
"順序付けされていないユニークな要素で構成されたコレクション"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "65251f13-f6bf-434b-992b-145c0e956b24",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3}"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = {1, 2, 2, 3, 3, 3}\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "318d0b1e-0904-4901-a45d-5d9ebb97bd55",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'H', 'e', 'l', 'o'}"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = set('Hello') # 文字列から集合型へ変換\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "f72622f4-1d59-4170-976d-531c8ea02d8f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'W', 'd', 'l', 'o', 'r'}"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B = set('World')\n",
"B"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "6159b05c-40cc-47c7-8269-bcf048455881",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'H', 'e'}"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A - B # 差集合 (共通する o, l を削除)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "148d27d0-ba4b-4a39-90d2-ae6504c6abef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'H', 'W', 'd', 'e', 'l', 'o', 'r'}"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A | B # 和集合 (要素の和)"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "4477e739-4fa2-4d57-90fe-227783d26a5d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'l', 'o'}"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A & B # 積集合 (共通する o, l を抽出)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "71e88331-3af2-49c7-85c0-2de19990861c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'H', 'W', 'd', 'e', 'r'}"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A ^ B # 排他的論理和 (共通する o, l 以外の要素)"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "15800903-8b3b-4141-a72f-e8d87a3f0293",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['a', 'b', 'c', 'd']"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# よく使う例: 重複を含むものからユニークなリストに変換\n",
"sorted(set('aaccbbbdaaaaccc'))"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "d60df515-98df-4ddd-b0a4-4f54bd63f309",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = set('aaccbbbdaaaaccc')\n",
"'d' in x # 変数 x に 'd' が含まれるか"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "e08d59f2-c2a4-457c-86ab-e7f1d1d00517",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"set"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = {1, 2, 3}\n",
"type(x) # 集合型 set"
]
},
{
"cell_type": "markdown",
"id": "88fe6f5a-0b65-4340-a740-124325b24217",
"metadata": {},
"source": [
"## if 文\n",
"\n",
"```python\n",
"条件分岐に関与しない処理\n",
"\n",
"if 条件式 1:\n",
" 条件式 1 が True な場合の処理 1\n",
" 条件式 1 が True な場合の処理 2\n",
" ...\n",
"elif 条件式 2:\n",
" 条件式 2 が True な場合の処理 1\n",
" 条件式 2 が True な場合の処理 2\n",
" ...\n",
"...\n",
"else:\n",
" 条件式 1,2,... がすべて  False な場合の処理 1\n",
" 条件式 1,2,... がすべて  False な場合の処理 2\n",
"\n",
"条件分岐に関与しない処理\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "a079de05-a5cb-4678-b9f4-01b06216a688",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 10\n",
"x == 10 # x が 10 と等しいかの条件式"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "9b6188a8-7ca5-4d6c-afb5-30d01f40565a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x != 10 # x が 10 以外"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "bbc1f6cb-0bd3-475c-95ce-246c4222a4c7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x <= 10 # x が 10 以下"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "1304f684-7fa6-4354-904f-78dbac847b6c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x < 10 # x が 10 未満"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "1dd3ac67-ecb6-4a2f-9b95-9a1bad1beb53",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x >= 10 # x が 10 以上"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "7ce586d8-311c-4351-b42e-c7e581014883",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x > 10 # x が 10 より大きい"
]
},
{
"cell_type": "code",
"execution_count": 89,
"id": "82374e2f-4bad-485b-8d77-b62e6ca09ada",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x >= 0 and x % 2 == 0 # x は 0 以上の 2 の倍数"
]
},
{
"cell_type": "code",
"execution_count": 90,
"id": "30ea1833-530d-488d-8ff6-95c71b2533e5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x < 0 or x % 2 == 0 # x は 0 未満の場合, もしくは 2 の倍数 (..., -4, -3, -2, -1, 0, 2, 4, 6, ... の時 True)"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "c5d87d6e-9016-40df-983a-f301fe64b7ac",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x is not None # x は None でない"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "774fcb8f-3438-4e94-9e9c-6647ef6c7c55",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = []\n",
"not x # x (リストや文字列、辞書) が空でない"
]
},
{
"cell_type": "markdown",
"id": "b6a16466-b8af-4e81-892c-be0a19b43213",
"metadata": {},
"source": [
"## for 文\n",
"\n",
"```python\n",
"繰り返えさない処理\n",
"\n",
"for ループ変数 in オブジェクト:\n",
" 繰り返し処理 1\n",
" 繰り返し処理 2\n",
" ...\n",
"\n",
"繰り返えさない処理\n",
"```\n",
"\n",
"### range() 関数\n",
"\n",
"```python\n",
"range([start,], stop[, step])\n",
"\n",
"start: (オプション) 開始の数値\n",
"stop: (必須) 終了の数値 (含まない)\n",
"step: (オプション) step の数値ずつ増加する (マイナスの場合は減少する)\n",
"```\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "0d395ced-cdc3-4dfa-a4b8-06b6acebdc53",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"foo\n",
"0\n",
"bar\n",
"1\n",
"bar\n",
"2\n",
"bar\n",
"3\n",
"bar\n",
"4\n",
"bar\n",
"baz\n"
]
}
],
"source": [
"print('foo')\n",
"for i in range(5):\n",
" print(i)\n",
" print('bar')\n",
"print('baz')"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "7c4c29ba-5345-49cf-970c-f1389aeb6f23",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"for i in range(1, 5):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 95,
"id": "aab1ce8e-0884-4813-80c4-17b79f26eb6c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"4\n",
"7\n"
]
}
],
"source": [
"for i in range(1, 10, 3):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 96,
"id": "4e3564ef-bca5-4d66-86d2-bc9635d6e89c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"2\n",
"1\n"
]
}
],
"source": [
"for i in [6, 2, 1]:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 97,
"id": "af81fc6d-999f-429f-aa1b-23c0ef6e96db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n",
"b\n",
"c\n"
]
}
],
"source": [
"for s in 'abc':\n",
" print(s)"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "f6713a54-cc36-4051-94a1-c8c9474d3e5a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"key1 val1\n",
"key2 val2\n",
"key3 val3\n"
]
}
],
"source": [
"d = {\n",
" 'key1': 'val1',\n",
" 'key2': 'val2',\n",
" 'key3': 'val3',\n",
"}\n",
"for k, v in d.items():\n",
" print(k, v)"
]
},
{
"cell_type": "markdown",
"id": "113d038e-42c0-4998-b6cf-a3faff4d38db",
"metadata": {},
"source": [
"## while 文\n",
"\n",
"```python\n",
"繰り返えさない処理\n",
"\n",
"while 条件式:\n",
" 条件式 が True な場合の処理 1\n",
" 条件式 が True な場合の処理 2\n",
" ...\n",
"\n",
"繰り返えさない処理\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "c7ad3630-fb27-40a3-a7ed-602c4a736281",
"metadata": {},
"outputs": [],
"source": [
"start = 1\n",
"stop = 10\n",
"step = 3"
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "8618beff-bd48-4253-86a1-5c21db9a1af4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"foo\n",
"4\n",
"foo\n",
"7\n",
"foo\n"
]
}
],
"source": [
"for i in range(start, stop, step):\n",
" print(i)\n",
" print('foo')"
]
},
{
"cell_type": "code",
"execution_count": 101,
"id": "5dc52bf3-210d-420c-a4b7-7f98ffe79d98",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"foo\n",
"4\n",
"foo\n",
"7\n",
"foo\n"
]
}
],
"source": [
"# 上の for 文と等価な処理\n",
"i = start\n",
"while i < stop:\n",
" print(i)\n",
" print('foo')\n",
" i += step"
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "3ee3f122-3a82-4faa-9a1f-e9ab873445d0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"6\n"
]
}
],
"source": [
"# 下一桁ずつ出力\n",
"n = 621\n",
"while n > 0:\n",
" print(n - n // 10 * 10)\n",
" n //= 10"
]
},
{
"cell_type": "markdown",
"id": "5d2b0dc5-491c-437a-857d-fb6365ebc679",
"metadata": {},
"source": [
"## 関数\n",
"### def 文\n",
"```python\n",
"def func_name(arg1, arg2, ...):\n",
" 処理 1\n",
" 処理 2\n",
" ...\n",
" return 戻り値\n",
"\n",
"# 関数の呼び出し\n",
"res = func_name(arg1, arg2, ...)\n",
"```\n",
"\n",
"### lambda 式 (無名関数)\n",
"```python\n",
"lambda arg1, arg2, ...: 戻り値\n",
"\n",
"\n",
"# lambda 式を変数 func_name に代入\n",
"func_name = lambda arg1, arg2, ...: 戻り値\n",
"# 関数の呼び出し\n",
"res = func_name(arg1, arg2, ...)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 103,
"id": "c59a00e0-c311-4e72-ad39-85f1d479109d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"123"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 受け取ったものを返す関数\n",
"def echo(x):\n",
" return x\n",
"\n",
"arg = 123\n",
"res = echo(arg)\n",
"res"
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "f7d730a1-cebd-43a1-8850-0562f6d91402",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"123"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"echo = lambda x: x\n",
"res = echo(arg)\n",
"res"
]
},
{
"cell_type": "code",
"execution_count": 105,
"id": "20b1fbe8-f3cf-4d44-8458-149f03cd3f6a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.0"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 数値のリストの平均値を求める関数\n",
"def average(l):\n",
" return sum(l)/len(l)\n",
"\n",
"l = [6, 2, 1]\n",
"average(l)"
]
},
{
"cell_type": "code",
"execution_count": 106,
"id": "bf6ebb54-3fc6-4b7f-bb69-e52574d3909d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.0"
]
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"average = lambda l: sum(l)/len(l)\n",
"average(l)"
]
},
{
"cell_type": "code",
"execution_count": 107,
"id": "be32c021-99b6-4fcd-9a1f-c8ecc7e4c4c2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"foo\n",
"4\n",
"foo\n",
"7\n",
"foo\n",
"1\n",
"foo\n",
"4\n",
"foo\n",
"7\n",
"foo\n"
]
}
],
"source": [
"start = 1\n",
"stop = 10\n",
"step = 3\n",
"for i in range(start, stop, step):\n",
" print(i)\n",
" print('foo')\n",
"i = start\n",
"while i < stop:\n",
" print(i)\n",
" print('foo')\n",
" i += step"
]
},
{
"cell_type": "code",
"execution_count": 108,
"id": "00879c58-66ee-4e12-b680-a1bf6f7e906f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"foo\n",
"4\n",
"foo\n",
"7\n",
"foo\n"
]
}
],
"source": [
"# 上の for 文や while 文と等価な処理\n",
"def loop(i):\n",
" if i >= stop: # 終了条件\n",
" return\n",
" # 繰り返し処理\n",
" print(i)\n",
" print('foo')\n",
" # 次のステップへ進める\n",
" return loop(i + step)\n",
"\n",
"loop(start)"
]
},
{
"cell_type": "markdown",
"id": "837b613c-e6df-4fd6-b041-75d715d37272",
"metadata": {},
"source": [
"## 型変換 (cast)\n",
"```python\n",
"int() 関数: 文字列から整数値へ変換\n",
"float() 関数: 文字列から浮動小数点へ変換\n",
"str() 関数: 数値などを文字列へ変換\n",
"list() 関数: 文字列やタプル、集合型などをリストへ変換\n",
"sorted() 関数 文字列やタプル、集合型などを、一定の順に並び替えたリストへ変換\n",
"tuple() 関数: 文字列やリストをタプルへ変換\n",
"dict() 関数: キーと値のペアのリストやタプルを要素とするリストなどを辞書へ変換\n",
"set() 関数: 文字列やリストを集合型へ変換\n",
"\n",
"map(function, list_object) 関数: list_object の各要素に対して function 関数を実行し、戻り値を新しいリストの要素にする\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "6bbcb761-9fdc-475f-920c-6660486f6835",
"metadata": {},
"outputs": [],
"source": [
"def show(obj):\n",
" # 値 と 型 を表示する\n",
" print(repr(obj), type(obj))"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "2bce4f7f-660a-4057-b363-e90839138712",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"127 <class 'int'>\n",
"127.0 <class 'float'>\n"
]
}
],
"source": [
"# 文字列から数値型 (int, float) への変換\n",
"s = \"127\"\n",
"i = int(s)\n",
"f = float(s)\n",
"show(i)\n",
"show(f)"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "2c9d1636-51c3-4378-a58e-a21883676c7b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'127' <class 'str'>\n",
"'127.0' <class 'str'>\n"
]
}
],
"source": [
"# 数値型 (int, float) から文字列への変換\n",
"s1 = str(i)\n",
"s2 = str(f)\n",
"show(s1)\n",
"show(s2)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "7c4557ee-44d5-46c9-99e8-47e85fd52e2e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['6', '1', '2'] <class 'list'>\n",
"[6, 1, 2] <class 'list'>\n",
"[1, 2, 6] <class 'list'>\n",
"['a', 'c', 'e'] <class 'list'>\n"
]
}
],
"source": [
"# リストへの変換\n",
"s = '612'\n",
"t = (6, 1, 2)\n",
"a = {6, 6, 1, 2, 2, 2}\n",
"d = {'a': 'b', 'c': 'd', 'e': 'f'}\n",
"l1 = list(s)\n",
"l2 = list(t)\n",
"l3 = list(a)\n",
"l4 = list(d) # キーのリスト\n",
"show(l1)\n",
"show(l2)\n",
"show(l3)\n",
"show(l4)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "e1b22cd3-839c-4750-9ed5-5ed43f1c3699",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['1', '2', '6'] <class 'list'>\n",
"[1, 2, 6] <class 'list'>\n",
"[1, 2, 6] <class 'list'>\n"
]
}
],
"source": [
"# sorted でのリストへの変換\n",
"l1 = sorted(s)\n",
"l2 = sorted(t)\n",
"l3 = sorted(a)\n",
"show(l1)\n",
"show(l2)\n",
"show(l3)"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "671a579d-0f46-4ee9-aa88-b5ed0db57b65",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('6', '1', '2') <class 'tuple'>\n",
"(6, 1, 2) <class 'tuple'>\n",
"(1, 2, 6) <class 'tuple'>\n",
"('a', 'c', 'e') <class 'tuple'>\n"
]
}
],
"source": [
"# タプルへの変換\n",
"s = '612'\n",
"l = [6, 1, 2]\n",
"a = {6, 6, 1, 2, 2, 2}\n",
"d = {'a': 'b', 'c': 'd', 'e': 'f'}\n",
"t1 = tuple(s)\n",
"t2 = tuple(l)\n",
"t3 = tuple(a)\n",
"t4 = tuple(d)\n",
"show(t1)\n",
"show(t2)\n",
"show(t3)\n",
"show(t4)"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "06b535d9-ec08-4295-af55-0ef0515535bd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1: 2, 'a': 'b', (3, 4): (5, 6)} <class 'dict'>\n",
"{1: 2, 'a': 'b', (3, 4): (5, 6)} <class 'dict'>\n",
"{'a': 1, 'b': 2, 'c': 3} <class 'dict'>\n"
]
}
],
"source": [
"# 辞書への変換\n",
"l1 = [[1, 2], ['a', 'b'], [(3, 4), (5, 6)]] # リスト内に、キーと値のペアのリスト\n",
"l2 = [(1, 2), ('a', 'b'), ((3, 4), (5, 6))] # リスト内に、キーと値のペアのタプル\n",
"d1 = dict(l1)\n",
"d2 = dict(l2)\n",
"# dict 関数へのキーワード引数が辞書のキーと値に変換される\n",
"d3 = dict(\n",
" a=1,\n",
" b=2,\n",
" c=3,\n",
")\n",
"show(d1)\n",
"show(d2)\n",
"show(d3)"
]
},
{
"cell_type": "code",
"execution_count": 116,
"id": "6ee1583a-ee70-42f0-94a3-6f917488461d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1, 2, 6} <class 'set'>\n",
"{'d', ' ', 'o', 'l', 'W', 'r', 'e', 'H'} <class 'set'>\n",
"{1, 2, 6} <class 'set'>\n"
]
}
],
"source": [
"# 集合型への変換\n",
"l = [6, 6, 1, 2, 2, 2]\n",
"s = 'Hello World'\n",
"t = (6, 6, 1, 2, 2, 2)\n",
"a1 = set(l)\n",
"a2 = set(s)\n",
"a3 = set(t)\n",
"show(a1)\n",
"show(a2)\n",
"show(a3)"
]
},
{
"cell_type": "code",
"execution_count": 117,
"id": "bed29895-085b-4f15-aaa6-87f50eaf6d88",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['6', '2', '1'] <class 'list'>\n",
"[6.0, 2.0, 1.0] <class 'list'>\n",
"[36.0, 4.0, 1.0] <class 'list'>\n"
]
}
],
"source": [
"# map 関数でリストの要素を変換\n",
"l1 = [6, 2, 1]\n",
"l2 = list(map(str, l1)) # 要素を整数値から文字列に変換\n",
"l3 = list(map(float, l2)) # 要素を文字列から float に変換\n",
"l4 = list(map(lambda x: x**2, l3)) # 各要素を 2 乗\n",
"show(l2)\n",
"show(l3)\n",
"show(l4)"
]
},
{
"cell_type": "markdown",
"id": "7e752e9e-3e48-45cb-87c5-69947312e2ec",
"metadata": {},
"source": [
"# 入出力\n",
"## 入力\n",
"```python\n",
"input() 関数: 標準入力を受け取る\n",
"```\n",
"\n",
"## 出力\n",
"```python\n",
"print() 関数: 標準出力へ出力する\n",
"\n",
"print('foo', end='')\n",
" end: 出力の末尾の文字列を指定する (デフォルトでは '\\n' (改行))\n",
"```\n",
"\n",
"## split() と join()\n",
"```python\n",
"s.split('区切り文字'): 文字列型の変数 s を '区切り文字' (指定しない場合は空白文字 (' ')) で区切り、リストに変換する\n",
"\n",
"'区切り文字'.join(l): 文字列型の要素を持つリスト l を '区切り文字' で結合して一つの文字列に変換する\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 118,
"id": "6b767d39-da76-4546-85b7-a4d367b626c0",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
" test\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'test' <class 'str'>\n"
]
}
],
"source": [
"s = input()\n",
"show(s) # 入力された内容を文字列として受け取る"
]
},
{
"cell_type": "code",
"execution_count": 119,
"id": "dec5b370-fdc2-42bb-8ddc-9b4c9e732255",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123 <class 'int'>\n"
]
}
],
"source": [
"# 数を入力して数値として利用する場合\n",
"# s = input()\n",
"s = '123' # 入力された文字が 123\n",
"\n",
"i = int(s) # 文字列を整数値 int に型変換する\n",
"show(i)"
]
},
{
"cell_type": "code",
"execution_count": 120,
"id": "ee8e1a05-a657-47bf-8609-e4adfbb81dd8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c'] <class 'list'>\n",
"['a', ' b', ' c'] <class 'list'>\n",
"['a', 'b', 'c'] <class 'list'>\n"
]
}
],
"source": [
"# 特定のフォーマットで入力して、リストとして利用する\n",
"# s = input()\n",
"s = 'a b c' # 空白区切りで入力された場合\n",
"\n",
"l = s.split() # 空白区切りで分離したリストに変換\n",
"show(l)\n",
"\n",
"s = 'a, b, c' # カンマ+スペース区切りで入力された場合\n",
"l1 = s.split(',') # 区切り文字にカンマを指定\n",
"show(l1) # ' b' と空白が余計に含まれてしまっている\n",
"l2 = s.split(', ') # 区切り文字に正しくカンマ+スペースを指定\n",
"show(l2) # 正しく分離できている"
]
},
{
"cell_type": "code",
"execution_count": 121,
"id": "3e33ceec-33e1-4693-b902-2bb91a1f79b1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3] <class 'list'>\n"
]
}
],
"source": [
"# 特定のフォーマットで入力して、数値型などの要素リストとして利用する\n",
"# s = input()\n",
"s = '1 2 3' # 空白区切りで数字が入力された場合\n",
"\n",
"l = s.split() # 空白区切りで分離したリストに変換\n",
"l = list(map(int, l)) # 各要素を int 型に型変換\n",
"show(l)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"id": "ba98f9b6-429a-4cb2-9888-cf050d483b3e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20231101"
]
}
],
"source": [
"# 改行せず続けて出力する\n",
"l = ['2023', '11', '01']\n",
"for i in l:\n",
" print(i, end='')"
]
},
{
"cell_type": "code",
"execution_count": 123,
"id": "f9597996-c963-4e9c-bd1a-9ebcbf1010d5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2023/11/01\n"
]
}
],
"source": [
"# 特定の区切り文字で結合して出力する\n",
"l = ['2023', '11', '01']\n",
"print('/'.join(l))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb2dfc81-2f17-4c9c-aefa-ef1599895347",
"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.10.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment