Created
May 17, 2023 01:35
-
-
Save hvnsweeting/4983238cabb8b2fefda05b75763b8702 to your computer and use it in GitHub Desktop.
Học lập trình Python Hà Nội Sài Gòn online lớp https://pymi.vn khóa 44 bài 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "id": "2ffbc089", | |
| "metadata": {}, | |
| "source": [ | |
| "# Lesson 2 - pymi.vn" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "56816d2e", | |
| "metadata": {}, | |
| "source": [ | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "8f137dd3", | |
| "metadata": {}, | |
| "source": [ | |
| "https://projecteuler.net/problem=1\n", | |
| "\n", | |
| "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.\n", | |
| "\n", | |
| "Find the sum of all the multiples of 3 or 5 below 1000.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "1c9c321b", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "233168" | |
| ] | |
| }, | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sum(i for i in range(1000)\n", | |
| " if i % 3 == 0 or i % 5 ==0)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "e4d17f3d", | |
| "metadata": {}, | |
| "source": [ | |
| "## Modulo/remainder - `%` chia lấy dư" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "id": "f28483f6", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "0" | |
| ] | |
| }, | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "4 % 2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "c9973243", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "0" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "5 % 1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "id": "2b81d41f", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "7 % 5" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "b80a2241", | |
| "metadata": {}, | |
| "source": [ | |
| "## 2023 - Quý Mão " | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "341c5ae3", | |
| "metadata": {}, | |
| "source": [ | |
| "## Bài toán có tính chất chu kỳ " | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "979200bb", | |
| "metadata": {}, | |
| "source": [ | |
| "## Thiên can - địa chi \n", | |
| "### Can - Chi" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "7b9c1959", | |
| "metadata": {}, | |
| "source": [ | |
| "### Địa chi: 12 \n", | |
| "- Tí \n", | |
| "- Sửu\n", | |
| "- Dần\n", | |
| "- Mão\n", | |
| "- Thìn\n", | |
| "- Tỵ\n", | |
| "- Ngọ\n", | |
| "- Mùi\n", | |
| "- Thân\n", | |
| "- Dậu\n", | |
| "- Tuất\n", | |
| "- Hợi " | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "d282db2b", | |
| "metadata": {}, | |
| "source": [ | |
| "### Thiên can - 10" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "237eb4d9", | |
| "metadata": {}, | |
| "source": [ | |
| "- 0 Canh\n", | |
| "- 1 Tân\n", | |
| "- 2 Nhâm\n", | |
| "- 3 Quý\n", | |
| "- 4 Giáp\n", | |
| "- 5 ẤT\n", | |
| "- 6 Bính\n", | |
| "- 7 Đinh\n", | |
| "- 8 Mậu\n", | |
| "- 9 Kỷ" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "75b0ec16", | |
| "metadata": {}, | |
| "source": [ | |
| "## 1369 là năm gì?" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "id": "0e4ae0db", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "9" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "(2023-1990)%12" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "id": "88e7ee02", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "6" | |
| ] | |
| }, | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "(2023-1369)%12" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "5a84ba08", | |
| "metadata": {}, | |
| "source": [ | |
| "## 1369 - kỷ dậu" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "fa3fa84e", | |
| "metadata": {}, | |
| "source": [ | |
| "https://canchi.pymi.vn/" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "be3484f6", | |
| "metadata": {}, | |
| "source": [ | |
| "### giảm kích thước bài toán có tính chu kỳ " | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "aea8dabb", | |
| "metadata": {}, | |
| "source": [ | |
| "## so sánh" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "id": "63bb5e82", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "1 < 2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "id": "1bb8fa46", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "1 == 2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "id": "82db8869", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "1 > 2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "id": "34e40149", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "1 != 2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "id": "663d62a5", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "1 <=2 " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "id": "58257e59", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "1 <= 1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "id": "23295a88", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "1==1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "id": "aefc8b41", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "bool" | |
| ] | |
| }, | |
| "execution_count": 17, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "type(1==1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "ead95d03", | |
| "metadata": {}, | |
| "source": [ | |
| "## clean /kliːn/\n", | |
| "## boolean /ˈbuːliən/" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "47c60a7d", | |
| "metadata": {}, | |
| "source": [ | |
| "### lean -> clean - lin? -> bu lin? " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 18, | |
| "id": "8b945c79", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 18, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "True" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "id": "db28f8f4", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "False" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "4391c5b5", | |
| "metadata": {}, | |
| "source": [ | |
| "### chữ T chữ F viết hoa" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "8223b3c9", | |
| "metadata": {}, | |
| "source": [ | |
| "### 3 phép toán với boolean" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "62b10883", | |
| "metadata": {}, | |
| "source": [ | |
| "### not" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 22, | |
| "id": "b1c8ed6c", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "execution_count": 22, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "not True " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 23, | |
| "id": "6fb88a4e", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 23, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "not False" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "40113e1e", | |
| "metadata": {}, | |
| "source": [ | |
| "### and" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 24, | |
| "id": "4871b734", | |
| "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": "28f089f3", | |
| "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": "28c734f3", | |
| "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": "94e950a4", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "execution_count": 27, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "False and False" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "64d076e2", | |
| "metadata": {}, | |
| "source": [ | |
| "### or - hoặc" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 28, | |
| "id": "72a195ea", | |
| "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": "e8937870", | |
| "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": "35c54502", | |
| "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": "e3d2229e", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "execution_count": 31, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "False or False" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "ea210824", | |
| "metadata": {}, | |
| "source": [ | |
| "## Kiểm tra " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 32, | |
| "id": "01464ef9", | |
| "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)", | |
| "Cell \u001b[0;32mIn[32], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mTrue\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n", | |
| "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "True and 1/0" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 33, | |
| "id": "75ab8415", | |
| "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": "1a5a0619", | |
| "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)", | |
| "Cell \u001b[0;32mIn[34], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m\n", | |
| "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "1/0 or True" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 35, | |
| "id": "be30dce2", | |
| "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)", | |
| "Cell \u001b[0;32mIn[35], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mFalse\u001b[39;00m \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n", | |
| "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "False or 1/0" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "24bb189a", | |
| "metadata": {}, | |
| "source": [ | |
| "## Tính short circuit của boolean trong Python" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "4b313d9f", | |
| "metadata": {}, | |
| "source": [ | |
| "### dừng lại ngay khi có thể" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "860dafd2", | |
| "metadata": {}, | |
| "source": [ | |
| "- and: dừng lại khi gặp False\n", | |
| "- or: dừng lại khi gặp True" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "9cb017ec", | |
| "metadata": {}, | |
| "source": [ | |
| "https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "530d2995", | |
| "metadata": {}, | |
| "source": [ | |
| "## string" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 36, | |
| "id": "9ec59cee", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "s = 'Python'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 37, | |
| "id": "9236e604", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "s2 = \"Python\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "f4436708", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "s == s2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 38, | |
| "id": "07026553", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "str" | |
| ] | |
| }, | |
| "execution_count": 38, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "type(s)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "56cdcac6", | |
| "metadata": {}, | |
| "source": [ | |
| "## Python uk /ˈpaɪ.θən/ us /ˈpaɪ.θɑːn/" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "6de756e4", | |
| "metadata": {}, | |
| "source": [ | |
| "### `'` - nháy đơn? - 1 nháy - single quote\n", | |
| "### `\"` - nháy kép? - 2 nháy - double quotes\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 39, | |
| "id": "043bb364", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "SyntaxError", | |
| "evalue": "unterminated string literal (detected at line 1) (2558857633.py, line 1)", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[0;36m Cell \u001b[0;32mIn[39], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m s = 'Hello, I'm Peter'\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unterminated string literal (detected at line 1)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "s = 'Hello, I'm Peter'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 40, | |
| "id": "37071c64", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "s = \"Hello, I'm Peter\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 41, | |
| "id": "98f1d376", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Hello, I'm Peter\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(s)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "85ee7fd6", | |
| "metadata": {}, | |
| "source": [ | |
| "## escape quote `\\`" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 42, | |
| "id": "fab2c664", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "s = 'Hello, I\\'m Peter'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 43, | |
| "id": "767d0896", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Hello, I'm Peter\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(s)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 44, | |
| "id": "05fb8143", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "SyntaxError", | |
| "evalue": "unterminated string literal (detected at line 1) (2588085788.py, line 1)", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[0;36m Cell \u001b[0;32mIn[44], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m s = 'Cháu lên ba\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unterminated string literal (detected at line 1)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "s = 'Cháu lên ba\n", | |
| "Cháu đi mẫu giáo'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "eae7fec3", | |
| "metadata": {}, | |
| "source": [ | |
| "## xuống dòng là gì? `\\n` - newline" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "84cb9042", | |
| "metadata": {}, | |
| "source": [ | |
| "## windows - `\\r\\n` - carridge return - newline" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 46, | |
| "id": "64c214d2", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Cháu lên ba\n", | |
| "Cháu đi mẫu giáo\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "s = 'Cháu lên ba\\nCháu đi mẫu giáo'\n", | |
| "\n", | |
| "print(s)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 47, | |
| "id": "69696169", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "a\n", | |
| "b\n", | |
| "c\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print('a\\nb\\nc')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "b03fdfd6", | |
| "metadata": {}, | |
| "source": [ | |
| "### tab - `\\t`" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 48, | |
| "id": "84fdba48", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "a\tb\tc\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print('a\\tb\\tc')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 51, | |
| "id": "be7950e6", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "a b c\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print('a b c')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "c941dbe7", | |
| "metadata": {}, | |
| "source": [ | |
| "### Triple quotes - 3 nháy" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 52, | |
| "id": "1f49476a", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "baitho = \"\"\"Cháu lên ba\n", | |
| "Cháu lên mẫu gáio\"\"\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 53, | |
| "id": "473989a3", | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Cháu lên ba\n", | |
| "Cháu lên mẫu gáio\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(baitho)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 57, | |
| "id": "6bcc6cf1", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "s = \"Python dsasfads fasdf asdfasd fasd fsad ad\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 58, | |
| "id": "eb13a5be", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "42" | |
| ] | |
| }, | |
| "execution_count": 58, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "len(s)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "3ca84bdd", | |
| "metadata": {}, | |
| "source": [ | |
| "## list (ngôn ngữ khác còn gọi là array)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 59, | |
| "id": "4ee102e7", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "xs = [2,1,3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 60, | |
| "id": "fb6a684e", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "list" | |
| ] | |
| }, | |
| "execution_count": 60, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "type(xs)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 62, | |
| "id": "4dc37af3", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "xs = [2,1,3,123,12,3,123,12,312,3,12,1,\n", | |
| " 123123,12,312,31,23,12,3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "0e8dffff", | |
| "metadata": {}, | |
| "source": [ | |
| "### length - số phần tử trong list" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 63, | |
| "id": "9994d8d3", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "19" | |
| ] | |
| }, | |
| "execution_count": 63, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "len(xs)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 64, | |
| "id": "08b4368d", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "empty_list = []" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 65, | |
| "id": "633888e9", | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "0" | |
| ] | |
| }, | |
| "execution_count": 65, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "len(empty_list)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "ff1052a2", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 66, | |
| "id": "143fab46", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "xs = [2,31,1,3,5]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 67, | |
| "id": "178a849e", | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "42" | |
| ] | |
| }, | |
| "execution_count": 67, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sum(xs)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 68, | |
| "id": "7c9c7e80", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "ns = [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]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 69, | |
| "id": "3b75c7f5", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "874" | |
| ] | |
| }, | |
| "execution_count": 69, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sum(ns)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 71, | |
| "id": "e7e237b1", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "[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(list(range(27,50)))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 72, | |
| "id": "bf0d4859", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "874" | |
| ] | |
| }, | |
| "execution_count": 72, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sum(list(range(27,50)))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 73, | |
| "id": "d53ea697", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "12007099" | |
| ] | |
| }, | |
| "execution_count": 73, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sum(list(range(27,4901)))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 75, | |
| "id": "7a6ed156", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "xs = [1000,3000,2000,5000,9000]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "abd03106", | |
| "metadata": {}, | |
| "source": [ | |
| "## indexing" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "d64df9cf", | |
| "metadata": {}, | |
| "source": [ | |
| "### index bắt đầu từ 0" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 78, | |
| "id": "a192feec", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "1000" | |
| ] | |
| }, | |
| "execution_count": 78, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[0] # 0 - index" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 79, | |
| "id": "9a16e596", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "3000" | |
| ] | |
| }, | |
| "execution_count": 79, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[1] # index 1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 80, | |
| "id": "0e2a7b1c", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2000" | |
| ] | |
| }, | |
| "execution_count": 80, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[2]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 81, | |
| "id": "6360cffd", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "5000" | |
| ] | |
| }, | |
| "execution_count": 81, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 82, | |
| "id": "4cc124fc", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "9000" | |
| ] | |
| }, | |
| "execution_count": 82, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[4]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 83, | |
| "id": "4600fad3", | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "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)", | |
| "Cell \u001b[0;32mIn[83], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mxs\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m]\u001b[49m\n", | |
| "\u001b[0;31mIndexError\u001b[0m: list index out of range" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "xs[5]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "007e001b", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 85, | |
| "id": "7725b9fe", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "list" | |
| ] | |
| }, | |
| "execution_count": 85, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "type(xs)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 87, | |
| "id": "fb965bbb", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "9000" | |
| ] | |
| }, | |
| "execution_count": 87, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[len(xs)-1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 90, | |
| "id": "e6441da7", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "9000" | |
| ] | |
| }, | |
| "execution_count": 90, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[-1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 88, | |
| "id": "cf5ca742", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "5000" | |
| ] | |
| }, | |
| "execution_count": 88, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[len(xs)-2]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 89, | |
| "id": "d37c9a47", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2000" | |
| ] | |
| }, | |
| "execution_count": 89, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[len(xs)-3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 91, | |
| "id": "f74dee04", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "9000" | |
| ] | |
| }, | |
| "execution_count": 91, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[-1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 92, | |
| "id": "ee52686a", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "5000" | |
| ] | |
| }, | |
| "execution_count": 92, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[-2]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "fba8007c", | |
| "metadata": {}, | |
| "source": [ | |
| "## negative index - index âm" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 94, | |
| "id": "29f5eacc", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 3000, 2000]" | |
| ] | |
| }, | |
| "execution_count": 94, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "[xs[0], xs[1], xs[2]]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "9c11992f", | |
| "metadata": {}, | |
| "source": [ | |
| "## slicing - trả về 1 list con " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 98, | |
| "id": "eebd4b1b", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 3000, 2000, 5000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 98, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 95, | |
| "id": "f8c18f3b", | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 3000, 2000]" | |
| ] | |
| }, | |
| "execution_count": 95, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[0:3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "a713553d", | |
| "metadata": {}, | |
| "source": [ | |
| "### lấy 3 phần tử đầu" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 99, | |
| "id": "82ea0c4b", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 3000, 2000]" | |
| ] | |
| }, | |
| "execution_count": 99, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[:3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "3b4b5115", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "71be0a9e", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 96, | |
| "id": "f4b6e12b", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[3000, 2000]" | |
| ] | |
| }, | |
| "execution_count": 96, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[1:3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 97, | |
| "id": "ac3c8cb7", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[3000, 2000, 5000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 97, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[1:5]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 100, | |
| "id": "32709439", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 3000, 2000, 5000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 100, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 103, | |
| "id": "154923bf", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[2000, 5000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 103, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[2:len(xs)]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 104, | |
| "id": "f64a4e72", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[2000, 5000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 104, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[2:]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "0d161a46", | |
| "metadata": {}, | |
| "source": [ | |
| "### lấy 3 phần tử cuối" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 105, | |
| "id": "fcc36307", | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[2000, 5000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 105, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[-3:]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "e230dfc8", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 106, | |
| "id": "8682a084", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 3000, 2000, 5000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 106, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "4943e4b9", | |
| "metadata": {}, | |
| "source": [ | |
| "## slice[start:stop:step]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 107, | |
| "id": "272e615c", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 2000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 107, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[0:len(xs):2]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 108, | |
| "id": "3a79c88e", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 2000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 108, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[::2]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "eb77fa7e", | |
| "metadata": {}, | |
| "source": [ | |
| "### dùng range, sum" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "971a8d9c", | |
| "metadata": {}, | |
| "source": [ | |
| "https://projecteuler.net/problem=1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "e40db581", | |
| "metadata": {}, | |
| "source": [ | |
| "Tổng các số nhỏ hơn 1000 chia hết 3 hoặc 5" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 111, | |
| "id": "8ef7b2dd", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[0, 3, 6, 9, 12, 15, 18]" | |
| ] | |
| }, | |
| "execution_count": 111, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list(range(20))[::3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 113, | |
| "id": "a06b7c11", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[0, 5, 10, 15]" | |
| ] | |
| }, | |
| "execution_count": 113, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list(range(20))[::5]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 116, | |
| "id": "a4412d51", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "233168" | |
| ] | |
| }, | |
| "execution_count": 116, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "(sum(list(range(1000))[::3])\n", | |
| "+sum(list(range(1000))[::5])\n", | |
| "-sum(list(range(1000))[::15])\n", | |
| ")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "1ba4cc10", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 117, | |
| "id": "dff9045b", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 3000, 2000, 5000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 117, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 118, | |
| "id": "f0292822", | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[9000, 5000, 2000, 3000, 1000]" | |
| ] | |
| }, | |
| "execution_count": 118, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs[::-1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "f77de58f", | |
| "metadata": {}, | |
| "source": [ | |
| "### Học 1 biết 2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 119, | |
| "id": "d47dfe6f", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "s = \"Python\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 120, | |
| "id": "5cd50c8d", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'P'" | |
| ] | |
| }, | |
| "execution_count": 120, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s[0]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 121, | |
| "id": "a05bad83", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'n'" | |
| ] | |
| }, | |
| "execution_count": 121, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s[-1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 122, | |
| "id": "c22ea817", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Pyt'" | |
| ] | |
| }, | |
| "execution_count": 122, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s[:3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 123, | |
| "id": "cf673c32", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'nohtyP'" | |
| ] | |
| }, | |
| "execution_count": 123, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s[::-1]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "9ddbd86d", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 125, | |
| "id": "2d290698", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'PythonPython'" | |
| ] | |
| }, | |
| "execution_count": 125, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s + s" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 126, | |
| "id": "7a7b064a", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'PythonPythonPython'" | |
| ] | |
| }, | |
| "execution_count": 126, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s + s + s" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 127, | |
| "id": "0e9f5857", | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'PythonPythonPython'" | |
| ] | |
| }, | |
| "execution_count": 127, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s * 3" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "80b5599f", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 135, | |
| "id": "151724bb", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "TypeError", | |
| "evalue": "can't multiply sequence by non-int of type 'str'", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
| "Cell \u001b[0;32mIn[135], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ms\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43ms\u001b[49m\n", | |
| "\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'str'" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "s*s" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "533cdb0c", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 132, | |
| "id": "24e0aa4f", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "[1000, 3000, 2000, 5000, 9000, 1000, 3000, 2000, 5000, 9000, 1000, 3000, 2000, 5000, 9000]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(xs * 3)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 133, | |
| "id": "fe4e7f6a", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print([0]*10)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "f97ddcff", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 137, | |
| "id": "0d41509c", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'n'" | |
| ] | |
| }, | |
| "execution_count": 137, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s[5]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 138, | |
| "id": "35a0a380", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "IndexError", | |
| "evalue": "string index out of range", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", | |
| "Cell \u001b[0;32mIn[138], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ms\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m7\u001b[39;49m\u001b[43m]\u001b[49m\n", | |
| "\u001b[0;31mIndexError\u001b[0m: string index out of range" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "s[7]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 140, | |
| "id": "67cb170b", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'th'" | |
| ] | |
| }, | |
| "execution_count": 140, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s[2:4]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 141, | |
| "id": "a82d5150", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'thon'" | |
| ] | |
| }, | |
| "execution_count": 141, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s[2:9]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 142, | |
| "id": "17424809", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "''" | |
| ] | |
| }, | |
| "execution_count": 142, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s[9:20]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 143, | |
| "id": "fa14d93b", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 3000, 2000, 5000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 143, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 145, | |
| "id": "ba40a928", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "xs[2]=0" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 146, | |
| "id": "2939a708", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1000, 3000, 0, 5000, 9000]" | |
| ] | |
| }, | |
| "execution_count": 146, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "xs" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 147, | |
| "id": "8789815f", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Python'" | |
| ] | |
| }, | |
| "execution_count": 147, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 148, | |
| "id": "35ad98ab", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "TypeError", | |
| "evalue": "'str' object does not support item assignment", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
| "Cell \u001b[0;32mIn[148], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ms\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mJ\u001b[39m\u001b[38;5;124m'\u001b[39m\n", | |
| "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "s[0] = 'J'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 149, | |
| "id": "744b4412", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Python'" | |
| ] | |
| }, | |
| "execution_count": 149, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "13ae0ca0", | |
| "metadata": {}, | |
| "source": [ | |
| "### un + change + able" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "784ba95a", | |
| "metadata": {}, | |
| "source": [ | |
| "## im + mutable" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "aed82b3b", | |
| "metadata": {}, | |
| "source": [ | |
| "## str is immutable\n", | |
| "## list is mutable" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "406b326b", | |
| "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.10" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment