{"id":1527,"date":"2025-09-28T20:17:13","date_gmt":"2025-09-28T13:17:13","guid":{"rendered":"https:\/\/kienthucmo.com\/?p=1527"},"modified":"2025-11-05T23:40:43","modified_gmt":"2025-11-05T16:40:43","slug":"them-phan-tu-vao-tuple-trong-python","status":"publish","type":"post","link":"https:\/\/kienthucmo.com\/vi\/them-phan-tu-vao-tuple-trong-python\/","title":{"rendered":"Th\u00eam ph\u1ea7n t\u1eed v\u00e0o Tuple trong Python"},"content":{"rendered":"\n<p>Trong Python, tuple l\u00e0 m\u1ed9t lo\u1ea1i c\u1ea5u tr\u00fac d\u1eef li\u1ec7u r\u1ea5t gi\u1ed1ng list nh\u01b0ng <strong>immutable<\/strong> (kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i tr\u1ef1c ti\u1ebfp). \u0110i\u1ec1u n\u00e0y ngh\u0129a l\u00e0 sau khi tuple \u0111\u01b0\u1ee3c t\u1ea1o ra, m\u00ecnh <strong>kh\u00f4ng th\u1ec3 th\u00eam, x\u00f3a ho\u1eb7c thay \u0111\u1ed5i ph\u1ea7n t\u1eed b\u00ean trong tuple \u0111\u00f3<\/strong>.<\/p>\n\n\n\n<p>V\u1eady l\u00e0m sao \u0111\u1ec3 <strong>th\u00eam ph\u1ea7n t\u1eed v\u00e0o tuple<\/strong>? \u0110\u00e2y l\u00e0 m\u1ed9t th\u1eafc m\u1eafc ph\u1ed5 bi\u1ebfn, nh\u1ea5t l\u00e0 khi m\u00ecnh l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u b\u1ea5t bi\u1ebfn trong Python.<\/p>\n\n\n\n<p>Trong b\u00e0i vi\u1ebft n\u00e0y, m\u00ecnh s\u1ebd chia s\u1ebb to\u00e0n b\u1ed9 c\u00e1c c\u00e1ch th\u00eam ph\u1ea7n t\u1eed v\u00e0o tuple, ph\u00e2n t\u00edch \u01b0u \u2013 nh\u01b0\u1ee3c \u0111i\u1ec3m, k\u00e8m v\u00ed d\u1ee5 minh h\u1ecda, v\u00e0 c\u1ea3 nh\u1eefng l\u01b0u \u00fd quan tr\u1ecdng v\u1ec1 hi\u1ec7u su\u1ea5t. Ngo\u00e0i ra, m\u00ecnh s\u1ebd b\u1ed5 sung m\u1ed9t s\u1ed1 b\u00e0i t\u1eadp th\u1ef1c h\u00e0nh \u0111\u1ec3 b\u1ea1n d\u1ec5 h\u1ec7 th\u1ed1ng ki\u1ebfn th\u1ee9c.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u0110\u1eb7c \u0111i\u1ec3m c\u1ee7a Tuple v\u00e0 l\u00fd do kh\u00f4ng th\u1ec3 th\u00eam tr\u1ef1c ti\u1ebfp<\/h2>\n\n\n\n<p>Tuple trong Python l\u00e0 m\u1ed9t lo\u1ea1i c\u1ea5u tr\u00fac d\u1eef li\u1ec7u gi\u1ed1ng list, nh\u01b0ng v\u1edbi m\u1ed9t \u0111i\u1ec3m kh\u00e1c bi\u1ec7t quan tr\u1ecdng: <strong>tuple l\u00e0 immutable<\/strong> \u2014 ngh\u0129a l\u00e0 sau khi t\u1ea1o tuple, m\u00ecnh kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i, th\u00eam ho\u1eb7c x\u00f3a ph\u1ea7n t\u1eed b\u00ean trong n\u00f3.<\/p>\n\n\n\n<p>\u0110i\u1ec1u n\u00e0y mang l\u1ea1i m\u1ed9t s\u1ed1 \u0111\u1eb7c \u0111i\u1ec3m quan tr\u1ecdng:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>B\u1ea3o to\u00e0n d\u1eef li\u1ec7u:<\/strong> V\u00ec tuple kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i, n\u00f3 b\u1ea3o v\u1ec7 d\u1eef li\u1ec7u tr\u00e1nh b\u1ecb ch\u1ec9nh s\u1eeda ngo\u00e0i \u00fd mu\u1ed1n. \u0110i\u1ec1u n\u00e0y r\u1ea5t h\u1eefu \u00edch khi m\u00ecnh mu\u1ed1n \u0111\u1ea3m b\u1ea3o d\u1eef li\u1ec7u b\u1ea5t bi\u1ebfn trong su\u1ed1t qu\u00e1 tr\u00ecnh ch\u01b0\u01a1ng tr\u00ecnh ch\u1ea1y.<\/li>\n\n\n\n<li><strong>Hi\u1ec7u su\u1ea5t cao h\u01a1n list:<\/strong> Tuple th\u01b0\u1eddng chi\u1ebfm \u00edt b\u1ed9 nh\u1edb h\u01a1n list v\u00e0 ho\u1ea1t \u0111\u1ed9ng nhanh h\u01a1n trong nhi\u1ec1u tr\u01b0\u1eddng h\u1ee3p, nh\u1ea5t l\u00e0 khi tuple \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng l\u00e0m key trong dictionary ho\u1eb7c l\u01b0u tr\u1eef d\u1eef li\u1ec7u c\u1ed1 \u0111\u1ecbnh.<\/li>\n\n\n\n<li><strong>\u1ee8ng d\u1ee5ng trong l\u1eadp tr\u00ecnh:<\/strong> Tuple th\u01b0\u1eddng d\u00f9ng \u0111\u1ec3 l\u01b0u tr\u1eef d\u1eef li\u1ec7u nhi\u1ec1u gi\u00e1 tr\u1ecb kh\u00f4ng thay \u0111\u1ed5i, nh\u01b0 t\u1ecda \u0111\u1ed9 <code>(x, y)<\/code>, th\u00f4ng tin c\u1ea5u h\u00ecnh ho\u1eb7c tr\u1ea3 v\u1ec1 nhi\u1ec1u gi\u00e1 tr\u1ecb t\u1eeb m\u1ed9t h\u00e0m.<\/li>\n<\/ul>\n\n\n\n<p>Tuy nhi\u00ean, <strong>\u0111i\u1ec3m b\u1ea5t ti\u1ec7n l\u1edbn nh\u1ea5t c\u1ee7a tuple<\/strong> ch\u00ednh l\u00e0 t\u00ednh b\u1ea5t bi\u1ebfn n\u00e0y \u2014 m\u00ecnh kh\u00f4ng th\u1ec3 tr\u1ef1c ti\u1ebfp th\u00eam ph\u1ea7n t\u1eed v\u00e0o tuple nh\u01b0 v\u1edbi list (v\u00ed d\u1ee5 tuple kh\u00f4ng c\u00f3 ph\u01b0\u01a1ng th\u1ee9c <code>append()<\/code> hay <code>insert()<\/code>). N\u1ebfu mu\u1ed1n th\u00eam ph\u1ea7n t\u1eed, m\u00ecnh bu\u1ed9c ph\u1ea3i t\u1ea1o m\u1ed9t tuple m\u1edbi d\u1ef1a tr\u00ean tuple g\u1ed1c.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 minh h\u1ecda:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>coordinates = (10, 20)\n# coordinates.append(30)  # \u274c This will raise an AttributeError\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">coordinates <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">20<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># coordinates.append(30)  # \u274c This will raise an AttributeError<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Thao t\u00e1c tr\u00ean s\u1ebd g\u00e2y l\u1ed7i v\u00ec tuple kh\u00f4ng h\u1ed7 tr\u1ee3 ph\u01b0\u01a1ng th\u1ee9c <code>append()<\/code>.<\/p>\n\n\n\n<p>Ch\u00ednh v\u00ec v\u1eady, khi l\u00e0m vi\u1ec7c v\u1edbi tuple, m\u00ecnh c\u1ea7n hi\u1ec3u r\u00f5 c\u00e1c ph\u01b0\u01a1ng ph\u00e1p \u0111\u1ec3 th\u00eam ph\u1ea7n t\u1eed sao cho v\u1eeba \u0111\u1ea3m b\u1ea3o t\u00ednh b\u1ea5t bi\u1ebfn, v\u1eeba \u0111\u1ea1t hi\u1ec7u qu\u1ea3 t\u1ed1i \u01b0u. Ph\u1ea7n ti\u1ebfp theo m\u00ecnh s\u1ebd \u0111i s\u00e2u v\u00e0o t\u1eebng ph\u01b0\u01a1ng ph\u00e1p c\u1ee5 th\u1ec3.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Th\u00eam ph\u1ea7n t\u1eed v\u00e0o Tuple b\u1eb1ng ph\u00e9p c\u1ed9ng (Concatenation)<\/h2>\n\n\n\n<p>V\u00ec tuple l\u00e0 immutable, m\u00ecnh kh\u00f4ng th\u1ec3 th\u00eam ph\u1ea7n t\u1eed tr\u1ef1c ti\u1ebfp. M\u1ed9t trong nh\u1eefng c\u00e1ch ph\u1ed5 bi\u1ebfn nh\u1ea5t \u0111\u1ec3 th\u00eam ph\u1ea7n t\u1eed v\u00e0o tuple l\u00e0 <strong>t\u1ea1o tuple m\u1edbi b\u1eb1ng ph\u00e9p c\u1ed9ng (concatenation)<\/strong>.<\/p>\n\n\n\n<p>Ph\u00e9p c\u1ed9ng tuple ho\u1ea1t \u0111\u1ed9ng b\u1eb1ng c\u00e1ch n\u1ed1i hai tuple l\u1ea1i v\u1edbi nhau, t\u1ea1o ra m\u1ed9t tuple m\u1edbi ho\u00e0n to\u00e0n. \u0110\u00e2y l\u00e0 c\u00e1ch \u0111\u01a1n gi\u1ea3n nh\u1ea5t \u0111\u1ec3 th\u00eam ph\u1ea7n t\u1eed, \u0111\u1ed3ng th\u1eddi gi\u1eef nguy\u00ean t\u00ednh b\u1ea5t bi\u1ebfn c\u1ee7a tuple g\u1ed1c.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Example: add element to tuple using concatenation\nnumbers = (1, 2, 3)\nnumbers = numbers + (4,)  # Add new element\nprint(numbers)  # (1, 2, 3, 4)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Example: add element to tuple using concatenation<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> numbers <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">,)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># Add new element<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (1, 2, 3, 4)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\ud83d\udd39 L\u01b0u \u00fd quan tr\u1ecdng:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Khi th\u00eam m\u1ed9t ph\u1ea7n t\u1eed \u0111\u01a1n l\u1ebb, m\u00ecnh ph\u1ea3i vi\u1ebft <code>(4,)<\/code> v\u1edbi d\u1ea5u ph\u1ea9y, \u0111\u1ec3 Python hi\u1ec3u \u0111\u00e2y l\u00e0 tuple, kh\u00f4ng ph\u1ea3i gi\u00e1 tr\u1ecb \u0111\u01a1n.<\/li>\n\n\n\n<li>To\u00e0n b\u1ed9 tuple m\u1edbi \u0111\u01b0\u1ee3c t\u1ea1o ra, tuple g\u1ed1c <code>numbers<\/code> v\u1eabn kh\u00f4ng thay \u0111\u1ed5i n\u1ebfu m\u00ecnh kh\u00f4ng g\u00e1n l\u1ea1i (<code>numbers = ...<\/code>).<\/li>\n<\/ul>\n\n\n\n<p>C\u00e1ch n\u00e0y r\u1ea5t ph\u00f9 h\u1ee3p khi m\u00ecnh c\u1ea7n th\u00eam m\u1ed9t ho\u1eb7c v\u00e0i ph\u1ea7n t\u1eed v\u00e0o tuple m\u1ed9t l\u1ea7n, nh\u01b0ng kh\u00f4ng th\u00edch thao t\u00e1c ph\u1ee9c t\u1ea1p.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed v\u00e0o Tuple b\u1eb1ng ph\u00e9p c\u1ed9ng<\/h2>\n\n\n\n<p>N\u1ebfu mu\u1ed1n th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed c\u00f9ng l\u00fac, m\u00ecnh c\u0169ng d\u00f9ng ph\u00e9p c\u1ed9ng, nh\u01b0ng ph\u1ea7n t\u1eed c\u1ea7n th\u00eam ph\u1ea3i \u0111\u01b0\u1ee3c \u0111\u00f3ng g\u00f3i trong m\u1ed9t tuple.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Example: add multiple elements to tuple\nnumbers = (1, 2, 3)\nnumbers = numbers + (4, 5, 6)  # Add multiple elements\nprint(numbers)  # (1, 2, 3, 4, 5, 6)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Example: add multiple elements to tuple<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> numbers <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">6<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># Add multiple elements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (1, 2, 3, 4, 5, 6)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0110i\u1ec3m c\u1ea7n l\u01b0u \u00fd khi d\u00f9ng ph\u01b0\u01a1ng ph\u00e1p n\u00e0y:\n<ul class=\"wp-block-list\">\n<li>Ph\u1ea3i \u0111\u1ea3m b\u1ea3o ph\u1ea7n t\u1eed c\u1ea7n th\u00eam \u0111\u01b0\u1ee3c \u0111\u1eb7t trong tuple, ngh\u0129a l\u00e0 ph\u1ea3i c\u00f3 d\u1ea5u ph\u1ea9y n\u1ebfu ch\u1ec9 c\u00f3 m\u1ed9t ph\u1ea7n t\u1eed.<\/li>\n\n\n\n<li>Ph\u00e9p c\u1ed9ng t\u1ea1o tuple m\u1edbi \u2192 s\u1ebd t\u1ed1n b\u1ed9 nh\u1edb h\u01a1n n\u1ebfu th\u00eam ph\u1ea7n t\u1eed nhi\u1ec1u l\u1ea7n.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u01afu \u0111i\u1ec3m c\u1ee7a ph\u00e9p c\u1ed9ng:\n<ul class=\"wp-block-list\">\n<li>C\u00fa ph\u00e1p \u0111\u01a1n gi\u1ea3n, d\u1ec5 \u0111\u1ecdc v\u00e0 d\u1ec5 hi\u1ec3u.<\/li>\n\n\n\n<li>Gi\u1eef nguy\u00ean t\u00ednh b\u1ea5t bi\u1ebfn c\u1ee7a tuple ban \u0111\u1ea7u.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Nh\u01b0\u1ee3c \u0111i\u1ec3m:\n<ul class=\"wp-block-list\">\n<li>M\u1ed7i l\u1ea7n th\u00eam ph\u1ea7n t\u1eed s\u1ebd t\u1ea1o tuple m\u1edbi \u2192 kh\u00f4ng hi\u1ec7u qu\u1ea3 n\u1ebfu thao t\u00e1c n\u00e0y x\u1ea3y ra nhi\u1ec1u l\u1ea7n trong ch\u01b0\u01a1ng tr\u00ecnh.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>M\u1ed9t v\u00ed d\u1ee5 th\u1ef1c t\u1ebf:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Adding multiple elements efficiently\ncoordinates = (10, 20)\nextra = (30, 40, 50)\ncoordinates = coordinates + extra\nprint(coordinates)  # (10, 20, 30, 40, 50)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Adding multiple elements efficiently<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">coordinates <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">20<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">extra <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">30<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">40<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">50<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">coordinates <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> coordinates <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> extra<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">coordinates<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (10, 20, 30, 40, 50)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>C\u00e1ch n\u00e0y r\u1ea5t h\u1eefu d\u1ee5ng khi m\u00ecnh \u0111\u00e3 c\u00f3 m\u1ed9t tuple ch\u1ee9a c\u00e1c ph\u1ea7n t\u1eed c\u1ea7n th\u00eam, ho\u1eb7c khi d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c thu th\u1eadp theo nh\u00f3m.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Th\u00eam ph\u1ea7n t\u1eed b\u1eb1ng c\u00e1ch chuy\u1ec3n Tuple th\u00e0nh List<\/h2>\n\n\n\n<p>M\u1ed9t ph\u01b0\u01a1ng ph\u00e1p ph\u1ed5 bi\u1ebfn kh\u00e1c \u0111\u1ec3 th\u00eam ph\u1ea7n t\u1eed v\u00e0o tuple l\u00e0 <strong>chuy\u1ec3n tuple th\u00e0nh list<\/strong>, th\u1ef1c hi\u1ec7n thao t\u00e1c th\u00eam ph\u1ea7n t\u1eed v\u1edbi list, sau \u0111\u00f3 chuy\u1ec3n l\u1ea1i v\u1ec1 tuple.<\/p>\n\n\n\n<p>V\u00ec list l\u00e0 mutable n\u00ean m\u00ecnh c\u00f3 th\u1ec3 d\u00f9ng c\u00e1c ph\u01b0\u01a1ng th\u1ee9c nh\u01b0 <code>append()<\/code>, <code>insert()<\/code> ho\u1eb7c <code>extend()<\/code> \u0111\u1ec3 th\u00eam ph\u1ea7n t\u1eed d\u1ec5 d\u00e0ng. Sau \u0111\u00f3, tuple m\u1edbi \u0111\u01b0\u1ee3c t\u1ea1o ra b\u1eb1ng c\u00e1ch convert l\u1ea1i t\u1eeb list.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Example: convert tuple to list and append an element\nnumbers = (1, 2, 3)\nnumbers_list = list(numbers)  # Convert tuple to list\nnumbers_list.append(4)        # Append new element\nnumbers = tuple(numbers_list) # Convert back to tuple\nprint(numbers)                # (1, 2, 3, 4)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Example: convert tuple to list and append an element<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers_list <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">list<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># Convert tuple to list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers_list<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">append<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #616E88\"># Append new element<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">tuple<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers_list<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\"># Convert back to tuple<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">                <\/span><span style=\"color: #616E88\"># (1, 2, 3, 4)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>L\u01b0u \u00fd:\n<ul class=\"wp-block-list\">\n<li>Ph\u01b0\u01a1ng ph\u00e1p n\u00e0y t\u01b0\u01a1ng \u0111\u1ed1i d\u1ec5 hi\u1ec3u v\u00e0 linh ho\u1ea1t.<\/li>\n\n\n\n<li>Tuy nhi\u00ean, qu\u00e1 tr\u00ecnh chuy\u1ec3n \u0111\u1ed5i t\u1ed1n th\u00eam th\u1eddi gian v\u00e0 b\u1ed9 nh\u1edb, \u0111\u1eb7c bi\u1ec7t khi l\u00e0m vi\u1ec7c v\u1edbi tuple l\u1edbn.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>V\u00ed d\u1ee5 th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>numbers = (1, 2, 3)\nnumbers_list = list(numbers)\nnumbers_list.extend(&#91;4, 5, 6&#93;)  # Add multiple elements\nnumbers = tuple(numbers_list)\nprint(numbers)  # (1, 2, 3, 4, 5, 6)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers_list <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">list<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers_list<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">extend<\/span><span style=\"color: #ECEFF4\">(&#91;<\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">6<\/span><span style=\"color: #ECEFF4\">&#93;)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># Add multiple elements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">tuple<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers_list<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (1, 2, 3, 4, 5, 6)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>M\u00ecnh th\u01b0\u1eddng d\u00f9ng c\u00e1ch n\u00e0y khi c\u1ea7n thao t\u00e1c ph\u1ee9c t\u1ea1p h\u01a1n l\u00e0 ch\u1ec9 th\u00eam ph\u1ea7n t\u1eed \u0111\u01a1n l\u1ebb, v\u00ed d\u1ee5 nh\u01b0 ch\u00e8n v\u00e0o v\u1ecb tr\u00ed c\u1ee5 th\u1ec3 ho\u1eb7c th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed c\u00f9ng l\u00fac.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Th\u00eam ph\u1ea7n t\u1eed v\u00e0o Tuple b\u1eb1ng unpacking<\/h2>\n\n\n\n<p>Python h\u1ed7 tr\u1ee3 unpacking tuple, gi\u00fap m\u00ecnh t\u1ea1o tuple m\u1edbi m\u1ed9t c\u00e1ch linh ho\u1ea1t m\u00e0 kh\u00f4ng c\u1ea7n chuy\u1ec3n \u0111\u1ed5i ki\u1ec3u d\u1eef li\u1ec7u. \u0110\u00e2y l\u00e0 m\u1ed9t c\u00e1ch r\u1ea5t Pythonic v\u00e0 ng\u1eafn g\u1ecdn.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Example: add element to tuple using unpacking\nnumbers = (1, 2, 3)\nnumbers = (*numbers, 4)  # Unpack old tuple and add new element\nprint(numbers)  # (1, 2, 3, 4)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Example: add element to tuple using unpacking<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">*<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># Unpack old tuple and add new element<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (1, 2, 3, 4)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>numbers = (*numbers, 5, 6)\nprint(numbers)  # (1, 2, 3, 4, 5, 6)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">*<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">6<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (1, 2, 3, 4, 5, 6)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u01afu \u0111i\u1ec3m:\n<ul class=\"wp-block-list\">\n<li>C\u00fa ph\u00e1p g\u1ecdn g\u00e0ng, d\u1ec5 hi\u1ec3u.<\/li>\n\n\n\n<li>Kh\u00f4ng c\u1ea7n chuy\u1ec3n \u0111\u1ed5i d\u1eef li\u1ec7u.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Nh\u01b0\u1ee3c \u0111i\u1ec3m:\n<ul class=\"wp-block-list\">\n<li>M\u1ed7i thao t\u00e1c t\u1ea1o tuple m\u1edbi \u2192 t\u1ed1n b\u1ed9 nh\u1edb.<\/li>\n\n\n\n<li>N\u1ebfu th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed li\u00ean t\u1ee5c, c\u00f3 th\u1ec3 g\u00e2y gi\u1ea3m hi\u1ec7u su\u1ea5t.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Unpacking \u0111\u1eb7c bi\u1ec7t h\u1eefu \u00edch khi m\u00ecnh mu\u1ed1n gh\u00e9p nhi\u1ec1u tuple ho\u1eb7c th\u00eam ph\u1ea7n t\u1eed v\u00e0o tuple m\u1ed9t c\u00e1ch nhanh ch\u00f3ng m\u00e0 kh\u00f4ng c\u1ea7n d\u00f9ng h\u00e0m hay ph\u01b0\u01a1ng th\u1ee9c ph\u1ee9c t\u1ea1p.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Th\u00eam ph\u1ea7n t\u1eed v\u00e0o Tuple b\u1eb1ng h\u00e0m t\u1ef1 \u0111\u1ecbnh ngh\u0129a<\/h2>\n\n\n\n<p>\u0110\u1ec3 gi\u00fap m\u00e3 ngu\u1ed3n g\u1ecdn g\u00e0ng v\u00e0 d\u1ec5 t\u00e1i s\u1eed d\u1ee5ng, m\u00ecnh c\u00f3 th\u1ec3 \u0111\u1ecbnh ngh\u0129a m\u1ed9t h\u00e0m \u0111\u1ec3 th\u00eam ph\u1ea7n t\u1eed v\u00e0o tuple.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Example: function to add element to tuple\ndef add_to_tuple(tpl, value):\n    \"\"\"Return a new tuple with value added at the end.\"\"\"\n    return tpl + (value,)\n\nnumbers = (1, 2, 3)\nnumbers = add_to_tuple(numbers, 4)\nprint(numbers)  # (1, 2, 3, 4)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Example: function to add element to tuple<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">add_to_tuple<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">tpl<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">value<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">&quot;&quot;&quot;<\/span><span style=\"color: #A3BE8C\">Return a new tuple with value added at the end.<\/span><span style=\"color: #ECEFF4\">&quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> tpl <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">value<\/span><span style=\"color: #ECEFF4\">,)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">add_to_tuple<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (1, 2, 3, 4)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u01afu \u0111i\u1ec3m c\u1ee7a c\u00e1ch n\u00e0y:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Code r\u00f5 r\u00e0ng, t\u00e1i s\u1eed d\u1ee5ng d\u1ec5 d\u00e0ng.<\/li>\n\n\n\n<li>Gi\u1ea3m s\u1ef1 tr\u00f9ng l\u1eb7p khi th\u00eam ph\u1ea7n t\u1eed nhi\u1ec1u l\u1ea7n.<\/li>\n<\/ul>\n\n\n\n<p>M\u00ecnh c\u0169ng c\u00f3 th\u1ec3 m\u1edf r\u1ed9ng h\u00e0m \u0111\u1ec3 th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>def add_elements_to_tuple(tpl, *values):\n    \"\"\"Return a new tuple with multiple values added.\"\"\"\n    return tpl + values\n\nnumbers = (1, 2, 3)\nnumbers = add_elements_to_tuple(numbers, 4, 5, 6)\nprint(numbers)  # (1, 2, 3, 4, 5, 6)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">add_elements_to_tuple<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">tpl<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">*<\/span><span style=\"color: #D8DEE9\">values<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">&quot;&quot;&quot;<\/span><span style=\"color: #A3BE8C\">Return a new tuple with multiple values added.<\/span><span style=\"color: #ECEFF4\">&quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> tpl <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> values<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">add_elements_to_tuple<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">6<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (1, 2, 3, 4, 5, 6)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">7. L\u01b0u \u00fd v\u1ec1 hi\u1ec7u su\u1ea5t<\/h2>\n\n\n\n<p>V\u00ec tuple l\u00e0 immutable, m\u1ecdi thao t\u00e1c th\u00eam ph\u1ea7n t\u1eed \u0111\u1ec1u <strong>t\u1ea1o ra tuple m\u1edbi<\/strong>, \u0111i\u1ec1u n\u00e0y \u1ea3nh h\u01b0\u1edfng t\u1edbi hi\u1ec7u su\u1ea5t khi thao t\u00e1c nhi\u1ec1u l\u1ea7n:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>N\u1ebfu ch\u1ec9 th\u00eam m\u1ed9t ho\u1eb7c v\u00e0i ph\u1ea7n t\u1eed \u2192 d\u00f9ng ph\u00e9p c\u1ed9ng ho\u1eb7c unpacking l\u00e0 nhanh v\u00e0 d\u1ec5 nh\u1ea5t.<\/li>\n\n\n\n<li>N\u1ebfu th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed ho\u1eb7c thao t\u00e1c th\u01b0\u1eddng xuy\u00ean \u2192 n\u00ean chuy\u1ec3n tuple th\u00e0nh list, x\u1eed l\u00fd, r\u1ed3i convert l\u1ea1i th\u00e0nh tuple \u0111\u1ec3 gi\u1ea3m chi ph\u00ed t\u1ea1o m\u1edbi tuple nhi\u1ec1u l\u1ea7n.<\/li>\n\n\n\n<li>N\u1ebfu d\u1eef li\u1ec7u c\u1ea7n thay \u0111\u1ed5i th\u01b0\u1eddng xuy\u00ean \u2192 c\u00e2n nh\u1eafc d\u00f9ng list ho\u1eb7c <code>collections.deque<\/code> thay v\u00ec tuple.<\/li>\n<\/ul>\n\n\n\n<p>V\u00ed d\u1ee5 so s\u00e1nh:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import time\n\ntpl = tuple(range(1000000))\n\n# Using concatenation repeatedly\nstart = time.time()\nfor i in range(100):\n    tpl = tpl + (i,)\nprint(\"Concatenation time:\", time.time() - start)\n\n# Using list conversion\nstart = time.time()\nlst = list(tpl)\nfor i in range(100):\n    lst.append(i)\ntpl = tuple(lst)\nprint(\"List conversion time:\", time.time() - start)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> time<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">tpl <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">tuple<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">range<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1000000<\/span><span style=\"color: #ECEFF4\">))<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Using concatenation repeatedly<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">start <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> time<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">time<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> i <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">range<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">100<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    tpl <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> tpl <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">i<\/span><span style=\"color: #ECEFF4\">,)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Concatenation time:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> time<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">time<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9FF\"> start<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Using list conversion<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">start <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> time<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">time<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">lst <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">list<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">tpl<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> i <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">range<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">100<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    lst<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">append<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">i<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">tpl <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">tuple<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lst<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">List conversion time:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> time<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">time<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9FF\"> start<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>K\u1ebft qu\u1ea3 s\u1ebd cho th\u1ea5y c\u00e1ch d\u00f9ng list conversion th\u01b0\u1eddng nhanh h\u01a1n khi th\u00eam ph\u1ea7n t\u1eed nhi\u1ec1u l\u1ea7n v\u00e0o tuple l\u1edbn.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. B\u00e0i t\u1eadp th\u1ef1c h\u00e0nh<\/h2>\n\n\n\n<p>\u0110\u1ec3 c\u1ee7ng c\u1ed1 ki\u1ebfn th\u1ee9c, b\u1ea1n c\u00f3 th\u1ec3 th\u1eed m\u1ed9t v\u00e0i b\u00e0i t\u1eadp nh\u1ecf:<\/p>\n\n\n\n<p><strong>B\u00e0i t\u1eadp 1:<\/strong> Vi\u1ebft h\u00e0m th\u00eam m\u1ed9t ph\u1ea7n t\u1eed v\u00e0o tuple.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>def add_element(tpl, value):\n    return tpl + (value,)\n\nprint(add_element((1, 2, 3), 4))  # (1, 2, 3, 4)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">add_element<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">tpl<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">value<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> tpl <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">value<\/span><span style=\"color: #ECEFF4\">,)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">add_element<\/span><span style=\"color: #ECEFF4\">((<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">),<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (1, 2, 3, 4)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>B\u00e0i t\u1eadp 2:<\/strong> Th\u00eam nhi\u1ec1u ph\u1ea7n t\u1eed v\u00e0o tuple.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>def add_elements(tpl, *values):\n    return tpl + values\n\nprint(add_elements((1, 2, 3), 4, 5))  # (1, 2, 3, 4, 5)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">add_elements<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">tpl<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">*<\/span><span style=\"color: #D8DEE9\">values<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> tpl <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> values<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">add_elements<\/span><span style=\"color: #ECEFF4\">((<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">),<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (1, 2, 3, 4, 5)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>B\u00e0i t\u1eadp 3:<\/strong> Chuy\u1ec3n tuple th\u00e0nh list \u0111\u1ec3 th\u00eam ph\u1ea7n t\u1eed, r\u1ed3i quay l\u1ea1i tuple.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>def append_using_list(tpl, value):&lt;br>    temp_list = list(tpl)&lt;br>    temp_list.append(value)&lt;br>    return tuple(temp_list)&lt;br>&lt;br>print(append_using_list((10, 20), 30))  # (10, 20, 30)<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">append_using_list<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">tpl<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">value<\/span><span style=\"color: #ECEFF4\">):<\/span><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">br<\/span><span style=\"color: #81A1C1\">&gt;<\/span><span style=\"color: #D8DEE9FF\">    temp_list <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">list<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">tpl<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">br<\/span><span style=\"color: #81A1C1\">&gt;<\/span><span style=\"color: #D8DEE9FF\">    temp_list<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">append<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">value<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">br<\/span><span style=\"color: #81A1C1\">&gt;<\/span><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">tuple<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">temp_list<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">br<\/span><span style=\"color: #81A1C1\">&gt;&lt;<\/span><span style=\"color: #D8DEE9FF\">br<\/span><span style=\"color: #81A1C1\">&gt;<\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">append_using_list<\/span><span style=\"color: #ECEFF4\">((<\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">20<\/span><span style=\"color: #ECEFF4\">),<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">30<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (10, 20, 30)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">9. K\u1ebft lu\u1eadn<\/h2>\n\n\n\n<p>Trong Python, tuple l\u00e0 immutable n\u00ean m\u00ecnh kh\u00f4ng th\u1ec3 tr\u1ef1c ti\u1ebfp th\u00eam ph\u1ea7n t\u1eed. Tuy nhi\u00ean, v\u1eabn c\u00f3 nhi\u1ec1u c\u00e1ch \u0111\u1ec3 <strong>th\u00eam ph\u1ea7n t\u1eed v\u00e0o tuple<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>D\u00f9ng ph\u00e9p c\u1ed9ng (concatenation).<\/li>\n\n\n\n<li>Chuy\u1ec3n tuple th\u00e0nh list r\u1ed3i d\u00f9ng <code>append()<\/code>.<\/li>\n\n\n\n<li>D\u00f9ng unpacking.<\/li>\n\n\n\n<li>T\u1ea1o h\u00e0m h\u1ed7 tr\u1ee3 t\u00e1i s\u1eed d\u1ee5ng.<\/li>\n<\/ul>\n\n\n\n<p>M\u1ed7i c\u00e1ch c\u00f3 \u01b0u \u2013 nh\u01b0\u1ee3c \u0111i\u1ec3m ri\u00eang. Vi\u1ec7c l\u1ef1a ch\u1ecdn ph\u01b0\u01a1ng ph\u00e1p ph\u1ee5 thu\u1ed9c v\u00e0o y\u00eau c\u1ea7u v\u1ec1 hi\u1ec7u su\u1ea5t v\u00e0 t\u00ednh d\u1ec5 \u0111\u1ecdc c\u1ee7a code. Hi\u1ec3u r\u00f5 nh\u1eefng c\u00e1ch n\u00e0y s\u1ebd gi\u00fap m\u00ecnh x\u1eed l\u00fd tuple linh ho\u1ea1t h\u01a1n, d\u00f9 b\u1ea3n ch\u1ea5t tuple l\u00e0 b\u1ea5t bi\u1ebfn.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">10. T\u00e0i li\u1ec7u tham kh\u1ea3o<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python Software Foundation. <em>Built-in Types \u2014 tuple<\/em>. Python 3 Documentation. <a>https:\/\/docs.python.org\/3\/library\/stdtypes.html#tuple<\/a><\/li>\n\n\n\n<li>Luciano Ramalho. <em>Fluent Python, 2nd Edition<\/em>. O\u2019Reilly Media, 2021.<\/li>\n\n\n\n<li>David Beazley. <em>Python Cookbook, 3rd Edition<\/em>. O\u2019Reilly Media, 2013.<\/li>\n\n\n\n<li>Python for Professionals: Learning Python as a Second Language: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710002222624882405978&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-for-professionals-3\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-for-professionals-3<\/a><\/li>\n\n\n\n<li>Python: Deeper Insights into Machine Learning: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710015810095319857183&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-deeper-insights-into-machine-learning\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-deeper-insights-into-machine-learning<\/a><\/li>\n\n\n\n<li>DataFusion Python Bindings in Practice: The Complete Guide for Developers and Engineers: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710049093362364820452&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fdatafusion-python-bindings-in-practice\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/datafusion-python-bindings-in-practice<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Trong Python, tuple l\u00e0 m\u1ed9t lo\u1ea1i c\u1ea5u tr\u00fac d\u1eef li\u1ec7u r\u1ea5t gi\u1ed1ng list nh\u01b0ng immutable (kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i tr\u1ef1c ti\u1ebfp). \u0110i\u1ec1u n\u00e0y ngh\u0129a l\u00e0 sau khi tuple \u0111\u01b0\u1ee3c t\u1ea1o ra, m\u00ecnh kh\u00f4ng th\u1ec3 th\u00eam, x\u00f3a ho\u1eb7c thay \u0111\u1ed5i ph\u1ea7n t\u1eed b\u00ean trong tuple \u0111\u00f3.<\/p>\n","protected":false},"author":1,"featured_media":1723,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowieHDDA:productID":"","footnotes":""},"categories":[41],"tags":[40],"class_list":["post-1527","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kien-thuc-lap-trinh","tag-python-co-ban"],"_links":{"self":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/comments?post=1527"}],"version-history":[{"count":5,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1527\/revisions"}],"predecessor-version":[{"id":2454,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1527\/revisions\/2454"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media\/1723"}],"wp:attachment":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media?parent=1527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/categories?post=1527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/tags?post=1527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}