Cách dùng Python làm máy tính

Bạn cần tính toán nhanh? Python có thể giúp bạn! Dưới đây là cách dùng “máy tính” Python.

Cách dùng Python làm máy tínhCách dùng Python làm máy tính

Mở Python

Cách mở Python tùy thuộc vào hệ điều hành bạn đang dùng. Trong Linux, macOS hay Windows với Windows Subsystem cho Linux, bạn chỉ cần nhập python hoặc python3 vào cửa sổ nhắc lệnh terminal.

Các phép tính toán học

Máy tính PythonMáy tính Python

Khi mở trình thông dịch Python, bạn được đưa tới Python prompt. Nếu quen dùng máy tính bỏ túi, bạn sẽ thấy các phép toán ở đây quen thuộc.

Bạn đang xem: Cách dùng Python làm máy tính

Phép cộng đơn giản:

2 + 2

Tất nhiên, trình thông dịch này sẽ trả kết quả về 4.

Phép trừ cũng tương tự.

4 - 2
2

Bạn cũng có thể thực hiện phép nhân, trong đó dấu nhân là biểu tượng *.

42 * 23
966

Phép chia dùng /. Trong Python 3, kết quả trả về dưới dạng phân số thập phân.

15 / 4
3.75

Số mũ dùng **

7**2
49

Phép toán nâng cao hơn

Giống như các ngôn ngữ lập trình khác, Python dùng các thư viện để mở rộng chức năng và thực hiện phép toán cũng không ngoại lệ. Là một phần của thư viện tiêu chuẩn, Python bao gồm library Toán học.

Để sử dụng nó, chỉ cần nhập dòng sau vào cửa sổ trình thông dịch:

import math

Thư viện toán học cũng có các hàm lượng giác cũng như thông tin về con số. Nó bao gồm cả một số gần đúng của pi.

Xem thêm  Bình giảng bài thơ Viếng lăng Bác

Ví dụ: Hãy dùng phép tính trên Python để chứng minh đặt một chiếc pizza cỡ lớn sẽ tiết kiệm tiền hơn bởi diện tích tăng theo bình phương bán kính của một chiếc pizza tròn. Nhớ rằng, công thức tính diện tích hình tròn là pi nhân với bình phương bán kính hình tròn.

Ví dụ, đây là diện tích của một chiếc pizza 8-inch được tính bằng Python:

math.pi * 4**2
50.26548245743669

Và đối với một chiếc bánh pizza 16 inch:

math.pi * 8**2 
201.06192982974676

Từ kết quả, bạn đã thấy rõ sự khác biệt phải không?

Trên đây là cách làm toán trên Python. Hi vọng bài viết hữu ích với các bạn.

Đăng bởi: BNOK.VN

Chuyên mục: Hướng dẫn giáo viên

16 bình luận về “Cách dùng Python làm máy tính”

  1. Fantastic goods from you, man. I have understand your stuff previous to and you are just too great. I actually like what you’ve acquired here, really like what you’re saying and the way in which you say it. You make it enjoyable and you still take care of to keep it wise. I can’t wait to read much more from you. This is actually a great site.

  2. Thanks a lot for providing individuals with remarkably superb opportunity to discover important secrets from this web site. It is usually so amazing plus stuffed with a good time for me personally and my office co-workers to search the blog at least three times in one week to see the new guides you have got. And of course, we’re always impressed with your great advice served by you. Selected two facts in this article are rather the most beneficial we have all ever had.

  3. There are actually quite a lot of details like that to take into consideration. That may be a nice level to deliver up. I supply the thoughts above as normal inspiration but clearly there are questions just like the one you convey up where crucial thing will be working in sincere good faith. I don?t know if finest practices have emerged around issues like that, however I’m certain that your job is clearly identified as a good game. Each girls and boys really feel the influence of just a second’s pleasure, for the rest of their lives.

  4. Just desire to say your article is as amazing. The clarity for your post is simply nice and i could think you are an expert on this subject. Well together with your permission allow me to seize your RSS feed to keep updated with forthcoming post. Thanks one million and please keep up the enjoyable work.

  5. Hey just wanted to give you a quick heads up and let you know a few of the images aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different web browsers and both show the same outcome.

  6. Undeniably believe that that you stated. Your favorite justification appeared to be at the web the simplest factor to take into accout of. I say to you, I certainly get annoyed while other folks think about concerns that they plainly don’t realize about. You managed to hit the nail upon the highest and outlined out the entire thing without having side-effects , other folks could take a signal. Will probably be again to get more. Thanks

Viết một bình luận