{"id":343,"date":"2025-03-10T14:09:50","date_gmt":"2025-03-10T14:09:50","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=343"},"modified":"2025-03-10T14:19:20","modified_gmt":"2025-03-10T14:19:20","slug":"10-meo-python-huu-ich-giup-cai-thien-ky-nang-lap-trinh","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=343","title":{"rendered":"\u2728 10 M\u1eb8O PYTHON H\u1eeeU \u00cdCH GI\u00daP C\u1ea2I THI\u1ec6N K\u1ef8 N\u0102NG L\u1eacP TR\u00ccNH \u2728"},"content":{"rendered":"\n<p>\ud83d\udc0d <strong>Python<\/strong> l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh m\u1ea1nh m\u1ebd, d\u1ec5 h\u1ecdc, nh\u01b0ng l\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 <strong>vi\u1ebft code ng\u1eafn g\u1ecdn, t\u1ed1i \u01b0u<\/strong> h\u01a1n? D\u01b0\u1edbi \u0111\u00e2y l\u00e0 <strong>10 m\u1eb9o si\u00eau h\u1eefu \u00edch<\/strong> gi\u00fap b\u1ea1n n\u00e2ng tr\u00ecnh l\u1eadp tr\u00ecnh Python ngay l\u1eadp t\u1ee9c! \ud83d\ude80<\/p>\n\n\n\n<p>\ud83d\udccc <strong>1. S\u1eed d\u1ee5ng List Comprehension<\/strong><br>\u2705 Vi\u1ebft code g\u1ecdn h\u01a1n, t\u1ea1o danh s\u00e1ch nhanh ch\u00f3ng:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>squares = &#091;x ** 2 for x in range(10)]\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>2. D\u00f9ng enumerate() thay v\u00ec range(len())<\/strong><br>\u2705 V\u1eeba l\u1ea5y ch\u1ec9 m\u1ee5c, v\u1eeba l\u1ea5y gi\u00e1 tr\u1ecb trong v\u00f2ng l\u1eb7p:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>names = &#091;\"Alice\", \"Bob\", \"Charlie\"]\nfor index, name in enumerate(names):\n    print(f\"{index}: {name}\")\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>3. S\u1eed d\u1ee5ng zip() \u0111\u1ec3 duy\u1ec7t nhi\u1ec1u danh s\u00e1ch c\u00f9ng l\u00fac<\/strong><br>\u2705 Duy\u1ec7t qua nhi\u1ec1u danh s\u00e1ch m\u1ed9t c\u00e1ch m\u01b0\u1ee3t m\u00e0:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>names = &#091;\"Alice\", \"Bob\", \"Charlie\"]\nages = &#091;25, 30, 35]\nfor name, age in zip(names, ages):\n    print(f\"{name} is {age} years old\")\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>4. S\u1eed d\u1ee5ng defaultdict \u0111\u1ec3 tr\u00e1nh KeyError<\/strong><br>\u2705 Kh\u00f4ng c\u1ea7n ki\u1ec3m tra key tr\u01b0\u1edbc khi th\u00eam gi\u00e1 tr\u1ecb:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import defaultdict\nd = defaultdict(int)\nd&#091;\"a\"] += 1\nprint(d&#091;\"a\"])  # Output: 1\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>5. D\u00f9ng f-string \u0111\u1ec3 \u0111\u1ecbnh d\u1ea1ng chu\u1ed7i<\/strong><br>\u2705 C\u00e1ch nhanh nh\u1ea5t \u0111\u1ec3 t\u1ea1o chu\u1ed7i \u0111\u1ed9ng:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Alice\"\nage = 25\nprint(f\"My name is {name} and I am {age} years old.\")\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>6. D\u00f9ng unpacking \u0111\u1ec3 ho\u00e1n \u0111\u1ed5i gi\u00e1 tr\u1ecb<\/strong><br>\u2705 Kh\u00f4ng c\u1ea7n bi\u1ebfn t\u1ea1m, ho\u00e1n \u0111\u1ed5i nhanh ch\u00f3ng:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a, b = 5, 10\na, b = b, a\nprint(a, b)  # Output: 10, 5\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>7. S\u1eed d\u1ee5ng any() v\u00e0 all() \u0111\u1ec3 ki\u1ec3m tra \u0111i\u1ec1u ki\u1ec7n trong danh s\u00e1ch<\/strong><br>\u2705 Ki\u1ec3m tra danh s\u00e1ch hi\u1ec7u qu\u1ea3 h\u01a1n:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nums = &#091;1, 2, 3, 4]\nprint(any(n &gt; 3 for n in nums))  # True\nprint(all(n &gt; 0 for n in nums))  # True\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>8. D\u00f9ng try-except thay v\u00ec ki\u1ec3m tra th\u1ee7 c\u00f4ng<\/strong><br>\u2705 Gi\u00fap code ch\u1ea1y m\u01b0\u1ee3t m\u00e0, tr\u00e1nh l\u1ed7i kh\u00f4ng mong mu\u1ed1n:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def safe_divide(a, b):\n    try:\n        return a \/ b\n    except ZeroDivisionError:\n        return \"Cannot divide by zero\"\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>9. S\u1eed d\u1ee5ng with khi l\u00e0m vi\u1ec7c v\u1edbi file<\/strong><br>\u2705 \u0110\u1ea3m b\u1ea3o file lu\u00f4n \u0111\u01b0\u1ee3c \u0111\u00f3ng sau khi s\u1eed d\u1ee5ng:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"example.txt\", \"r\") as file:\n    content = file.read()\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>10. D\u00f9ng map() \u0111\u1ec3 \u00e1p d\u1ee5ng h\u00e0m nhanh ch\u00f3ng<\/strong><br>\u2705 Duy\u1ec7t qua danh s\u00e1ch m\u00e0 kh\u00f4ng c\u1ea7n d\u00f9ng v\u00f2ng l\u1eb7p:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#091;1, 2, 3, 4]\nsquared = list(map(lambda x: x ** 2, numbers))\nprint(squared)  # Output: &#091;1, 4, 9, 16]\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udd25 <strong>B\u1ea1n \u0111\u00e3 \u00e1p d\u1ee5ng bao nhi\u00eau m\u1eb9o trong s\u1ed1 n\u00e0y? H\u00e3y th\u1eed ngay v\u00e0 chia s\u1ebb kinh nghi\u1ec7m c\u1ee7a b\u1ea1n nh\u00e9!<\/strong> \ud83d\udd25<\/p>\n\n\n\n<p>#Python #CodeTips #L\u1eadpTr\u00ecnhHi\u1ec7uQu\u1ea3 \ud83d\ude80\ud83d\udc0d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc0d Python l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh m\u1ea1nh m\u1ebd, d\u1ec5 h\u1ecdc, nh\u01b0ng l\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 vi\u1ebft code ng\u1eafn g\u1ecdn, t\u1ed1i \u01b0u h\u01a1n? D\u01b0\u1edbi \u0111\u00e2y l\u00e0 10 m\u1eb9o si\u00eau h\u1eefu \u00edch&#8230;<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-343","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=343"}],"version-history":[{"count":2,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions"}],"predecessor-version":[{"id":345,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions\/345"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}