Skip to content

Instantly share code, notes, and snippets.

@dudung
Last active December 13, 2025 03:52
Show Gist options
  • Select an option

  • Save dudung/29a47f8ce5db918b9415e00b28d6cf6e to your computer and use it in GitHub Desktop.

Select an option

Save dudung/29a47f8ce5db918b9415e00b28d6cf6e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "06976e2f-d052-4545-874a-1dd2fe1b6610",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# 01\n",
"%reset -f\n",
"\n",
"print('a')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fefbb4ee-9284-4ba8-896e-9ea468703a51",
"metadata": {},
"outputs": [],
"source": [
"# 02\n",
"%reset -f\n",
"\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2701e5c7-31df-465c-974d-502c2b064c49",
"metadata": {},
"outputs": [],
"source": [
"# 03\n",
"%reset -f\n",
"\n",
"a = 'a'\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2387f101-d001-4735-b78e-093fdd41c8ca",
"metadata": {},
"outputs": [],
"source": [
"# 04\n",
"%reset -f\n",
"\n",
"print('Hello, World!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b548ba2-05ff-482e-ae8f-7966c1c561e0",
"metadata": {},
"outputs": [],
"source": [
"# 05\n",
"%reset -f\n",
"\n",
"print('Hello', end=', ')\n",
"print('World!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "633efa7f-a08b-437c-be24-eefd2def6510",
"metadata": {},
"outputs": [],
"source": [
"# 06\n",
"%reset -f\n",
"\n",
"print('Hello', 'world', sep=', ', end='!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56a5f26d-b49f-45bd-83de-7f83794cb528",
"metadata": {},
"outputs": [],
"source": [
"# 07\n",
"%reset -f\n",
"\n",
"print('a', 'b', 'c', 'd', 'e')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad31eb7d-c11f-4294-b4e8-6d4961266000",
"metadata": {},
"outputs": [],
"source": [
"# 08\n",
"%reset -f\n",
"\n",
"print('a', 'b', 'c', 'd', 'e', sep='')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7bd41823-606d-49ba-a366-eb9cc5f9c6e4",
"metadata": {},
"outputs": [],
"source": [
"# 09\n",
"%reset -f\n",
"\n",
"print('a', 'b', 'c', 'd', 'e', sep=';')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "258aa2f7-44f4-4109-8a98-5c5acfa4d739",
"metadata": {},
"outputs": [],
"source": [
"# 10\n",
"%reset -f\n",
"\n",
"print('a', 'b', 'c', 'd', 'e', sep='\\n')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "863f48a3-6c6a-4aee-870f-3a5a59420453",
"metadata": {},
"outputs": [],
"source": [
"# 11\n",
"%reset -f\n",
"\n",
"r = range(10)\n",
"print(r)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e3caa6c-7939-4611-83c9-0d1c7931890c",
"metadata": {},
"outputs": [],
"source": [
"# 12\n",
"%reset -f\n",
"\n",
"r = range(1, 10)\n",
"print(r)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1344d616-6e55-470b-be9d-208acc3c0521",
"metadata": {},
"outputs": [],
"source": [
"# 13\n",
"%reset -f\n",
"\n",
"r = range(2, 9, 3)\n",
"print(r)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6e1405c-eb38-418b-be05-80a32da685c2",
"metadata": {},
"outputs": [],
"source": [
"# 14\n",
"%reset -f\n",
"\n",
"r = range(2, 11, 3)\n",
"for i in r:\n",
" print(i, end=', ')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b36593c1-ed79-45ac-afa4-14efd0854f5f",
"metadata": {},
"outputs": [],
"source": [
"# 15\n",
"%reset -f\n",
"\n",
"r = range(10, 0, -2)\n",
"for i in r:\n",
" print(i, end=', ')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a26a5df-7c48-4e6d-8707-d58929ae7b3f",
"metadata": {},
"outputs": [],
"source": [
"# 16\n",
"%reset -f\n",
"\n",
"n = 5\n",
"r = range(1, n+1)\n",
"for i in r:\n",
" print('line', i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c3884aa-bc4d-413b-8247-926e7124d947",
"metadata": {},
"outputs": [],
"source": [
"# 17\n",
"%reset -f\n",
"\n",
"s = 0\n",
"n = 5\n",
"r = range(1, n+1)\n",
"for i in r:\n",
" s += i\n",
" print('sum =', s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f07cfaf-6487-4dad-9a0c-c16a9e0fb1ec",
"metadata": {},
"outputs": [],
"source": [
"# 18\n",
"%reset -f\n",
"\n",
"l = []\n",
"n = 5\n",
"r = range(1, n+1)\n",
"for i in r:\n",
" l.append(i)\n",
" print('list =', l)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7cd22b8-33cc-4e40-8af7-e80ef218ae5a",
"metadata": {},
"outputs": [],
"source": [
"# 19\n",
"%reset -f\n",
"\n",
"l = []\n",
"n = 5\n",
"r = range(1, n+1)\n",
"for i in r:\n",
" l.append(i)\n",
" s = sum(l)\n",
" print('sum of', l, 'is', s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67c72d1d-7018-4dc9-8666-27afb28a3642",
"metadata": {},
"outputs": [],
"source": [
"# 20\n",
"%reset -f\n",
"\n",
"l = []\n",
"n = 5\n",
"r = range(1, n+1)\n",
"for i in r:\n",
" l.append(i)\n",
" s = sum(l)\n",
" m = len(l)\n",
" avg = s /m\n",
" print('average of', l, 'is', avg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8259cedd-6dd5-4c93-81f3-4450f32475f9",
"metadata": {},
"outputs": [],
"source": [
"# 21\n",
"%reset -f\n",
"\n",
"r = range(1, 11)\n",
"print(r)\n",
"\n",
"x = [*r]\n",
"print('x =', x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1715b849-e390-4f61-a8bd-229c26668754",
"metadata": {},
"outputs": [],
"source": [
"# 22\n",
"%reset -f\n",
"\n",
"x = [*range(1, 11)]\n",
"s = 0\n",
"for i in x:\n",
" s += i\n",
"\n",
"print('x =', x)\n",
"print('sum x =', s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a3ba91ce-18f5-4b92-928b-ab149a3c27bc",
"metadata": {},
"outputs": [],
"source": [
"# 23\n",
"%reset -f\n",
"\n",
"def my_sum(x):\n",
" ss = 0\n",
" for i in x:\n",
" ss += i\n",
" return ss\n",
"\n",
"x = [*range(1, 11)]\n",
"s = my_sum(x)\n",
"\n",
"print('x =', x)\n",
"print('sum x =', s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2a48a8c-ccf7-40c0-83ce-b9cd4c82dbaa",
"metadata": {},
"outputs": [],
"source": [
"# 24\n",
"%reset -f\n",
"\n",
"x = [*range(1, 11)]\n",
"s = sum(x)\n",
"\n",
"print('x =', x)\n",
"print('sum x =', s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5759f201-fe63-4986-89f1-8aab2cadc09c",
"metadata": {},
"outputs": [],
"source": [
"# 25\n",
"%reset -f\n",
"\n",
"x = [*range(1, 11)]\n",
"s2 = 0\n",
"for i in x:\n",
" s2 += i*i\n",
"\n",
"print('x =', x)\n",
"print('sum x^2 =', s2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05a83d19-115a-493c-8b30-6205cbc4e0b6",
"metadata": {},
"outputs": [],
"source": [
"# 26\n",
"%reset -f\n",
"\n",
"def sum_square(x):\n",
" s2 = 0\n",
" for i in x:\n",
" s2 += i*i\n",
" return s2\n",
"\n",
"x = [*range(1, 11)]\n",
"s = sum_square(x)\n",
"\n",
"print('x =', x)\n",
"print('sum x^2 =', s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c211cd08-fe9d-4054-8236-d02cc8d58d85",
"metadata": {},
"outputs": [],
"source": [
"# 27\n",
"%reset -f\n",
"\n",
"x = [*range(0, 4)]\n",
"n = 3\n",
"sn = 0\n",
"for i in x:\n",
" xn = 1\n",
" for j in range(n):\n",
" xn *= i\n",
" sn += xn\n",
"\n",
"print('n =', n)\n",
"print('x =', x)\n",
"print('sum i^n = ', sn)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a3ae525b-9bdb-4ed2-962a-8d610807cfe9",
"metadata": {},
"outputs": [],
"source": [
"# 28\n",
"%reset -f\n",
"\n",
"def sum_power(x, n):\n",
" sn = 0\n",
" for i in x:\n",
" xn = 1\n",
" for j in range(n):\n",
" xn *= i\n",
" sn += xn\n",
" return sn\n",
"\n",
"x = [*range(0, 4)]\n",
"n = 3\n",
"sn = sum_power(x, n)\n",
"\n",
"print('n =', n)\n",
"print('x =', x)\n",
"print('sum i^n = ', sn)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "612ccdbf-a21b-4351-8f33-9b2f5c759c3f",
"metadata": {},
"outputs": [],
"source": [
"# 29\n",
"%reset -f\n",
"\n",
"def sum_power(x, n):\n",
" sn = 0\n",
" for i in x:\n",
" xn = 1\n",
" for j in range(n):\n",
" xn *= i\n",
" sn += xn\n",
" return sn\n",
"\n",
"x = [*range(0, 5)]\n",
"n = 2\n",
"sn = sum_power(x, n)\n",
"\n",
"print('n =', n)\n",
"print('x =', x)\n",
"print('sum i^n = ', sn)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8071aa4-0982-4d4c-9b0f-661a12611336",
"metadata": {},
"outputs": [],
"source": [
"# 30\n",
"%reset -f\n",
"\n",
"def sum_power(x, n):\n",
" sn = 0\n",
" for i in x:\n",
" xn = 1\n",
" for j in range(n):\n",
" xn *= i\n",
" sn += xn\n",
" return sn\n",
"\n",
"x = [*range(0, 11)]\n",
"n = 1\n",
"sn = sum_power(x, n)\n",
"\n",
"print('n =', n)\n",
"print('x =', x)\n",
"print('sum i^n = ', sn)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "629ad1f5-f648-4050-9062-5c5e53bba5c5",
"metadata": {},
"outputs": [],
"source": [
"# 31\n",
"%reset -f\n",
"\n",
"a = 1\n",
"b = -10\n",
"c = 16\n",
"coefs = [c, b, a]\n",
"\n",
"D = b**2 - 4*a*c\n",
"x1 = (-b - D**0.5) / (2*a)\n",
"x2 = (-b + D**0.5) / (2*a)\n",
"\n",
"print('quadratic function =', coefs)\n",
"print('root x1 =', x1)\n",
"print('root x2 =', x2)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "809f9ca9-ee7d-4052-b95d-645c3daea86f",
"metadata": {},
"outputs": [],
"source": [
"# 32\n",
"%reset -f\n",
"\n",
"def quad_roots(coefs):\n",
" a = coefs[0]\n",
" b = coefs[1]\n",
" c = coefs[2]\n",
" \n",
" D = b**2 - 4*a*c\n",
" x1 = (-b - D**0.5) / (2*a)\n",
" x2 = (-b + D**0.5) / (2*a)\n",
" \n",
" return x1, x2\n",
"\n",
"coefs = [1, -9, 20]\n",
"print('quadratic function =', coefs)\n",
"x1, x2 = quad_roots(coefs)\n",
"print('root x1 =', x1)\n",
"print('root x2 =', x2)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55a21e32-bb50-4a68-8b6c-12bfe2a207be",
"metadata": {},
"outputs": [],
"source": [
"# 33\n",
"%reset -f\n",
"\n",
"n = 21\n",
"xmin = 0\n",
"xmax = 10\n",
"dx = (xmax - xmin) / (n - 1)\n",
"x = []\n",
"for i in range(n):\n",
" x.append(xmin + i * dx)\n",
"\n",
"print('xmin =', xmin)\n",
"print('xmax =', xmax)\n",
"print('n =', n)\n",
"print('x =', x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41725322-8951-4695-8a3c-4427bd9b9b2f",
"metadata": {},
"outputs": [],
"source": [
"# 34\n",
"%reset -f\n",
"\n",
"def gen_x(xmin, xmax, n):\n",
" dx = (xmax - xmin) / (n - 1)\n",
" x = []\n",
" for i in range(n):\n",
" x.append(xmin + i * dx)\n",
" return x\n",
"\n",
"n = 21\n",
"xmin = 0\n",
"xmax = 10\n",
"x = gen_x(xmin, xmax, n)\n",
"\n",
"print('xmin =', xmin)\n",
"print('xmax =', xmax)\n",
"print('n =', n)\n",
"print('x =', x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "daf742f3-31fd-4a8e-866b-44d23e1e8cf8",
"metadata": {},
"outputs": [],
"source": [
"# 35\n",
"\n",
"n = 11\n",
"xmin = 0\n",
"xmax = 10\n",
"x = gen_x(xmin, xmax, n)\n",
"\n",
"print('xmin =', xmin)\n",
"print('xmax =', xmax)\n",
"print('n =', n)\n",
"print('x =', x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf80809f-6f83-4335-8c56-1528ac81fe4a",
"metadata": {},
"outputs": [],
"source": [
"# 36\n",
"\n",
"n = 11\n",
"xmin = 0\n",
"xmax = 10\n",
"x = gen_x(xmin, xmax, n)\n",
"\n",
"coefs = [18, -11, 1]\n",
"y = []\n",
"for i in x:\n",
" y0 = coefs[0]\n",
" y1 = coefs[1] * i\n",
" y2 = coefs[2] * i*i\n",
" y.append(y0 + y1 + y2)\n",
"\n",
"print('coefs =', coefs)\n",
"print('x =', x)\n",
"print('y =', y)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "322ec4c6-9379-4ed6-827a-742feb04c48b",
"metadata": {},
"outputs": [],
"source": [
"# 37\n",
"\n",
"def gen_y(coefs, x):\n",
" y = []\n",
" for i in x:\n",
" y0 = coefs[0]\n",
" y1 = coefs[1] * i\n",
" y2 = coefs[2] * i*i\n",
" y.append(y0 + y1 + y2)\n",
" return y\n",
"\n",
"n = 11\n",
"xmin = 0\n",
"xmax = 10\n",
"x = gen_x(xmin, xmax, n)\n",
"\n",
"coefs = [18, -11, 1]\n",
"y = gen_y(coefs, x)\n",
"\n",
"print('coefs =', coefs)\n",
"print('x =', x)\n",
"print('y =', y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2377c506-9211-409a-97d0-ef3400c1a1ac",
"metadata": {},
"outputs": [],
"source": [
"# 38\n",
"\n",
"n = 11\n",
"xmin = 0\n",
"xmax = 10\n",
"x = gen_x(xmin, xmax, n)\n",
"\n",
"coefs = [18, -11, 1]\n",
"y = gen_y(coefs, x)\n",
"\n",
"import matplotlib.pyplot as plt\n",
"plt.plot(x, y, '.-')\n",
"plt.xlabel('x')\n",
"plt.ylabel('y')\n",
"plt.plot([xmin, xmax], [0, 0])\n",
"plt.grid()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c53e7ed-0cdf-4a5a-b787-4638a357258f",
"metadata": {},
"outputs": [],
"source": [
"# 39\n",
"\n",
"n = 21\n",
"xmin = 0\n",
"xmax = 10\n",
"x = gen_x(xmin, xmax, n)\n",
"\n",
"coefs = [18, -11, 1]\n",
"y = gen_y(coefs, x)\n",
"\n",
"import matplotlib.pyplot as plt\n",
"plt.plot(x, y, '.-')\n",
"plt.xlabel('x')\n",
"plt.ylabel('y')\n",
"plt.plot([xmin, xmax], [0, 0])\n",
"plt.grid()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e17faa6-db74-4269-8dd7-ed270faa6f19",
"metadata": {},
"outputs": [],
"source": [
"# 40\n",
"\n",
"n = 101\n",
"xmin = 0\n",
"xmax = 10\n",
"x = gen_x(xmin, xmax, n)\n",
"\n",
"coefs = [18, -11, 1]\n",
"y = gen_y(coefs, x)\n",
"\n",
"import matplotlib.pyplot as plt\n",
"plt.plot(x, y, '.-')\n",
"plt.xlabel('x')\n",
"plt.ylabel('y')\n",
"plt.xlim(1, 3)\n",
"plt.plot([xmin, xmax], [0, 0])\n",
"plt.grid()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3e314aa2-2501-4942-831a-ee1a2ae8e3e5",
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment