{"id":155,"date":"2025-02-21T04:23:02","date_gmt":"2025-02-21T04:23:02","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=155"},"modified":"2025-02-21T04:28:14","modified_gmt":"2025-02-21T04:28:14","slug":"nhung-loi-sai-thuong-gap-khi-moi-hoc-python","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=155","title":{"rendered":"Nh\u1eefng L\u1ed7i Sai Th\u01b0\u1eddng G\u1eb7p Khi M\u1edbi H\u1ecdc Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-40.png\" alt=\"\" class=\"wp-image-157\" srcset=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-40.png 1024w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-40-300x200.png 300w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-40-768x512.png 768w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-40-850x567.png 850w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Python l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh ph\u1ed5 bi\u1ebfn v\u00e0 d\u1ec5 h\u1ecdc, nh\u01b0ng ngay c\u1ea3 nh\u1eefng ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u c\u0169ng th\u01b0\u1eddng g\u1eb7p ph\u1ea3i m\u1ed9t s\u1ed1 l\u1ed7i ph\u1ed5 bi\u1ebfn trong qu\u00e1 tr\u00ecnh h\u1ecdc. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 nh\u1eefng l\u1ed7i sai th\u01b0\u1eddng g\u1eb7p m\u00e0 b\u1ea1n c\u1ea7n ch\u00fa \u00fd \u0111\u1ec3 tr\u00e1nh.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong>Qu\u00ean D\u1ea5u C\u00e1ch (Indentation)<\/strong><\/h4>\n\n\n\n<p>Python s\u1eed d\u1ee5ng kho\u1ea3ng tr\u1eafng (indentation) \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh c\u00e1c kh\u1ed1i l\u1ec7nh thay v\u00ec d\u1ea5u ngo\u1eb7c nh\u1ecdn nh\u01b0 trong c\u00e1c ng\u00f4n ng\u1eef kh\u00e1c. M\u1ed9t l\u1ed7i ph\u1ed5 bi\u1ebfn l\u00e0 qu\u00ean th\u1ee5t \u0111\u1ea7u d\u00f2ng khi vi\u1ebft m\u00e3, d\u1eabn \u0111\u1ebfn l\u1ed7i c\u00fa ph\u00e1p.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 sai:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>def say_hello():<br>print(\"Hello, World!\")<br><\/code><\/pre>\n\n\n\n<p><strong>C\u00e1ch s\u1eeda:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>def say_hello():<br>    print(\"Hello, World!\")<br><\/code><\/pre>\n\n\n\n<p>\u0110\u1ea3m b\u1ea3o r\u1eb1ng c\u00e1c c\u00e2u l\u1ec7nh trong kh\u1ed1i <code>if<\/code>, <code>for<\/code>, <code>def<\/code>&#8230; \u0111\u1ec1u \u0111\u01b0\u1ee3c th\u1ee5t l\u1ec1 \u0111\u00fang.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>Kh\u00f4ng \u0110\u1eb7t Gi\u00e1 Tr\u1ecb Cho Bi\u1ebfn Tr\u01b0\u1edbc Khi S\u1eed D\u1ee5ng<\/strong><\/h4>\n\n\n\n<p>M\u1ed9t s\u1ed1 ng\u01b0\u1eddi m\u1edbi h\u1ecdc Python c\u00f3 th\u1ec3 g\u1eb7p ph\u1ea3i l\u1ed7i khi s\u1eed d\u1ee5ng bi\u1ebfn m\u00e0 ch\u01b0a g\u00e1n gi\u00e1 tr\u1ecb cho n\u00f3. \u0110i\u1ec1u n\u00e0y s\u1ebd d\u1eabn \u0111\u1ebfn l\u1ed7i <code>NameError<\/code>.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 sai:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>print(x)<br><\/code><\/pre>\n\n\n\n<p><strong>C\u00e1ch s\u1eeda:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>x = 10<br>print(x)<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong>Nh\u1ea7m L\u1eabn Gi\u1eefa D\u1ea5u &#8220;=&#8221; v\u00e0 &#8220;==&#8221;<\/strong><\/h4>\n\n\n\n<p>\u0110\u00e2y l\u00e0 m\u1ed9t l\u1ed7i c\u01a1 b\u1ea3n nh\u01b0ng ph\u1ed5 bi\u1ebfn. D\u1ea5u &#8220;=&#8221; d\u00f9ng \u0111\u1ec3 g\u00e1n gi\u00e1 tr\u1ecb cho bi\u1ebfn, trong khi &#8220;==&#8221; d\u00f9ng \u0111\u1ec3 so s\u00e1nh hai gi\u00e1 tr\u1ecb.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 sai:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>if x = 10:<br>    print(\"x is 10\")<br><\/code><\/pre>\n\n\n\n<p><strong>C\u00e1ch s\u1eeda:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>if x == 10:<br>    print(\"x is 10\")<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. <strong>L\u1ed7i C\u00fa Ph\u00e1p V\u1edbi D\u1ea5u Ngo\u1eb7c<\/strong><\/h4>\n\n\n\n<p>Python y\u00eau c\u1ea7u c\u00e1c d\u1ea5u ngo\u1eb7c \u0111\u01a1n, ngo\u1eb7c k\u00e9p, v\u00e0 ngo\u1eb7c nh\u1ecdn ph\u1ea3i \u0111\u01b0\u1ee3c \u0111\u00f3ng \u0111\u00fang c\u00e1ch. N\u1ebfu thi\u1ebfu ho\u1eb7c sai d\u1ea5u ngo\u1eb7c, b\u1ea1n s\u1ebd g\u1eb7p ph\u1ea3i l\u1ed7i c\u00fa ph\u00e1p.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 sai:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>print(\"Hello, World!)<br><\/code><\/pre>\n\n\n\n<p><strong>C\u00e1ch s\u1eeda:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>print(\"Hello, World!\")<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5. <strong>L\u1ed7i V\u1edbi C\u00e1c Ki\u1ec3u D\u1eef Li\u1ec7u<\/strong><\/h4>\n\n\n\n<p>Vi\u1ec7c s\u1eed d\u1ee5ng sai ki\u1ec3u d\u1eef li\u1ec7u l\u00e0 m\u1ed9t l\u1ed7i kh\u00e1 ph\u1ed5 bi\u1ebfn \u0111\u1ed1i v\u1edbi ng\u01b0\u1eddi m\u1edbi h\u1ecdc. Python c\u00f3 c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u nh\u01b0 <code>int<\/code>, <code>float<\/code>, <code>string<\/code>, <code>list<\/code>, <code>tuple<\/code>, v.v. M\u1ed7i ki\u1ec3u d\u1eef li\u1ec7u c\u00f3 c\u00e1ch s\u1eed d\u1ee5ng ri\u00eang, v\u00ec v\u1eady vi\u1ec7c nh\u1ea7m l\u1eabn gi\u1eefa ch\u00fang c\u00f3 th\u1ec3 g\u00e2y ra l\u1ed7i kh\u00f4ng mong mu\u1ed1n.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 sai:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>x = \"Hello\" + 5<br><\/code><\/pre>\n\n\n\n<p><strong>C\u00e1ch s\u1eeda:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>x = \"Hello\" + str(5)<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6. <strong>Kh\u00f4ng Hi\u1ec3u R\u00f5 S\u1ef1 Kh\u00e1c Bi\u1ec7t Gi\u1eefa <code>is<\/code> v\u00e0 <code>==<\/code><\/strong><\/h4>\n\n\n\n<p>Ng\u01b0\u1eddi m\u1edbi h\u1ecdc Python \u0111\u00f4i khi hay nh\u1ea7m l\u1eabn gi\u1eefa <code>is<\/code> v\u00e0 <code>==<\/code>. D\u1ea5u <code>==<\/code> d\u00f9ng \u0111\u1ec3 so s\u00e1nh gi\u00e1 tr\u1ecb, trong khi <code>is<\/code> d\u00f9ng \u0111\u1ec3 so s\u00e1nh \u0111\u1ecba ch\u1ec9 b\u1ed9 nh\u1edb c\u1ee7a c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 sai:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>a = [1, 2, 3]<br>b = [1, 2, 3]<br>print(a is b)  # False nh\u01b0ng ng\u01b0\u1eddi m\u1edbi h\u1ecdc s\u1ebd k\u1ef3 v\u1ecdng True<br><\/code><\/pre>\n\n\n\n<p><strong>C\u00e1ch s\u1eeda:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>print(a == b)  # True<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">7. <strong>S\u1eed D\u1ee5ng L\u1eb7p V\u00f2ng (Loop) Kh\u00f4ng Ch\u00ednh X\u00e1c<\/strong><\/h4>\n\n\n\n<p>Khi s\u1eed d\u1ee5ng v\u00f2ng l\u1eb7p <code>for<\/code> ho\u1eb7c <code>while<\/code>, m\u1ed9t s\u1ed1 ng\u01b0\u1eddi m\u1edbi c\u00f3 th\u1ec3 qu\u00ean c\u1eadp nh\u1eadt \u0111i\u1ec1u ki\u1ec7n v\u00f2ng l\u1eb7p ho\u1eb7c l\u1eb7p v\u00f4 h\u1ea1n.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 sai:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>while x &gt; 0:<br>    print(x)<br><\/code><\/pre>\n\n\n\n<p><strong>C\u00e1ch s\u1eeda:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>while x &gt; 0:\n    print(x)\n    x -= 1\n<\/code><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">8. <strong>Kh\u00f4ng Hi\u1ec3u L\u1ed7i TypeError ho\u1eb7c ValueError<\/strong><\/h4>\n\n\n\n<p>Khi l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c ph\u00e9p to\u00e1n ho\u1eb7c chuy\u1ec3n \u0111\u1ed5i ki\u1ec3u d\u1eef li\u1ec7u, ng\u01b0\u1eddi m\u1edbi h\u1ecdc Python th\u01b0\u1eddng g\u1eb7p ph\u1ea3i c\u00e1c l\u1ed7i nh\u01b0 <code>TypeError<\/code> ho\u1eb7c <code>ValueError<\/code>. Nh\u1eefng l\u1ed7i n\u00e0y th\u01b0\u1eddng x\u1ea3y ra khi c\u1ed1 g\u1eafng th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n gi\u1eefa c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u kh\u00f4ng t\u01b0\u01a1ng th\u00edch.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 sai:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>x = \"10\" + 5<br><\/code><\/pre>\n\n\n\n<p><strong>C\u00e1ch s\u1eeda:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>x = int(\"10\") + 5<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">K\u1ebft Lu\u1eadn<\/h3>\n\n\n\n<p>H\u1ecdc Python c\u00f3 th\u1ec3 g\u1eb7p ph\u1ea3i nhi\u1ec1u th\u1eed th\u00e1ch, nh\u01b0ng n\u1ebfu b\u1ea1n n\u1eafm \u0111\u01b0\u1ee3c nh\u1eefng l\u1ed7i c\u01a1 b\u1ea3n n\u00e0y, vi\u1ec7c l\u1eadp tr\u00ecnh s\u1ebd tr\u1edf n\u00ean d\u1ec5 d\u00e0ng h\u01a1n r\u1ea5t nhi\u1ec1u. Luy\u1ec7n t\u1eadp th\u01b0\u1eddng xuy\u00ean v\u00e0 ki\u1ec3m tra k\u1ef9 m\u00e3 ngu\u1ed3n s\u1ebd gi\u00fap b\u1ea1n tr\u00e1nh \u0111\u01b0\u1ee3c c\u00e1c l\u1ed7i kh\u00f4ng \u0111\u00e1ng c\u00f3. Ch\u00fac b\u1ea1n h\u1ecdc Python th\u00e0nh c\u00f4ng!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<p>Hi v\u1ecdng b\u00e0i vi\u1ebft n\u00e0y s\u1ebd gi\u00fap b\u1ea1n tr\u00e1nh \u0111\u01b0\u1ee3c nh\u1eefng l\u1ed7i sai ph\u1ed5 bi\u1ebfn khi h\u1ecdc Python. Ch\u00fac b\u1ea1n h\u1ecdc t\u1ed1t!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh ph\u1ed5 bi\u1ebfn v\u00e0 d\u1ec5 h\u1ecdc, nh\u01b0ng ngay c\u1ea3 nh\u1eefng ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u c\u0169ng th\u01b0\u1eddng g\u1eb7p ph\u1ea3i m\u1ed9t s\u1ed1 l\u1ed7i ph\u1ed5 bi\u1ebfn trong qu\u00e1 tr\u00ecnh&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-155","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/155","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=155"}],"version-history":[{"count":2,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":159,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/155\/revisions\/159"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}