Skip to content

Instantly share code, notes, and snippets.

@hestiwahyuningsih
Created December 3, 2025 12:59
Show Gist options
  • Select an option

  • Save hestiwahyuningsih/6b7d84c325786fb7465812e19a62cddc to your computer and use it in GitHub Desktop.

Select an option

Save hestiwahyuningsih/6b7d84c325786fb7465812e19a62cddc to your computer and use it in GitHub Desktop.
Phyton bool if for sequence
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "848d1a61-e860-47f5-b1c3-22061944402e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"It is ok.\n"
]
}
],
"source": [
"ok = True\n",
"if ok:\n",
" print(\"It is ok.\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3950ae92-8d04-4988-90ff-f460face851b",
"metadata": {},
"outputs": [],
"source": [
"ok = False\n",
"if ok:\n",
" print(\"It is ok.\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0ee1c307-a7df-46a4-8123-71e6b99cb8da",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"It is not ok.\n"
]
}
],
"source": [
"ok = False\n",
"if not ok:\n",
" print(\"It is not ok.\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "de714de4-0647-4576-89e8-2f70060eade8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"It is not ok.\n"
]
}
],
"source": [
"ok = False\n",
"if ok:\n",
" print(\"It is ok.\")\n",
"else:\n",
" print(\"It is not ok.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e086140d-a39c-491b-8795-feda5e6412db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"It is ok\n"
]
}
],
"source": [
"ok = True\n",
"if ok:\n",
" print(\"It is ok\")\n",
"else:\n",
" print(\"It is not ok.\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "af50de50-1791-4a40-99f8-54f92f55686d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n"
]
}
],
"source": [
"a = True\n",
"b = False\n",
"print(a, b, sep='\\n')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e496f5ab-536a-4f85-b917-018fe0be912e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n"
]
}
],
"source": [
"a = True\n",
"b = False\n",
"c = not a\n",
"d = not b\n",
"print(c, d, sep='\\n')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "ab587bb2-b3da-405c-ad0f-022e8f43c799",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n"
]
}
],
"source": [
"a = 2 < 3\n",
"b = 2 > 3\n",
"print(a, b, sep='\\n')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "db735fbb-aa69-4da7-b83b-16d609c8a72c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"x = 1\n",
"a = 2 < x\n",
"b = x < 10\n",
"c = a and b\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "08e945ac-0e6a-4055-8647-d5e6b8079ea1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"x = 5\n",
"a = 2 < x\n",
"b = x < 10\n",
"c = a and b\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "b99b7cfb-c207-4d5e-b730-ef87813255f1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"x = 11\n",
"a = 2 < x\n",
"b = x < 10\n",
"c = a and b\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "84349d71-f45f-4422-8c5e-6f0f9d825685",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"x = 3\n",
"a = x < 5\n",
"b = 7 < x\n",
"c = a or b\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "92c988de-6095-4c8a-9ad0-804d404d73db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"x = 6\n",
"a = x < 5\n",
"b = 7 < x\n",
"c = a or b\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1e2aadbe-e20a-4626-bcb4-b949287e2a39",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"x = 8\n",
"a = x < 5\n",
"b = 7 < x\n",
"c = a or b\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "284a6e4c-650d-42b2-8d84-913ff9bddb9f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"\n",
"True\n"
]
}
],
"source": [
"a = 5 < 5\n",
"print(a)\n",
"\n",
"print()\n",
"\n",
"b = 5 <= 5\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "1d15e976-df05-41c8-8e29-16fc4ca78f29",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n",
"8\n",
"7\n",
"6\n",
"5\n"
]
}
],
"source": [
"# for with list\n",
"a = [9, 8, 7, 6, 5]\n",
"for i in a:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "46472c4e-8f4a-4f1f-bedf-c521a0b42202",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n"
]
}
],
"source": [
"# for with range\n",
"a = range(1, 11, 2)\n",
"for i in a:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "b974818c-9786-40f3-ba60-436add4d8754",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n",
"b\n",
"c\n",
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"# for with string\n",
"a = 'abc123'\n",
"for i in a:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "a767dae8-87fd-4ce3-83f1-ff1d3dd8ca38",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[9, 8, 7, 6, 5]\n",
"range(20, 30, 2)\n",
"Hello\n"
]
}
],
"source": [
"# for with list of sequences\n",
"a = [9, 8, 7, 6, 5]\n",
"b = range(20, 30, 2)\n",
"c = 'Hello'\n",
"d = [a, b, c]\n",
"for i in d:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "e8fe248f-eebb-40a5-a21e-c0e54f58d067",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9 8 7 6 5 \n",
"\n",
"20 22 24 26 28 \n",
"\n",
"H e l l o \n",
"\n"
]
}
],
"source": [
"# for with list of sequences\n",
"a = [9, 8, 7, 6, 5]\n",
"b = range(20, 30, 2)\n",
"c = 'Hello'\n",
"d = [a, b, c]\n",
"for i in d:\n",
" for j in i:\n",
" print(j, end=' ')\n",
" print()\n",
" print()\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "cec2fcdb-0cce-4d1c-bb48-76e79947bc46",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 99\n",
"1 88\n",
"2 77\n",
"3 66\n",
"4 55\n"
]
}
],
"source": [
"# for with list and range\n",
"a = [99, 88, 77, 66, 55]\n",
"n = len(a)\n",
"b = range(0, n)\n",
"for i in b:\n",
" print(i, a[i])"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "d5d8c6e2-a5e2-47f0-9c34-fdf1ec70ed23",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 99\n",
"1 88\n",
"2 77\n",
"3 66\n",
"4 55\n"
]
}
],
"source": [
"# for enum with list\n",
"a = [99, 88, 77, 66, 55]\n",
"for i, val in enumerate(a):\n",
" print(i, val)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8b77fdbe-438f-4fc5-ad7d-93e72d891e75",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 a\n",
"30 n\n",
"40 r\n",
"70 y\n",
"90 z\n"
]
}
],
"source": [
"# for with two lists\n",
"a = [10, 30, 40, 70, 90]\n",
"b = ['a', 'n', 'r', 'y', 'z']\n",
"n = len(a)\n",
"r = range(0, n);\n",
"for i in r:\n",
" print(a[i], b[i])\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8e1941fb-ac88-4cd5-9ef9-2ab803c52e91",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 a\n",
"30 n\n",
"40 r\n",
"70 y\n",
"90 z\n"
]
}
],
"source": [
"# for with zip for two lists\n",
"a = [10, 30, 40, 70, 90]\n",
"b = ['a', 'n', 'r', 'y', 'z']\n",
"for i, j in zip(a, b):\n",
" print(i, j)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6a5a8c9d-3c59-43ac-a523-2431ef1a5502",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 a\n",
"30 n\n",
"40 r\n",
"70 y\n"
]
},
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mIndexError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 7\u001b[39m\n\u001b[32m 5\u001b[39m r = \u001b[38;5;28mrange\u001b[39m(\u001b[32m0\u001b[39m, n);\n\u001b[32m 6\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m r:\n\u001b[32m----> \u001b[39m\u001b[32m7\u001b[39m \u001b[38;5;28mprint\u001b[39m(a[i], \u001b[43mb\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m)\n",
"\u001b[31mIndexError\u001b[39m: list index out of range"
]
}
],
"source": [
"# for with two lists\n",
"a = [10, 30, 40, 70, 90]\n",
"b = ['a', 'n', 'r', 'y']\n",
"n = len(a)\n",
"r = range(0, n);\n",
"for i in r:\n",
" print(a[i], b[i])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "597449d4-6e35-4660-8ee1-4d5a2b590a3a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 a\n",
"30 n\n",
"40 r\n",
"70 y\n"
]
}
],
"source": [
"# for with two lists\n",
"a = [10, 30, 40, 70, 90]\n",
"b = ['a', 'n', 'r', 'y']\n",
"for i, j in zip(a, b):\n",
" print(i, j)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "db477c02-3aec-4d0b-8136-6ff022f6ea31",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 a @ True\n",
"30 n # False\n",
"40 r $ False\n",
"70 y % True\n"
]
}
],
"source": [
"# for with two lists\n",
"a = [10, 30, 40, 70, 90]\n",
"b = ['a', 'n', 'r', 'y']\n",
"c = ['@', '#', '$', '%']\n",
"d = [True, False, False, True]\n",
"for i, j, k, l in zip(a, b, c, d):\n",
" print(i, j, k, l)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e862b278-3eae-43d6-b0ef-ce9dfe4e6022",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
]
}
],
"source": [
"# assign list using for\n",
"r = range(0, 10)\n",
"x = []\n",
"for i in r:\n",
" x.append(i)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "18422412-06c6-4199-af85-4bc2910192df",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3, 5, 7, 9, 11, 13, 15, 17, 19, 21]\n"
]
}
],
"source": [
"# assign list using for\n",
"r = range(0, 10)\n",
"x = []\n",
"for i in r:\n",
" j = 2*i + 3\n",
" x.append(j)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "25b50b81-3238-4466-a784-d37926d8676a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[6, 2, 0, 0, 2, 6, 12, 20, 30, 42]\n"
]
}
],
"source": [
"# assign list using for\n",
"r = range(0, 10)\n",
"x = []\n",
"for i in r:\n",
" j = i**2 - 5*i + 6\n",
" x.append(j)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c957aba1-f867-489b-ab68-21de910bde18",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
]
}
],
"source": [
"# assign list comprehension\n",
"r = range (10)\n",
"x = [i for i in r]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c53241d2-9b29-4b36-92d5-47511a768244",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n"
]
}
],
"source": [
"# assign list comprehension\n",
"r = range (10)\n",
"x = [2*i+1 for i in r]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "63ead9b7-bfe8-4331-8f96-a64136605db0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
]
}
],
"source": [
"# assign list comprehension\n",
"r = range (10)\n",
"x = [i**2 + 2*i + 1 for i in r]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "5b43bfee-18ac-4395-9257-874871bbd2a6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 7, 12, 17, 22, 27, 32, 37, 42, 47]\n"
]
}
],
"source": [
"# assign list comprehension\n",
"x = [5*i+2 for i in range(10)]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "3493d8a8-bb4d-4707-acbd-af52f09e629a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"\n",
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
]
}
],
"source": [
"# assign list from range\n",
"x = [i for i in range(10)]\n",
"print(x)\n",
"\n",
"print()\n",
"\n",
"y = [*range(10)]\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "a6802001-cecf-4b15-a677-1ee85ef94b7f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\t3\t0\t0\n",
"1\t3\t-4\t1\n",
"2\t3\t-8\t4\n",
"3\t3\t-12\t9\n",
"4\t3\t-16\t16\n",
"5\t3\t-20\t25\n"
]
}
],
"source": [
"# assign list from list\n",
"x = [*range(6)]\n",
"y0 = [3 for i in x]\n",
"y1 = [-4*i for i in x]\n",
"y2 = [i**2 for i in x]\n",
"lists = zip(x, y0, y1, y2)\n",
"for i, j, k, l in lists:\n",
" print(i, j, k, l, sep='\\t')"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "8e402377-c783-4968-80bd-4c9c829895b9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 0, 0, 0, 0, 1, 1, 1, 1, 1]\n"
]
}
],
"source": [
"# assign for it\n",
"x = []\n",
"for i in range(0, 10):\n",
" if i < 5:\n",
" x.append(0)\n",
" else:\n",
" x.append(1)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "2b5ed17c-f4a9-4dde-bdf3-0d3ed26cc94e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 4, 3, 2, 1, 0]\n"
]
}
],
"source": [
"# assign for it\n",
"x = []\n",
"for i in range(0, 10):\n",
" if i < 5:\n",
" x.append(i)\n",
" else:\n",
" x.append(9-i)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "5112eca5-2a0f-4133-b8c2-33f33a782031",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 0, 0, 0, 0, 1, 1, 1, 1, 1]\n"
]
}
],
"source": [
"# assign for it list comprehension\n",
"r = range(10)\n",
"x = [0 if i < 5 else 1 for i in r]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "c8a89afe-748e-415b-b636-d4c6f06e4915",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 4, 3, 2, 1, 0]\n"
]
}
],
"source": [
"# assign for it list comprehension\n",
"r = range(10)\n",
"x = [i if i < 5 else 9-i for i in r]\n",
"print(x)"
]
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment