2. Chương trình Python đầu tiên
2.1 Ngôn ngữ lập trình Python
Python là ngôn ngữ lập trình phổ biến, dễ học, dễ viết, được nhiều người sử dụng.
Bạn có thể dùng Python để làm ra nhiều thứ: tạo ra website, phân tích dữ liệu, làm game, và thậm chí là lập trình trí tuệ nhân tạo (AI).
Python là mã nguồn mở, nên bạn có thể sử dụng hoàn toàn miễn phí.
2.2 Lập trình với Python
Để lập trình với Python, bạn cần 2 thứ:
- Một là Trình viết mã
- Hai là Trình thông dịch
Trình viết mã
Trình viết mã là một chương trình, bạn sử dụng nó để viết (gõ) các câu lệnh, sau đó đưa câu lệnh này cho máy tính thực thi.
Một số trình viết mã bạn có thể sử dụng:
- Chương trình cửa sổ dòng lệnh của Windows (CMD)
- IDLE của Python
- Notepad của Windows
- VS Code của Microsoft
Trình thông dịch
Trình thông dịch là một chương trình, bạn sử dụng nó để dịch các câu lệnh (mà bạn đã soạn trong Trình viết mã) giúp máy tính có thể hiểu và thực thi.
Trong lập trình Python, trình thông dịch chính là chương trình Python.exe.
2.3 Tải và cài đặt Trình thông dịch Python
Chúng ta sẽ tải bản cài đặt Trình thông dịch Python cho máy tính đang chạy Windows:
Tải Trình thông dịch
Để tải Trình thông dịch, làm theo các bước sau:
- Truy cập trang web: https://www.python.org/downloads/
- Nhấp vào nút "Download Python …" để tải chương trình cài đặt về máy tính của bạn
Cài đặt Trình thông dịch
Để cài đặt Trình thông dịch, làm theo các bước sau:
- Nhấp đúp chuột vào tập tin vừa tải về
- Thực hiện theo các hướng dẫn của quá trình cài đặt
- Chờ đợi cho tới khi quá trình cài đặt hoàn tất
2.4 Cách kiểm tra máy tính đã cài đặt Trình thông dịch Python hay chưa
Thực tế, trên nhiều máy tính đã được cài đặt sẵn Trình thông dịch Python, do vậy, bạn có thể kiểm tra. Nếu máy tính đã có Trình thông dịch rồi, thì không cần cài đặt nữa.
Hoặc, sau khi cài đặt xong Trình thông dịch Python, bạn cũng cần phải kiểm tra xem quá trình cài đặt có thành công hay không?
Để kiểm tra, làm theo các bước sau:
- Mở cửa sổ Command Prompt (CMD) hoặc Windows PowerShell. (Gõ "CMD" hoặc "PowerShell" vào thanh tìm kiếm của Windows).
- Gõ lệnh sau và nhấn Enter:
python --version
- Nếu Trình thông dịch Python được cài đặt đúng, màn hình sẽ hiển thị phiên bản của Trình thông dịch Python (ví dụ: Python 3.14.0).
C:\Users\Teo>python --version
Python 3.14.0
2.5 Chương trình "Hello World"
"Hello World" là chương trình đầu tiên và cơ bản nhất khi bạn bắt đầu học một ngôn ngôn ngữ lập trình.
Như ở phần trước đã đề cập, để viết và chạy một chương trình Python, bạn cần có 2 thứ là Trình viết mã và Trình thông dịch.
Trong phần này, chúng ta sẽ thực hiện viết chương trình Python đầu tiên bằng một số Trình viết mã khác nhau, sau đó gửi cho Trình thông dịch để dịch và thực thi.
Cách 1: Dùng chương trình cửa sổ dòng lệnh của Windows (CMD)
Cách này giúp bạn chạy lệnh ngay lập tức, không cần lưu mã nguồn thành tập tin.
Mở Trình viết mã:
- Mở Command Prompt (CMD) hoặc PowerShell
- Gõ lệnh sau và nhấn Enter:
python
- Màn hình sẽ hiển thị dấu: >>> (đây là dấu nhắc lệnh của Trình thông dịch Python)
Viết mã (lập trình):
- Gõ lệnh Python sau và nhấn Enter:
print("Hello World!")
Chạy và hiển thị kết quả:
- Ngay lập tức, dòng chữ "Hello World!" sẽ xuất hiện trên màn hình
- Để thoát khỏi môi trường lập trình này, gõ lệnh exit() và nhấn Enter
Xem minh họa:
C:\Users\VIET HOANG - VTS>python
Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> exit()
2.6 Bài tập và câu hỏi
Bài tập 2a. Bạn hãy viết chương trình Python bằng cửa sổ dòng lệnh (CMD) để xuất các nội dung sau:
- Họ tên của bạn
- Học bài bản - Làm chuyên nghiệp - Sống hạnh phúc
- Bạn nên uống đủ nước mỗi ngày, để có một cơ thể khỏe mạnh
Gợi ý làm bài tập:
Dùng lệnh:
print("Nội dung cần xuất ra màn hình")
Câu hỏi 2.1 Về ngôn ngữ Python. Phát biểu nào không đúng
A. Python là ngôn ngữ lập trình phổ biến, dễ học, dễ viết, được nhiều người sử dụng.
B. Bạn có thể dùng Python để làm ra nhiều thứ: tạo ra website, phân tích dữ liệu, làm game, và thậm chí là lập trình trí tuệ nhân tạo (AI).
C. Python là mã nguồn mở, nên bạn có thể sử dụng hoàn toàn miễn phí.
D. Python là mã nguồn đóng, có bản quyền, nên bạn phải trả tiền để có thể sử dụng.
Câu hỏi 2.2 Để lập trình với Python, bạn cần chuẩn bị những thứ sau. Phát biểu nào không đúng?
A. Trình viết mã
B. Kiến thức về ngôn ngữ Python
C. Trình thông dịch
D. Bản quyền để sử dụng Python
Câu hỏi 2.3 Bạn có thể sử dụng các Trình viết mã sau để lập trình Python. Phát biểu nào không đúng?
A. Chương trình Microsoft Word
B. Chương trình cửa sổ dòng lệnh (CMD)
C. Chương trình Notepad
D. Chương trình IDLE
2. The Your First Program
2.1 What is Python
Python is a very popular programming language. It's easy to learn and easy to write, which is why so many people use it.
You can use Python to build many things: creating websites, analyzing data, making games, and even programming Artificial Intelligence (AI).
Python is open-source, which means you can use it totally for free.
2.2 Programming with Python
To program with Python, you need two main things:
[1] A Code Editor
[2] An Interpreter
Code Editor
A Code Editor is a program you use to write (type) the instructions (called "code") that you want the computer to follow.
Some Code Editors you can use are:
- Windows Command Prompt (CMD) window
- Python's IDLE
- Windows Notepad
- Microsoft's VS Code
Interpreter
The Interpreter is a program that translates the instructions (the code you wrote in the Code Editor) so the computer can understand and execute them.
In Python programming, the interpreter is the actual Python.exe program.
2.3 Python Install
We will download the Python for computers running Windows:
Download Python
Follow these steps to download the Python:
- Go to the website: https://www.python.org/downloads/
- Click the "Download Python …" button to download the installer file to your computer.
Install the Python
Follow these steps to install the Python:
- Double-click the file you just downloaded
- Follow the instructions in the setup process
- Wait until the installation process is complete.
2.4 Check if Python is Installed
Actually, Python may already be installed on many computers, so you should check first. If the Python is already there, you don't need to install it again! Or, even after installing it, you need to check if the installation was successful.
To check, follow these steps:
- Open the Command Prompt (CMD) or Windows PowerShell window. (Type "CMD" or "PowerShell" into the Windows search bar).
- Type the following command and press Enter:
python --version
- If the Python is installed correctly, the screen will show its version (for example: Python 3.14.0).
C:\Users\Teo>python --version
Python 3.14.0
2.5 The "Hello World" Program
"Hello World" is the first and most basic program you write when learning any programming language.
As mentioned before, to write and run a Python program, you need a Code Editor and an Interpreter.
In this section, we will write our first Python program using one type of Code Editor, and then send it to the Interpreter to translate and execute.
Method 1: Using the Windows Command Line (CMD)
This way lets you run commands instantly without saving the code in a file.
Open the Code Editor:
- Open Command Prompt (CMD) or PowerShell.
- Type the following command and press Enter:
python
- The screen will show the >>> symbol (this is the command prompt of the Python Interpreter).
Write the Code (Program):
Type the following Python command and press Enter:
print("Hello World!")
Run and Display the Result:
- Immediately, the text "Hello World!" will appear on the screen.
- To exit this programming environment, type the command exit() and press Enter.
Example:
C:\Users\VIET HOANG - VTS>python
Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> exit()
2.6 Exercises and Questions
Exercise 2a. Write a Python program using the command line (CMD) to output the following text:
- Your full name
- Study properly – Work professionally – Live happily
- You should drink enough water every day to have a healthy body
Hint: Use the command: print("Your text goes here")
Question 2.1 About the Python language. Which statement is incorrect?
A. Python is a popular, easy-to-learn, easy-to-write programming language used by many people.
B. You can use Python to build many things: create websites, analyze data, make games, and even program Artificial Intelligence (AI).
C. Python is open-source, so you can use it completely for free.
D. Python is closed-source, copyrighted, so you have to pay to use it.
Question 2.2 To program with Python, you need to prepare the following things. Which statement is incorrect?
A. Code Editor
B. Knowledge of the Python language
C. Interpreter
D. Copyright to use Python
Question 2.3 You can use the following Code Editors for Python programming. Which statement is incorrect?
A. Microsoft Word program
B. Command Prompt (CMD) window program
C. Notepad program
D. IDLE program