{"id":424,"date":"2025-03-21T03:38:29","date_gmt":"2025-03-21T03:38:29","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=424"},"modified":"2025-03-21T03:38:29","modified_gmt":"2025-03-21T03:38:29","slug":"%f0%9f%9a%80-unpacking-trong-python-giai-nen-du-lieu-de-dang-hieu-qua-va-linh-hoat-%f0%9f%90%8d","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=424","title":{"rendered":"\ud83d\ude80 UNPACKING TRONG PYTHON \u2013 GI\u1ea2I N\u00c9N D\u1eee LI\u1ec6U D\u1ec4 D\u00c0NG, HI\u1ec6U QU\u1ea2 V\u00c0 LINH HO\u1ea0T \ud83d\udc0d"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"691\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/IWQeLWrUL-1024x691.png\" alt=\"\" class=\"wp-image-425\" srcset=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/IWQeLWrUL-1024x691.png 1024w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/IWQeLWrUL-300x202.png 300w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/IWQeLWrUL-768x518.png 768w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/IWQeLWrUL-850x574.png 850w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/IWQeLWrUL.png 1230w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>Trong l\u1eadp tr\u00ecnh Python, <strong>Unpacking<\/strong> l\u00e0 m\u1ed9t k\u1ef9 thu\u1eadt quan tr\u1ecdng gi\u00fap b\u1ea1n <strong>t\u00e1ch d\u1eef li\u1ec7u t\u1eeb c\u00e1c c\u1ea5u tr\u00fac nh\u01b0 tuple, list, dictionary, object,&#8230;<\/strong> v\u00e0 g\u00e1n ch\u00fang v\u00e0o c\u00e1c bi\u1ebfn m\u1ed9t c\u00e1ch <strong>nhanh ch\u00f3ng v\u00e0 tr\u1ef1c quan<\/strong>.<\/p>\n\n\n\n<p>Unpacking gi\u00fap b\u1ea1n <strong>vi\u1ebft code g\u1ecdn h\u01a1n, d\u1ec5 \u0111\u1ecdc h\u01a1n v\u00e0 x\u1eed l\u00fd d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3 h\u01a1n<\/strong>. Trong b\u00e0i vi\u1ebft n\u00e0y, ch\u00fang ta s\u1ebd kh\u00e1m ph\u00e1 <strong>m\u1ecdi kh\u00eda c\u1ea1nh c\u1ee7a Unpacking<\/strong> t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao, k\u00e8m theo nh\u1eefng <strong>v\u00ed d\u1ee5 th\u1ef1c t\u1ebf<\/strong> v\u00e0 <strong>\u1ee9ng d\u1ee5ng th\u00fa v\u1ecb<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 <strong>1. Unpacking l\u00e0 g\u00ec?<\/strong><\/h1>\n\n\n\n<p><strong>Unpacking<\/strong> (gi\u1ea3i n\u00e9n d\u1eef li\u1ec7u) l\u00e0 qu\u00e1 tr\u00ecnh <strong>t\u00e1ch t\u1eebng ph\u1ea7n t\u1eed t\u1eeb m\u1ed9t c\u1ea5u tr\u00fac d\u1eef li\u1ec7u v\u00e0 g\u00e1n ch\u00fang v\u00e0o c\u00e1c bi\u1ebfn ri\u00eang bi\u1ec7t<\/strong>.<\/p>\n\n\n\n<p>\ud83d\udca1 <strong>L\u1ee3i \u00edch c\u1ee7a Unpacking:<\/strong><br>\u2705 <strong>D\u1ec5 d\u00e0ng l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u c\u00f3 c\u1ea5u tr\u00fac ph\u1ee9c t\u1ea1p.<\/strong><br>\u2705 <strong>Vi\u1ebft code ng\u1eafn g\u1ecdn, d\u1ec5 b\u1ea3o tr\u00ec.<\/strong><br>\u2705 <strong>T\u0103ng hi\u1ec7u su\u1ea5t khi thao t\u00e1c v\u1edbi danh s\u00e1ch, tuple, dictionary, function arguments,&#8230;<\/strong><br>\u2705 <strong>\u1ee8ng d\u1ee5ng m\u1ea1nh m\u1ebd trong x\u1eed l\u00fd d\u1eef li\u1ec7u, AI, Machine Learning v\u00e0 Web API.<\/strong><\/p>\n\n\n\n<p>\ud83d\udccc <strong>Unpacking c\u00f3 th\u1ec3 \u00e1p d\u1ee5ng v\u1edbi:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Tuple<\/strong> v\u00e0 <strong>List<\/strong><\/li>\n\n\n\n<li><strong>Dictionary<\/strong><\/li>\n\n\n\n<li><strong>H\u00e0m v\u00e0 \u0111\u1ed1i s\u1ed1 (<code>*args<\/code>, <code>**kwargs<\/code>)<\/strong><\/li>\n\n\n\n<li><strong>Chu\u1ed7i (String)<\/strong><\/li>\n\n\n\n<li><strong>Object trong l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng (OOP)<\/strong><\/li>\n<\/ul>\n\n\n\n<p>H\u00e3y c\u00f9ng t\u00ecm hi\u1ec3u t\u1eebng tr\u01b0\u1eddng h\u1ee3p c\u1ee5 th\u1ec3! \ud83d\ude80<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf <strong>2. Unpacking v\u1edbi Tuple v\u00e0 List<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc 2.1. Gi\u1ea3i n\u00e9n to\u00e0n b\u1ed9 Tuple ho\u1eb7c List<\/strong><\/h3>\n\n\n\n<p>Trong Python, b\u1ea1n c\u00f3 th\u1ec3 <strong>gi\u1ea3i n\u00e9n gi\u00e1 tr\u1ecb trong tuple ho\u1eb7c list<\/strong> b\u1eb1ng c\u00e1ch g\u00e1n ch\u00fang v\u00e0o c\u00e1c bi\u1ebfn.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udee0 <strong>V\u00ed d\u1ee5 1: Gi\u1ea3i n\u00e9n Tuple<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>person = (\"Alice\", 25, \"Engineer\")\n\nname, age, job = person  \n\nprint(name)  # Alice\nprint(age)   # 25\nprint(job)   # Engineer\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udee0 <strong>V\u00ed d\u1ee5 2: Gi\u1ea3i n\u00e9n List<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#091;10, 20, 30]\n\na, b, c = numbers\n\nprint(a)  # 10\nprint(b)  # 20\nprint(c)  # 30\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc 2.2. Gi\u1ea3i n\u00e9n v\u1edbi <code>*<\/code> (Star Operator)<\/strong><\/h3>\n\n\n\n<p><code>*<\/code> gi\u00fap b\u1ea1n <strong>gi\u1ea3i n\u00e9n m\u1ed9t ph\u1ea7n d\u1eef li\u1ec7u<\/strong> thay v\u00ec to\u00e0n b\u1ed9.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udee0 <strong>V\u00ed d\u1ee5 3: L\u1ea5y ph\u1ea7n \u0111\u1ea7u v\u00e0 ph\u1ea7n c\u00f2n l\u1ea1i<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#091;1, 2, 3, 4, 5]\n\nfirst, *rest = numbers\n\nprint(first)  # 1\nprint(rest)   # &#091;2, 3, 4, 5]\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udee0 <strong>V\u00ed d\u1ee5 4: L\u1ea5y ph\u1ea7n t\u1eed \u0111\u1ea7u, gi\u1eefa v\u00e0 cu\u1ed1i<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>data = &#091;100, 200, 300, 400, 500]\n\nfirst, *middle, last = data\n\nprint(first)   # 100\nprint(middle)  # &#091;200, 300, 400]\nprint(last)    # 500\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u1ee8ng d\u1ee5ng:<\/strong> H\u1eefu \u00edch khi b\u1ea1n c\u1ea7n l\u1ea5y d\u1eef li\u1ec7u t\u1eeb danh s\u00e1ch c\u00f3 \u0111\u1ed9 d\u00e0i kh\u00f4ng c\u1ed1 \u0111\u1ecbnh.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf <strong>3. Unpacking v\u1edbi Dictionary<\/strong><\/h1>\n\n\n\n<p>Python cho ph\u00e9p unpacking <strong>key ho\u1eb7c value c\u1ee7a dictionary<\/strong> m\u1ed9t c\u00e1ch linh ho\u1ea1t.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc 3.1. Gi\u1ea3i n\u00e9n key v\u00e0 value<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udee0 <strong>V\u00ed d\u1ee5 5: L\u1ea5y danh s\u00e1ch keys v\u00e0 values<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>student = {\"name\": \"Bob\", \"age\": 22, \"major\": \"Math\"}\n\n# Unpacking keys\nkeys = student.keys()\nprint(*keys)  # name age major\n\n# Unpacking values\nvalues = student.values()\nprint(*values)  # Bob 22 Math\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc 3.2. Unpacking Dictionary v\u00e0o Function (<code>**<\/code>)<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udee0 <strong>V\u00ed d\u1ee5 6: Truy\u1ec1n dictionary v\u00e0o function<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def introduce(name, age, major):\n    print(f\"My name is {name}, I'm {age} years old, and I study {major}.\")\n\nstudent_info = {\"name\": \"Bob\", \"age\": 22, \"major\": \"Math\"}\n\nintroduce(**student_info)\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u1ee8ng d\u1ee5ng:<\/strong> D\u00f9ng khi l\u00e0m vi\u1ec7c v\u1edbi JSON API ho\u1eb7c d\u1eef li\u1ec7u t\u1eeb database.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf <strong>4. Unpacking v\u1edbi Function Arguments (<code>*args<\/code> v\u00e0 <code>**kwargs<\/code>)<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc 4.1. S\u1eed d\u1ee5ng <code>*args<\/code> \u0111\u1ec3 truy\u1ec1n nhi\u1ec1u tham s\u1ed1<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udee0 <strong>V\u00ed d\u1ee5 7: Truy\u1ec1n list\/tuple v\u00e0o h\u00e0m<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def sum_numbers(*args):\n    return sum(args)\n\nprint(sum_numbers(1, 2, 3, 4, 5))  # 15\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc 4.2. S\u1eed d\u1ee5ng <code>**kwargs<\/code> \u0111\u1ec3 truy\u1ec1n dictionary<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udee0 <strong>V\u00ed d\u1ee5 8: Truy\u1ec1n dictionary v\u00e0o h\u00e0m<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def display_info(**kwargs):\n    for key, value in kwargs.items():\n        print(f\"{key}: {value}\")\n\ndisplay_info(name=\"Alice\", age=25, job=\"Engineer\")\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u1ee8ng d\u1ee5ng:<\/strong> D\u00f9ng trong Web API, Flask, Django, Machine Learning.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf <strong>5. \u1ee8ng d\u1ee5ng th\u1ef1c t\u1ebf c\u1ee7a Unpacking<\/strong><\/h1>\n\n\n\n<p>\u2705 <strong>Ho\u00e1n \u0111\u1ed5i gi\u00e1 tr\u1ecb gi\u1eefa hai bi\u1ebfn nhanh ch\u00f3ng<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a, b = 5, 10\na, b = b, a\nprint(a, b)  # 10 5\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>L\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u JSON, API<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = {\"id\": 1, \"name\": \"Alice\", \"role\": \"admin\"}\nid, name, role = data.values()\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>T\u1ed1i \u01b0u h\u00f3a v\u00f2ng l\u1eb7p<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>students = &#091;(\"Alice\", 20), (\"Bob\", 22), (\"Charlie\", 21)]\nfor name, age in students:\n    print(f\"{name} is {age} years old.\")\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>T\u1ed1i \u01b0u thu\u1eadt to\u00e1n<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def get_min_max(numbers):\n    return min(numbers), max(numbers)\n\nmin_val, max_val = get_min_max(&#091;10, 2, 8, 6])\nprint(min_val, max_val)  # 2 10\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf <strong>6. T\u1ed5ng k\u1ebft<\/strong><\/h1>\n\n\n\n<p>\ud83d\udd39 <strong>Unpacking gi\u00fap t\u1ed1i \u01b0u h\u00f3a code Python, l\u00e0m cho n\u00f3 g\u1ecdn g\u00e0ng h\u01a1n v\u00e0 d\u1ec5 hi\u1ec3u h\u01a1n.<\/strong><br>\ud83d\udd39 <strong>\u1ee8ng d\u1ee5ng trong nhi\u1ec1u l\u0129nh v\u1ef1c nh\u01b0 x\u1eed l\u00fd d\u1eef li\u1ec7u, AI, Machine Learning, Web API.<\/strong><br>\ud83d\udd39 <strong>K\u1ebft h\u1ee3p v\u1edbi <code>*<\/code>, <code>**<\/code>, <code>*args<\/code>, <code>**kwargs<\/code> gi\u00fap t\u0103ng c\u01b0\u1eddng t\u00ednh linh ho\u1ea1t trong l\u1eadp tr\u00ecnh.<\/strong><\/p>\n\n\n\n<p>\ud83d\udd25 <strong>B\u1ea1n \u0111\u00e3 th\u1eed Unpacking trong Python ch\u01b0a? H\u00e3y \u00e1p d\u1ee5ng ngay \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a code c\u1ee7a b\u1ea1n!<\/strong> \ud83d\ude80<\/p>\n\n\n\n<p>#Python #Unpacking #LapTrinhPython #PythonTips #CodeOptimization<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trong l\u1eadp tr\u00ecnh Python, Unpacking l\u00e0 m\u1ed9t k\u1ef9 thu\u1eadt quan tr\u1ecdng gi\u00fap b\u1ea1n t\u00e1ch d\u1eef li\u1ec7u t\u1eeb c\u00e1c c\u1ea5u tr\u00fac nh\u01b0 tuple, list, dictionary, object,&#8230; v\u00e0 g\u00e1n ch\u00fang v\u00e0o c\u00e1c bi\u1ebfn m\u1ed9t&#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-424","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/424","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=424"}],"version-history":[{"count":1,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/424\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/424\/revisions\/426"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}