Hướng dẫn dùng integar python

Nội dung chính

Nội dung chính

  • Hàm int() trong python là gì
  • Cú pháp hàm int() trong Python
  • Chương trình mẫu sử dụng hàm int() trong Python
  • Lưu ý khi sử dụng hàm int() trong python
  • Tổng kết và thực hành

Nội dung chính

  • Hàm int() trong python là gì
  • Cú pháp hàm int() trong Python
  • Chương trình mẫu sử dụng hàm int() trong Python
  • Lưu ý khi sử dụng hàm int() trong python
  • Tổng kết và thực hành

Nội dung chính

  • Hàm int() trong python là gì
  • Cú pháp hàm int() trong Python
  • Chương trình mẫu sử dụng hàm int() trong Python
  • Lưu ý khi sử dụng hàm int() trong python
  • Tổng kết và thực hành

Nội dung chính

  • Hàm int() trong python là gì
  • Cú pháp hàm int() trong Python
  • Chương trình mẫu sử dụng hàm int() trong Python
  • Lưu ý khi sử dụng hàm int() trong python
  • Tổng kết và thực hành

Nội dung chính

  • Hàm int() trong python là gì
  • Cú pháp hàm int() trong Python
  • Chương trình mẫu sử dụng hàm int() trong Python
  • Lưu ý khi sử dụng hàm int() trong python
  • Tổng kết và thực hành
  • Hàm int() trong python là gì
  • Cú pháp hàm int() trong Python
  • Chương trình mẫu sử dụng hàm int() trong Python
  • Lưu ý khi sử dụng hàm int() trong python
  • Tổng kết và thực hành

Hướng dẫn sử dụng hàm int() trong python. Bạn sẽ học được cách Chuyển từ string sang int trong Python bằng hàm int() sau bài học này.

Hàm int() trong python là gì

int() trong python là hàm dùng để Chuyển từ string sang int trong Python.

Chúng ta sử dụng hàm int() trong python để chuyển chuỗi thành số trong Python và tạo ra một số mới với cú pháp sau đây:

Cú pháp hàm int() trong Python

int(x)
Hoặc
int(x, base=y)

Trong đó

  • int là tên hàm số
  • x là số, chữ số hoặc chuỗi chữ số ở dạng str cần chuyển sang int.
  • base là để chỉ định hệ cơ số y của x. Giá trị của y có thể là là hệ cơ số 2, cơ số 10, thập lục phân v.v..

Chương trình mẫu sử dụng hàm int() trong Python

Chúng ta có thể chỉ định các đối số x, y để sử dụng hàm int() trong Python như sau:

Nếu x được lược bỏ, kết quả trả về bằng 0.

int()
>> 0

Nếu x là số nguyên, kết quả trả về chính bằng x.

int(15)
>> 15

Nếu x là số thập phân, kết quả trả về là số nguyên được làm tròn của x.

int(3.14)
>> 3

Nếu x ở dạng hệ cơ số được chỉ định ở y, kết quả trả về là số ở hệ thập phân của x.

int("1011", 2)
>> 11

int("3F", 16)
>> 63

Nếu x là một chuỗi ký tự, kết quả trả về là số ở dạng int của ký tự đó:

int('28')
>> 28

int("28", 10)
>> 28

int("1011", 2)
>> 11

int("3F", 16)
>> 63

Ngoài ra, chúng ta có thể lược bỏ base và chỉ chỉ định hệ cơ số y trong biểu thức như sau:

int("24", base=8)
>> 20

int("24",8)
>> 20

Lưu ý khi sử dụng hàm int() trong python

Chúng ta chỉ sử dụng hàm int() để Chuyển từ string sang int trong Python với các string là số, chữ số hoặc chuỗi chữ số, là các dữ liệu có khả năng chuyển sang int.
Nếu string được chỉ định không có khả năng chuyển sang int thì lỗi ValueError sẽ xảy ra như ví dụ sau đây:

int("good game")

Lỗi ValueError trả về:

Traceback (most recent call last):
File "Main.py", line 1, in <module>
int("good game")
ValueError: invalid literal for int() with base 10: 'good game'

Do chuỗi "good game" không có khả năng chuyển sang int nên lỗi đã xảy ra.

Tổng kết và thực hành

Trên đây Kiyoshi đã hướng dẫn bạn cách sử dụng hàm int() để Chuyển từ string sang int trong Python rồi. Để nắm rõ nội dung bài học hơn, bạn hãy thực hành viết lại các ví dụ của ngày hôm nay nhé.

Và hãy cùng tìm hiểu những kiến thức căn bản về python trong các bài học tiếp theo.

Viết bởi Kiyoshi. Đã đăng ký bản quyền tác giả tại &lt;a title="Bạn được tự do chia sẻ bài viết nhưng phải để lại đường link bài viết từ laptrinhcanban.com. Bạn không được sử dụng tài liệu cho mục đích thương mại. Không được phép chỉnh sửa nội dung được phát hành trên website của chúng tôi" style="color:#fff;background-color:silver" rel="license noopener" target="_blank" href="https://creativecommons.org/licenses/by-nc-nd/4.0/"&gt;Creativecommons&lt;/a&gt;&amp;nbsp;và &lt;a title="Bạn được tự do chia sẻ bài viết nhưng phải để lại đường link bài viết từ laptrinhcanban.com. Bạn không được sử dụng tài liệu cho mục đích thương mại. Không được phép chỉnh sửa nội dung được phát hành trên website của chúng tôi" style="color:#fff;background-color:silver" target="_blank" rel="noopener" href="https://www.dmca.com/Protection/Status.aspx?ID=1631afcd-7c4a-467d-8016-402c5073e5cd" class="dmca-badge"&gt;DMCA&lt;/a&gt;&lt;script src="https://images.dmca.com/Badges/DMCABadgeHelper.min.js"&gt;</p><h3 style="font-size:15px">Bài viết liên quan</h3><ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/float-trong-python/" title="Hàm float() trong Python" rel="bookmark">Hàm float() trong Python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/ham-trong-python-la-gi/" title="Hàm trong Python là gì - Các hàm trong Python" rel="bookmark">Hàm trong Python là gì - Các hàm trong Python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/ham-def-trong-python/" title="Def trong python và cú pháp tạo hàm" rel="bookmark">Def trong python và cú pháp tạo hàm</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/len-trong-python/" title="Hàm len() trong Python" rel="bookmark">Hàm len() trong Python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/range-trong-python/" title="Hàm range() trong Python" rel="bookmark">Hàm range() trong Python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/return-trong-python/" title="Return trong python và giá trị trả về" rel="bookmark">Return trong python và giá trị trả về</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/str-trong-python/" title="Hàm str() trong python" rel="bookmark">Hàm str() trong python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/bien-trong-python/bien-trong-python-la-gi/" title="Biến trong python là gì" rel="bookmark">Biến trong python là gì</a></h3></div></li></ul></div><div id="keugoi"><span>Hãy chia sẻ và cùng lan tỏa kiến thức lập trình Nhật Bản tại Việt Nam!</span></div><section class="icon-bar-2" style="border-bottom:dotted grey .3px!important"><a target="_blank" title="chia sẻ qua facebook" rel="noopener noreferrer" href="https://www.facebook.com/sharer/sharer.php?u=https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/int-trong-python/" class="facebook"><i class="fa fa-facebook"></i></a> <a target="_blank" rel="noopener noreferrer" href="https://twitter.com/intent/tweet?url=https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/int-trong-python/" title="chia sẻ qua twitter" class="twitter"><i class="fa fa-twitter"></i></a> <a target="_blank" rel="noopener noreferrer" href="https://getpocket.com/edit?url=https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/int-trong-python/" title="chia sẻ qua pocket" class="pocket"><i class="fa fa-get-pocket"></i></a> <span class="zalo-share-button" title="chia sẻ qua zalo" style="margin-left:18px;margin-bottom:-3px" data-href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/int-trong-python/" data-oaid="579745863508352884" data-layout="3" data-color="blue" data-customize="false"></span></section><p class="ico-folder"><a target="_blank" href="https://laptrinhcanban.com/"><span itemprop="HOME">HOME</span></a><span class="sya"></span>&gt;&gt; <a target="_blank" class="article-category-link" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/">python cơ bản - lập trình python cho người mới bắt đầu</a>&gt;&gt;<a target="_blank" class="article-category-link" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/">14. hàm trong python</a></p><nav id="article-nav"><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/str-trong-python/" id="article-nav-older" class="article-nav-link-wrap" style="float:left;text-align:right;padding-right:20px"><strong class="article-nav-caption">Bài sau</strong><div class="article-nav-title">Hàm str() trong python</div></a><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/ham-trong-python/float-trong-python/" id="article-nav-newer" class="article-nav-link-wrap" style="float:right;text-align:left;padding-left:20px"><strong class="article-nav-caption">Bài tiếp</strong><div class="article-nav-title">Hàm float() trong Python</div></a><span id="ezoic-pub-ad-placeholder-611" class="ezoic-adpicker-ad"></span></nav><div id="recent_posts_down"><div class="widget_athemes_tabs"><ul id="widget-tab" class="clearfix widget-tab-nav"><li class="active">Bài viết mới nhất</li></ul><div class="widget"><ul><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/math-object-trong-javascript/lam-tron-so-trong-javascript/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban46.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/math-object-trong-javascript/lam-tron-so-trong-javascript/">Làm tròn số trong JavaScript (Math.round, Math.ceil, Math.floor)</a></span> <span>tháng 7 1, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/math-object-trong-javascript/so-mu-trong-javascript/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban45.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/math-object-trong-javascript/so-mu-trong-javascript/">Số mũ trong JavaScript (Math.pow, Math.exp)</a></span> <span>tháng 7 1, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/math-object-trong-javascript/math-max-math-min-trong-javascript/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban44.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/math-object-trong-javascript/math-max-math-min-trong-javascript/">Math.max và Math.min trong JavaScript</a></span> <span>tháng 7 1, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/math-object-trong-javascript/math-random-trong-javascript/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban43.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/math-object-trong-javascript/math-random-trong-javascript/">Math.random trong JavaScript</a></span> <span>tháng 7 1, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/xu-ly-ngoai-le-trong-javascript/throw-trong-javascript/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban42.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/xu-ly-ngoai-le-trong-javascript/throw-trong-javascript/">throw trong JavaScript và cách trả về ngoại lệ tuỳ ý</a></span> <span>tháng 7 1, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/xu-ly-ngoai-le-trong-javascript/cac-ngoai-le-trong-javascript-va-cach-xu-ly/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban41.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/xu-ly-ngoai-le-trong-javascript/cac-ngoai-le-trong-javascript-va-cach-xu-ly/">Các ngoại lệ trong JavaScript và cách xử lý tương ứng</a></span> <span>tháng 7 1, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/xu-ly-ngoai-le-trong-javascript/try-catch-trong-javascript/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban40.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/xu-ly-ngoai-le-trong-javascript/try-catch-trong-javascript/">try...catch trong JavaScript và cách xử lý ngoại lệ</a></span> <span>tháng 7 1, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/xu-ly-ngoai-le-trong-javascript/xu-ly-ngoai-le-cua-ham-xu-ly-mo-ta-trong-khoi-try/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban39.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/javascript/javascript-co-ban-den-nang-cao/xu-ly-ngoai-le-trong-javascript/xu-ly-ngoai-le-cua-ham-xu-ly-mo-ta-trong-khoi-try/">Xử lý ngoại lệ của hàm xử lý mô tả trong khối try</a></span> <span>tháng 7 1, 2022</span></div></li></ul></div></div></div><footer class="entry-meta entry-footer"></footer><hr class="entry-footer-hr"></div><div id="profiletitle2">Profile</div><div id="profileblock2"><div id="profilephoto2"><a target="_blank" rel="noopener" href="https://www.facebook.com/mr.nchita"><img width="100" height="100" alt="きよしです!笑" ezimgfmt="rs rscb2 src ng ngcb2" class="ezlazyload" data-ezsrc="/css/images/kiyoshi.webp"></a></div><div id="profiletext2"><p>Tác giả : <a href="https://www.facebook.com/mr.nchita" target="_blank" rel="nofollow noopener">Kiyoshi (Chis Thanh)</a></p><p>Kiyoshi là một <a href="https://www.youtube.com/c/ChisThanh" target="_blank" rel="nofollow noopener">cựu du học sinh tại Nhật Bản</a>. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.<span id="ezoic-pub-ad-placeholder-196" class="ezoic-adpicker-ad"></span></p></div></div></div></article><aside id="sidebar"><div id="sidebar_first"><div class="search" style="margin-top:-30px"><div id="fb-root"></div><script async defer crossorigin="anonymous" src="https://connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v12.0&appId=3084959888222500&autoLogAppEvents=1" nonce="KPkvxeh4">

Hướng dẫn dùng integar python