{"id":25182134,"url":"https://github.com/ductnn/python-tu","last_synced_at":"2026-05-16T22:32:12.672Z","repository":{"id":46328913,"uuid":"259333066","full_name":"ductnn/Python-tu","owner":"ductnn","description":"Start with Python ...","archived":false,"fork":false,"pushed_at":"2021-12-17T20:26:29.000Z","size":138,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-09T17:35:56.853Z","etag":null,"topics":["algorithms","cli","django","python","tools"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ductnn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-27T13:37:13.000Z","updated_at":"2021-12-17T20:26:32.000Z","dependencies_parsed_at":"2022-08-30T03:41:29.815Z","dependency_job_id":null,"html_url":"https://github.com/ductnn/Python-tu","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ductnn%2FPython-tu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ductnn%2FPython-tu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ductnn%2FPython-tu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ductnn%2FPython-tu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ductnn","download_url":"https://codeload.github.com/ductnn/Python-tu/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247130211,"owners_count":20888544,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms","cli","django","python","tools"],"created_at":"2025-02-09T17:29:54.910Z","updated_at":"2026-05-16T22:32:07.641Z","avatar_url":"https://github.com/ductnn.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# My Python\n\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/ductnn/Python-tu/pulls)\n[![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\n```sh\n    ____        __  __                      __\n   / __ \\__  __/ /_/ /_  ____  ____        / /___  __\n  / /_/ / / / / __/ __ \\/ __ \\/ __ \\______/ __/ / / /\n / ____/ /_/ / /_/ / / / /_/ / / / /_____/ /_/ /_/ /\n/_/    \\__, /\\__/_/ /_/\\____/_/ /_/      \\__/\\__,_/\n      /____/\n```\n\n## Control Flow\n1. if Statements\n    * Cấu trúc điều kiện if...else \n    * Tùy chọn if...elif...elif... thay thế  switch...case trong các ngôn ngữ khác \n2. for Statements\n    * Vòng lặp for lặp qua cái item của sequence(list or string)\n3. The range() Function\n    * Sử dụng range(n) vòng lặp chạy từ 0 đến n-1\n4. break and continue Statements, and else Clauses on Loops\n    * **break**: Dừng chương trình\n    * **continue**: Dùng để tiếp tục vòng lặp tiếp \n5. pass Statements\n    * Sử dụng để yêu câu phải có cú pháp. Câu lệnh này không làm gì cả\n6. Defining Functions\n    * Sử dụng **def** khai bao Function\n    * Tạo 1 Function tính giai thừa \n    ```python\n    def Factotial(n):\n        while (n\u003e0):\n            if (n == 1):\n                return 1\n            else: return n*Factotial(n-1)\n    ```\n## Data Structures\n1. More on Lists \n    * list.append(x): Thêm phần tử  vào cuối List tương đương `a[len(a):] = [x]` \n    * list.extend(iterable): Extend the list by appending all the items from the iterable. Equivalent to `a[len(a):] = iterable`\n    * list.insert(i, x): Chèn item x vào vị trí i. `a.insert(len(a), x)` is equivalent to `a.append(x)`\n    * list.remove(x): Remove item có giá trị bằng x. Trả về  `ValueError` nếu không tìm thấy item đó\n    * list.pop([i]): Remove item ở vị trí i và trả về  giá trị của nó. `a.pop()` remove và trả về giá trị của item cuối cùng của list\n    * list.clear(): Remove toàn bộ item trong list\n    * list.index(x[, start[, end]]): Trả về  vị trí của item trong lists. Trả về  `ValueError` nếu không tìm thấy item đó.\n    ```bash\n    \u003e\u003e\u003e fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']\n    \u003e\u003e\u003e fruits.index('banana')\n    4\n    \u003e\u003e\u003e fruits.index('banana', 4)\n    6\n    ```\n    * list.count(x): Trả về  số  lần xuất hiện của item x trong list, nếu không có item x trong x trả về 0\n    * list.sort(key=None, reverse=False): Sắp xếp các item trong list\n    * list.reverse(): Đảo ngược lại list\n    * list.copy(): Return a shallow copy of the list  Equivalent to `a[:]`\n2. Using Lists as Stacks\n    * LIFO \n    ```bash\n    \u003e\u003e\u003e stack = [3, 4, 5]\n    \u003e\u003e\u003e stack.append(6)\n    \u003e\u003e\u003e stack\n    [3, 4, 5, 6]\n    \u003e\u003e\u003e stack.pop()\n    6\n    \u003e\u003e\u003e stack\n    [3, 4, 5]\n    ```\n3. Using Lists as Queues\n    * FIFO. **However, lists are not efficient for this purpose**\n    * Để  thực hiện như 1 hàng đợi, sử dụng `collections.deque`\n    ```bash\n    \u003e\u003e\u003e from collections import deque\n    \u003e\u003e\u003e queue = deque([\"Eric\", \"John\", \"Michael\"])\n    \u003e\u003e\u003e queue.append(\"Terry\") \n    \u003e\u003e\u003e queue\n    deque(['Eric', 'John', 'Michael', 'Terry'])\n    \u003e\u003e\u003e queue.popleft()\n    'Eric'\n    \u003e\u003e\u003e queue\n    deque(['John', 'Michael', 'Terry'])\n    ```\n4. List Comprehensions\n    * List Comprehensions cung cấp các cách ngắn gọn để  tạo ra List\n    * Tạo 1 list các số  chính phương\n    ```bash\n    \u003e\u003e\u003e squares = []\n    \u003e\u003e\u003e for x in range(10):\n    ...     squares.append(x**2)\n    ...\n    \u003e\u003e\u003e squares\n    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n    ```\n    * Có thể  tạo list theo cách sau\n    ```bash\n    squares = list(map(lambda x: x**2, range(10)))\n    ```\n    or\n    ```\n    squares = [x**2 for x in range(10)]\n    ```\n5. Nested List Comprehensions\n6. The del statement\n    * Remove item trong list, khác với pop() trả về  item bị xóa.\n    ```bash\n    \u003e\u003e\u003e a = [-1, 1, 66.25, 333, 333, 1234.5]\n    \u003e\u003e\u003e del a[0]\n    \u003e\u003e\u003e a\n    [1, 66.25, 333, 333, 1234.5]\n    \u003e\u003e\u003e del a[2:4]\n    \u003e\u003e\u003e a\n    [1, 66.25, 1234.5]\n    \u003e\u003e\u003e del a[:]\n    \u003e\u003e\u003e a\n    []\n    ```\n7. Tuples and Sequences\n    * Tuples are **immutable**, Lists are **mutable**\n8. Sets\n    * Set là tập các element không trùng nhau, không có thứ tự\n    ```bash\n    \u003e\u003e\u003e S = {'a', 'b', 'c', 'd', 'e', 'a'}\n    \u003e\u003e\u003e S\n    {'a', 'b', 'c', 'd', 'e'}\n    \u003e\u003e\u003e set(S)\n    {'e', 'a', 'b', 'd', 'c'}\n    ```\n9. Dictionaries\n    * Dictionaries đánh dấu bằng *key*, là cặp `key: value`\n    ```bash\n    \u003e\u003e\u003e animals = {'cat': 'meow', 'chipu': 'ooo', 'mouse': 999}\n    {'cat': 'meow', 'chipu': 'ooo', 'mouse': 999}\n    \u003e\u003e\u003e animals['bird'] = 0\n    \u003e\u003e\u003e animals\n    {'cat': 'meow', 'chipu': 'ooo', 'mouse': 999, 'bird': 0}\n    ```\n    * `dict()` xây dựng dictionaries từ các cặp *key-value*\n    ```bash\n    \u003e\u003e\u003e dict([('J', 11), ('Q', 12), ('K', 13), ('A', 1)])\n    {'J': 11, 'Q': 12, 'K': 13, 'A': 1}\n    ```\n## Modules and Packages\n1. Modules\n    * Module: Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. \n    * Các definitions từ 1 module có thể  được *import* vào module khác hoặc module chính. \n    * Tạo file **fibo.py**\n    ```python\n    def fib(n):    # write Fibonacci series up to n\n        a, b = 0, 1\n        while a \u003c n:\n            print(a, end=' ')\n            a, b = b, a+b\n        print()\n\n    def fib2(n):   # return Fibonacci series up to n\n        result = []\n        a, b = 0, 1\n        while a \u003c n:\n            result.append(a)\n            a, b = b, a+b\n        return result\n    ```\n    * `import fibo` trong interpreter\n    ```bash\n    \u003e\u003e\u003e import fibo\n    ```\n    ```bash\n    \u003e\u003e\u003e fibo.fib(1000)\n    0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987\n    \u003e\u003e\u003e fibo.fib2(100)\n    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n    \u003e\u003e\u003e fibo.__name__\n    'fibo'\n    ```\n    * Trong trường hợp nếu không muốn sử dụng toàn bộ module mà chỉ sử dụng 1 phần, sử dụng `from...import`\n    ```bash\n    \u003e\u003e\u003e from fibo import fib\n    ```\n2. Packages \n    * Package trong Python là một thư mục chứa một hoặc nhiều modules hay các package khác nhau, nó được tạo ra  nhằm mục đích phân bố các modules có cùng chức năng hay một cái gì đó, để dễ quản lý source code\n    * Tạo 1 package chỉ cần tạo 1 thư mục, tên thư mục là tên package và phải có file `__init__.py`, file này sẽ được gọi ra đầu tiên khi import package\n## Errors and Exceptions\n1. Errors\n2. Exceptions \n    * Xử  lý lỗi ngoại lệ, là Error được phát hiện khi thực hiện chương trình.\n    * Tạo chương trình tính thương 2 số\n    ```python\n    def test(a, b):\n        return a / b\n    print(test(6, 0))\n    ```\n    Kết quả\n    ```bash\n    Traceback (most recent call last):\n        File \"test.py\", line 4, in \u003cmodule\u003e\n            print(test(6, 0))\n        File \"test.py\", line 2, in test\n            return a / b\n    ZeroDivisionError: division by zero\n    ```\n3. Handling Exceptions\n    * Sử dụng `try...except`\n    * Nếu khối lệnh trong `try` có 1 lỗi gì đó xảy ra thì chương trình sẽ tìm các except bên dưới, nếu có except thỏa mãn nó sẽ thực thi code trong khối except đó \n    ```python\n    def test(a, b):\n        return a / b\n\n    try :\n        print(test(6, 0))\n    except ZeroDivisionError:\n        print('Bug Bug !!!')\n    ```\n    Kết quả\n    ```bash\n    Bug Bug !!!\n    ```\n4. Raising Exceptions\n    * Cho phép chỉ định *exception* khi xảy ra lỗi\n    ```bash\n    \u003e\u003e\u003e raise ValueError('Hello World !!!')\n    Traceback (most recent call last):\n        File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\n    ValueError: Hello World !!!\n    ```\n## Classes\n+ Khai báo class\n```python\nclass Person:\n    pass:\n``` \n+ Khai báo các thuộc tính (name, age, sex) và các phương thức (getName(), setAge(), getAge(), setMale(), getMAle()) trong class\n```python\nclass Person:\n    # thuộc tính\n    name = \"DUCTN\";\n    age = 22;\n    male = True\n    # phương thức\n    def setName(self, name):\n        self.name = name\n    \n    def getName(self):\n        return self.name\n    \n    def setAge(self, age):\n        self.age = age\n    \n    def getAge(self):\n        return self.age\n    \n    def setMale(self, male):\n        self.male = male\n    \n    def getMale(self):\n        return self.male\n```\n+ Khởi tạo class sau khi khái báo\n```python\nperson = Person()\n```\n## Virtual Environments\n1. Intro\n    * Khi thực hiện 1 project, việc tạo ra 1 môi trường ảo giúp project thực hiện được độc lập với các project khác, phù hợp theo yêu cầu, đúng phiên bản với các modules hay packages sử dụng mà không nằm trong thư viện chuẩn của python. \n    * Tránh bị conflic giữa các app khi đang code.\n    * Các app khác nhau có thể sử dụng trong các môi trường khác nhau\n2. Creating Virtual Environments\n    * Tạo môi trường ảo tên `ductn`\n    ```bash\n    $ python3 -m venv ductn\n    ```\n    * Kích hoạt môi trường\n    ```bash\n    $ source ductn/bin/activate\n    ```\n    * Cài đặt các gói package\n    Tải ver mới nhất\n    ```bash\n    (ductn) $ pip install requests\n    ```\n    Tải ver cụ thể\n    ```bash\n    (ductn) $ pip install requests==2.6\n    ```\n    Update ver \n    ```bash\n    (ductn) $ pip install --upgrade requests\n    ```\n    * Khi có thông tin về  tên và ver của các packages trong requirements.txt, chỉ cần thực thi lệnh sau để  cài đặt.\n    ```bash\n    $ pip install -r requirements.txt\n    ```\n    * Đưa lists các packages và 1 file, sử dụng\n    ```\n    $ pip freeze \u003e requirements.txt\n    ```\n1. ML Basic\n2. Algorithm\n3. Syntax basic\n4. Syntax basic_2\n5. Network automation\n\n## Show your support\nGive a ⭐ if you like this repo ❤️\n\n## Contribution\nAll contributions are welcomed in this project.\n\n## License\nThe MIT License (MIT). Please see [LICENSE](LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fductnn%2Fpython-tu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fductnn%2Fpython-tu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fductnn%2Fpython-tu/lists"}