Skip to content

Instantly share code, notes, and snippets.

@hvnsweeting
Created March 3, 2022 15:19
Show Gist options
  • Save hvnsweeting/53e4439387f297421bcd819e954807f0 to your computer and use it in GitHub Desktop.
Save hvnsweeting/53e4439387f297421bcd819e954807f0 to your computer and use it in GitHub Desktop.
Học lập trình Python Hà Nội Sài Gòn TPHCM 2022 tại https://pymi.vn
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "9aa3cde2",
"metadata": {},
"source": [
"# Pymi.vn Lesson 2"
]
},
{
"cell_type": "markdown",
"id": "01d4cdaf",
"metadata": {},
"source": [
"## cách học ở PYmi\n",
"- tài liệu: https://docs.python.org/3/tutorial/index.html\n",
"- học viên gõ theo trên bảng\n",
"- không ghi chép, bài giảng gửi sau buổi học\n",
"- đây là lớp thực hành "
]
},
{
"cell_type": "markdown",
"id": "a8474d25",
"metadata": {},
"source": [
"## floor division"
]
},
{
"cell_type": "markdown",
"id": "31d28de3",
"metadata": {},
"source": [
"## number (cont)x"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e3d8ed2b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"17 // 3"
]
},
{
"cell_type": "markdown",
"id": "fddfd239",
"metadata": {},
"source": [
"## remainder"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bf710b1d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"17 % 5 "
]
},
{
"cell_type": "markdown",
"id": "399c1f06",
"metadata": {},
"source": [
"## ứng dụng? \n",
"- gỉam kích thước bài toán có tính chu kỳ"
]
},
{
"cell_type": "markdown",
"id": "6f648e5b",
"metadata": {},
"source": [
"## năm nay 2022 - nhâm dần "
]
},
{
"cell_type": "markdown",
"id": "95307bb1",
"metadata": {},
"source": [
"### 1992 -> nhâm thân "
]
},
{
"cell_type": "markdown",
"id": "dfcff379",
"metadata": {},
"source": [
"## 2034 -> gì dần "
]
},
{
"cell_type": "markdown",
"id": "79c60c24",
"metadata": {},
"source": [
"## 1369 ???"
]
},
{
"cell_type": "markdown",
"id": "52da5caf",
"metadata": {},
"source": [
"## địa chi \n",
"- ngọ mùi thân dậu tuất hợi tí sửu dần mão thìn tỵ"
]
},
{
"cell_type": "markdown",
"id": "32b215b6",
"metadata": {},
"source": [
"## thiên can\n",
"- canh 0 tân 1 nhâm 2 quý 3 giáp4 ất5 bính6 đinh7 mậu8 kỷ9"
]
},
{
"cell_type": "markdown",
"id": "b5597d02",
"metadata": {},
"source": [
"https://canchi.pymi.vn/"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0d8ed8c5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(2022 - 1369) % 12"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "405f1b0b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "0f61ff31",
"metadata": {},
"source": [
"## số phức: complex"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2e6a2b3b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3+5j)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 + 5j"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "db0debda",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"complex"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(3+5j)"
]
},
{
"cell_type": "markdown",
"id": "86a1ef22",
"metadata": {},
"source": [
"## Comparision\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d7bb95c8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 < 3"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5d142414",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 > 3"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "be3e1291",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"bool"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(1 > 3)"
]
},
{
"cell_type": "markdown",
"id": "b1d1b0cf",
"metadata": {},
"source": [
"## bool - boolean"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "fc8d8b86",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 > 3"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "5d711b11",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 < 3"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "6f2ccba6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "79277421",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"bool"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(False)"
]
},
{
"cell_type": "markdown",
"id": "814bdeb6",
"metadata": {},
"source": [
"## chú ý : viết hoa chữ cái đầu tiên"
]
},
{
"cell_type": "markdown",
"id": "cc5d7889",
"metadata": {},
"source": [
"### operators"
]
},
{
"cell_type": "markdown",
"id": "d5578489",
"metadata": {},
"source": [
"## not"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "f2df3f1a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not True "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2855809a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 19,
"id": "22d74446",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not False"
]
},
{
"cell_type": "markdown",
"id": "a130526e",
"metadata": {},
"source": [
"## dont' do this"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "6ca6471a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not True == False"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "b4fed99d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not False == True"
]
},
{
"cell_type": "markdown",
"id": "8fa3129b",
"metadata": {},
"source": [
"## do tthis"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "ed30872e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not True is False"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "c46a476a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not False is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "680424eb",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "7209557e",
"metadata": {},
"source": [
"## and"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "a6108c2a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True and True "
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "f6c2d496",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True and False "
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "485775e9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False and True "
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "52bbacdb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False and False "
]
},
{
"cell_type": "markdown",
"id": "e4ebffbf",
"metadata": {},
"source": [
"## or"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "8c65c2a6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True or True "
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "03aeff0c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True or False"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "f4b93bec",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False or True"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "01a0c7a6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False or False "
]
},
{
"cell_type": "markdown",
"id": "9a76f3e6",
"metadata": {},
"source": [
"## Câu hỏi phỏng vấn"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "9a8bb6af",
"metadata": {
"scrolled": true
},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_141470/3147863620.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mTrue\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"True and 1/0"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b0b7995",
"metadata": {},
"outputs": [],
"source": [
"1/0 and True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "046aa36c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 37,
"id": "8d2ff8ad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False and 1/0"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "a6a3b44c",
"metadata": {},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_141470/1311455965.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"1/0 and False"
]
},
{
"cell_type": "markdown",
"id": "25188e63",
"metadata": {},
"source": [
"## Boolean Operations — and, or, not\n",
"\n",
"These are the Boolean operations, ordered by ascending priority:\n",
"\n",
"Operation\n",
"\t\n",
"\n",
"Result\n",
"\t\n",
"\n",
"Notes\n",
"\n",
"x or y\n",
"\t\n",
"\n",
"if x is false, then y, else x\n",
"\t\n",
"\n",
"(1)\n",
"\n",
"x and y\n",
"\t\n",
"\n",
"if x is false, then x, else y\n",
"\t\n",
"\n",
"(2)\n",
"\n",
"not x\n",
"\t\n",
"\n",
"if x is false, then True, else False\n",
"\t\n",
"\n",
"(3)\n",
"\n",
"Notes:\n",
"\n",
" This is a short-circuit operator, so it only evaluates the second argument if the first one is false.\n",
"\n",
" This is a short-circuit operator, so it only evaluates the second argument if the first one is true.\n",
"\n",
" not has a lower priority than non-Boolean operators, so not a == b is interpreted as not (a == b), and a == not b is a syntax error.\n"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "257aad32",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True or 1/0"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "32d44bec",
"metadata": {},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_141470/2638778418.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"1/0 or True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5d0714b",
"metadata": {},
"outputs": [],
"source": [
"False or 1/0"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a396024f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "1a90a186",
"metadata": {},
"source": [
"## short-circuit\n",
"\n",
"## Dừng lại ngay khi có thể"
]
},
{
"cell_type": "markdown",
"id": "acc165c1",
"metadata": {},
"source": [
"## and: dừng lại khi gặp False\n",
"## or: dừng lại khi gặp True"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "053dd079",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False or True "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "95df7a20",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "30ede1e1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 41,
"id": "cc981196",
"metadata": {
"scrolled": true
},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_141470/812870342.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mFalse\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"False or 1/0"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "08d2428f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True or 1/0"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "908deac0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False and 1/0"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "e39417d1",
"metadata": {
"scrolled": true
},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_141470/3147863620.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mTrue\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"True and 1/0"
]
},
{
"cell_type": "markdown",
"id": "87c94662",
"metadata": {},
"source": [
"## bí mật (~ ko dùng)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "83938555",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True + True "
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "5c01c8c2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True == 1"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "9d5a540d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"False == 0"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "fab91ae8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 != 2"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "e012e353",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 <= 1"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "550bf740",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 <= 2 <= 3"
]
},
{
"cell_type": "markdown",
"id": "2fda3445",
"metadata": {},
"source": [
"http://pymi.vn/tutorial/boolean/"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "5798274c",
"metadata": {},
"outputs": [],
"source": [
"import math"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "710c0384",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.8939966636005579"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.sin(90)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "a3288d54",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.sin(math.pi/2)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "25cdf4f2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6.123233995736766e-17"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.cos(math.pi/2)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "7c36b421",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.141592653589793"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.pi"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "b7f6040e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.643856189774724"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.log2(25)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75ee6105",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "1d6d416b",
"metadata": {},
"source": [
"## variable/names"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "3299793e",
"metadata": {},
"outputs": [],
"source": [
"x = 5\n",
"y = 7\n",
"z = x + y\n"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "0a2b0fd9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z"
]
},
{
"cell_type": "markdown",
"id": "c098dcb5",
"metadata": {},
"source": [
"## list"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "4b85d141",
"metadata": {},
"outputs": [],
"source": [
"xs = [1,2,3]"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "d3930096",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(xs)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "86900076",
"metadata": {},
"outputs": [],
"source": [
"ns = [1,1,23,12,312,3,123,12,31,2,132,31,23,123,12,3,12,123,\n",
" 3123,12,312,3,123,12,3,213,12,312,3,12,31,23,123,1,2]"
]
},
{
"cell_type": "markdown",
"id": "cb46ad20",
"metadata": {},
"source": [
"## len - length"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "07ea8356",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"35"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(ns)"
]
},
{
"cell_type": "markdown",
"id": "7f96e2a0",
"metadata": {},
"source": [
"## empty list"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "38a1f07b",
"metadata": {},
"outputs": [],
"source": [
"ns = []"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "0a275f3c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(ns)"
]
},
{
"cell_type": "markdown",
"id": "81ef2b86",
"metadata": {},
"source": [
"## sum"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "4de5cfbf",
"metadata": {},
"outputs": [],
"source": [
"ns = [1,2,3,4,5,6,7,8,9]"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "dd96bd01",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"45"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(ns)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "80aaf38b",
"metadata": {},
"outputs": [],
"source": [
"ns = [5,6,7,8,9,10,\n",
" 11,12,13,14,15,16,17,18,19,20,\n",
" 21,22,23,24,25,26,27,28,29,30,\n",
" 31,32,33,34,35,36,37,38,39,40,\n",
" 41,42,43,44,45,46,47,48,49,50,\n",
" 51,52,53,54,55,56,57,58,59,60,\n",
" 61,62,63,64,65,66,67,68,69,70,\n",
" 71,72,73,74,75,76,77,78,79,80,\n",
" 81,82,83,84,85,86,87,88,89,90,\n",
" 91,92,93,94,95]"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "5b4ed4c4",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"4550"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(ns)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "83a23c10",
"metadata": {},
"outputs": [],
"source": [
"ns = list(range(5, 95+1))"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "52684578",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95]\n"
]
}
],
"source": [
"print(ns)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "065facec",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4550"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(ns)"
]
},
{
"cell_type": "markdown",
"id": "3d9bcf6c",
"metadata": {},
"source": [
"## range(5, 95+1) \n",
"chú ý: range từ bắt đầu, đến kết thúc + 1"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "7a0c6193",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"500500"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(list(range(1, 1000+1)))"
]
},
{
"cell_type": "markdown",
"id": "d00a6840",
"metadata": {},
"source": [
"## BTVN: chữa buổi 12: tổng từ 1 đến 1 tỷ"
]
},
{
"cell_type": "markdown",
"id": "5ea9151b",
"metadata": {},
"source": [
"## list"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "fcd0987d",
"metadata": {},
"outputs": [],
"source": [
"ns = [1,3,2]"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "419b27d9",
"metadata": {},
"outputs": [],
"source": [
"ns = [1, False, True, 1.5, 2]"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "2ade65e1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, False, True, 1.5, 2]\n"
]
}
],
"source": [
"print(ns)"
]
},
{
"cell_type": "code",
"execution_count": 90,
"id": "524e59dc",
"metadata": {},
"outputs": [],
"source": [
"ns = [1, [1,2, [1,2,[1]],1], 1]"
]
},
{
"cell_type": "markdown",
"id": "d767e58a",
"metadata": {},
"source": [
"## list có thể chứa mọi thứ"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "dc8116dd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(ns)"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "4889b2ca",
"metadata": {},
"outputs": [],
"source": [
"xs = [4,10, 23, 50]"
]
},
{
"cell_type": "markdown",
"id": "ce0cfc2f",
"metadata": {},
"source": [
"## indexing: đếm bắt đầu từ 0"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "78e0b93c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[0]"
]
},
{
"cell_type": "markdown",
"id": "953fc0af",
"metadata": {},
"source": [
"## index - 0"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "83afba7d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[1]"
]
},
{
"cell_type": "code",
"execution_count": 95,
"id": "5a605a16",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[2]"
]
},
{
"cell_type": "code",
"execution_count": 96,
"id": "9627f5ea",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"50"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[3]"
]
},
{
"cell_type": "code",
"execution_count": 97,
"id": "f5f804c3",
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_141470/3875768526.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mxs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"xs[4]"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "d4029789",
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_141470/1777072514.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mxs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"xs[5]"
]
},
{
"cell_type": "markdown",
"id": "56c44daa",
"metadata": {},
"source": [
"https://www.familug.org/2016/02/python-python-tuoi-gi.html"
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "d6bb2bff",
"metadata": {},
"outputs": [],
"source": [
"xs = [4,10, 23, 50]"
]
},
{
"cell_type": "code",
"execution_count": 103,
"id": "88454301",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 50]"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[xs[1], xs[3]]"
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "f80772ea",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"50"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[len(xs)-1]"
]
},
{
"cell_type": "markdown",
"id": "26c1e0ce",
"metadata": {},
"source": [
"## negative index"
]
},
{
"cell_type": "code",
"execution_count": 105,
"id": "3af38bad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"50"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[-1]"
]
},
{
"cell_type": "markdown",
"id": "bbc57620",
"metadata": {},
"source": [
"## change"
]
},
{
"cell_type": "code",
"execution_count": 107,
"id": "9fa9c3d6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 10, 23, 50]"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs"
]
},
{
"cell_type": "code",
"execution_count": 108,
"id": "46836c3e",
"metadata": {},
"outputs": [],
"source": [
"xs[-1] = 100"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "364dbcfe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 10, 23, 100]"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "8661b0db",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 10, 23, 100]"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "e4afbf13",
"metadata": {},
"outputs": [],
"source": [
"xs = list(range(10, 50))"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "515cccf6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]\n"
]
}
],
"source": [
"print(xs)"
]
},
{
"cell_type": "markdown",
"id": "fb68c687",
"metadata": {},
"source": [
"## slicing"
]
},
{
"cell_type": "markdown",
"id": "bd394475",
"metadata": {},
"source": [
"## SLICE[START:STOP]"
]
},
{
"cell_type": "code",
"execution_count": 124,
"id": "d5ee797b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
]
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[0:10]"
]
},
{
"cell_type": "code",
"execution_count": 117,
"id": "d7b84749",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[12, 13, 14, 15, 16, 17, 18, 19]"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[2:10]"
]
},
{
"cell_type": "code",
"execution_count": 118,
"id": "a717bc20",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[19]"
]
},
"execution_count": 118,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[9:10]"
]
},
{
"cell_type": "markdown",
"id": "a923a5f6",
"metadata": {},
"source": [
"## ko exception"
]
},
{
"cell_type": "code",
"execution_count": 119,
"id": "834a3b05",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[1000:10]"
]
},
{
"cell_type": "code",
"execution_count": 121,
"id": "6c83d1dc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]\n"
]
}
],
"source": [
"print(xs)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"id": "b40fb0c3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[len(xs)-10:len(xs)]"
]
},
{
"cell_type": "code",
"execution_count": 123,
"id": "98dbf445",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[-10:]"
]
},
{
"cell_type": "code",
"execution_count": 125,
"id": "14e973d1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[0:10]"
]
},
{
"cell_type": "code",
"execution_count": 126,
"id": "93e3a253",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs[:10]"
]
},
{
"cell_type": "code",
"execution_count": 128,
"id": "da5f220a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]\n"
]
}
],
"source": [
"print(xs[:])"
]
},
{
"cell_type": "code",
"execution_count": 131,
"id": "e1019448",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]\n"
]
}
],
"source": [
"print(xs)"
]
},
{
"cell_type": "markdown",
"id": "9c13e2f2",
"metadata": {},
"source": [
"### slice[Start:stop:step]"
]
},
{
"cell_type": "code",
"execution_count": 133,
"id": "dbd2f46e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]\n"
]
}
],
"source": [
"print(xs[::2])"
]
},
{
"cell_type": "code",
"execution_count": 134,
"id": "50de6783",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]\n"
]
}
],
"source": [
"print(xs[1::2])"
]
},
{
"cell_type": "markdown",
"id": "ccfbc2c5",
"metadata": {},
"source": [
"## BT: tính tổng các số < 1000 chia hết cho 3 hoặc 5"
]
},
{
"cell_type": "code",
"execution_count": 135,
"id": "0606df9e",
"metadata": {},
"outputs": [],
"source": [
"xs = list(range(1000))"
]
},
{
"cell_type": "code",
"execution_count": 136,
"id": "956993e7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n"
]
}
],
"source": [
"print(xs[:100])"
]
},
{
"cell_type": "code",
"execution_count": 149,
"id": "0551bd03",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"233168"
]
},
"execution_count": 149,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs3 = xs[::3]\n",
"xs5 = xs[::5]\n",
"xs15 = xs[::15]\n",
"sum(xs3) + sum(xs5) - sum(xs15)"
]
},
{
"cell_type": "code",
"execution_count": 147,
"id": "f449a8d3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]\n"
]
}
],
"source": [
"print(xs3[:10])"
]
},
{
"cell_type": "code",
"execution_count": 148,
"id": "cc81ff88",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]\n"
]
}
],
"source": [
"print(xs5[:10])"
]
},
{
"cell_type": "markdown",
"id": "0fb5e399",
"metadata": {},
"source": [
"## BTVN: doc https://docs.python.org/3/tutorial/introduction.html#lists"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9860b86e",
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment