The Python Programming Language
The Python Programming Language
The programming language you will be learning is Python. Python is an example of a high-level language; other high-level languages you might have heard of are C++, PHP, and Java.
As you might infer from the name high-level language, there are also low-level languages, sometimes referred to as machine languages or assembly languages. Loosely speaking, computers can only execute programs written in low-level languages. Thus, programs written in a high-level language have to be processed before they can run. This extra processing takes some time, which is a small disadvantage of high-level languages. However, the advantages to high-level languages are enormous.
Ngôn ngữ lập trình mà bạn sẽ được học là Python. Python là một ví dụ của ngôn ngữ bậc cao; những ngôn ngữ bậc cao khác mình có thể nghe như C++, PHP, và Java. Như bạn có thể suy luận từ cái tên của ngôn ngữ bậc cao, ở đây cũng có những ngôn ngữ bậc thấp, đôi khi có thể biết tới như các ngôn ngữ máy móc hoặc ngôn ngữ lắp ráp. Do đó, chương trình được viết trong ngôn ngữ bậc cao cần phải được xử lý trước khi chúng có thể chạy. Quá trình thêm này có thể chút thời gian, đây là một bất lợi nhỏ của các ngôn ngữ bậc cao. Tuy nhiên, lợi thế của ngôn ngữ bậc cao là rất lớn.
First, it is much easier to program in a high-level language. Programs written in a high-level language take less time to write, they are shorter and easier to read, and they are more likely to be correct. Second, high-level languages are portable, meaning that they can run on different kinds of computers with few or no modifications. Low-level programs can run on only one kind of computer and have to be rewritten to run on another.
Đầu tiên, rất dễ dàng để làm một chương trình tại một ngôn ngữ bậc cao. Chương trình được viết bởi một ngôn ngữ bậc cao chiếm ít thời gian, chúng có thể ngắn hơn và dễ đọc hơn, và chúng càng dễ chính xác hơn. Thứ 2, các ngôn ngữ bậc cao là di động, điều ngày nghĩa là chúng có thể chạy ở nhiều loại máy tính với chút hoặc không có chỉnh sửa. Các ngôn ngữ bậc thấp có thể chạy tren 1 loại của máy tính và phải viết lại để chạy trên máy khác.
Due to these advantages, almost all programs are written in high-level languages. Low-level languages are used only for a few specialized applications.
Do những lợi thế này, hầu hết tất cả các chương trình được viết thông qua các ngôn ngữ bậc cao. Ngôn ngữ bậc hấp được sử dụng cho một vài chương trình đặc biệt.
Two kinds of programs process high-level languages into low-level languages: interpreters and compilers. An interpreter reads a high-level program and executes it, meaning that it does what the program says. It processes the program a little at a time, alternately reading lines and performing computations.
Hai loại chương trình xử lý ngôn ngữ cấp cao sang các ngôn ngữ bậc thấp: phiên dịch và trình biên dịch. Một thông dịch viên đọc một chương trình cấp cao và thực hiện nó, có nghĩa là nó thực hiện những gì chương trình đó yêu cầu. Nó xử lý chương trình một chút tại một thời điểm, luân phiên đọc các dòng và thực hiện tính toán.
Interpret illustration
A compiler reads the program and translates it completely before the program starts running. In this case, the high-level program is called the source code, and the translated program is called the object code or the executable. Once a program is compiled, you can execute it repeatedly without further translation.
Trình biên dịch sẽ đọc chương trình và dịch nó hoàn toàn trước khi chương trình bắt đầu chạy. Trong trường hợp này, chương trình cao cấp được gọi là mã nguồn, và chương trình dịch được gọi là mã đối tượng hoặc tệp thi hành. Một khi một chương trình được biên dịch, bạn có thể thực hiện nó liên tục mà không cần dịch thêm.
Compile illustration
Many modern languages use both processes. They are first compiled into a lower level language, called byte code, and then interpreted by a program called a virtual machine. Python uses both processes, but because of the way programmers interact with it, it is usually considered an interpreted language.
Nhiều ngôn ngữ hiện đại sử dụng cả hai quy trình. Chúng được biên dịch thành ngôn ngữ cấp thấp hơn, được gọi là byte code, và sau đó giải thích bởi một chương trình được gọi là máy ảo. Python sử dụng cả hai quy trình, nhưng do cách lập trình tương tác với nó, nó thường được coi là một ngôn ngữ giải thích
There are two ways to use the Python interpreter: shell mode and program mode. In shell mode, you type Python expressions into the Python shell, and the interpreter immediately shows the result. The example below shows the Python shell at work.
Có hai cách để sử dụng trình thông dịch Python: chế độ bên ngoài và chế độ chương trình. Trong chế độ ngoài chương trình, bạn gõ các biểu thức Python vào trong trình bao Python, và trình thông dịch sẽ hiển thị ngay kết quả. Ví dụ dưới đây cho thấy trình bao Python đang hoạt động.
$ python3
Python 3.2 (r32:88445, Mar 25 2011, 19:28:28)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 3
5
>>>
The >>> is called the Python prompt. The interpreter uses the prompt to indicate that it is ready for instructions. We typed 2 + 3. The interpreter evaluated our expression and replied 5. On the next line it gave a new prompt indicating that it is ready for more input.
>>> được gọi là dấu nhắc Python. Thông dịch viên sử dụng dấu nhắc để chỉ ra rằng nó đã sẵn sàng để được hướng dẫn. Chúng ta đánh máy 2 + 3. Người thông dịch đánh giá "hành động" của chúng ta và trả lời 5. Trên dòng tiếp theo, nó đưa ra một dấu nhắc mới chỉ ra rằng nó đã sẵn sàng cho đầu vào nhiều hơn.
Working directly in the interpreter is convenient for testing short bits of code because you get immediate feedback. Think of it as scratch paper used to help you work out problems.
Alternatively, you can write an entire program by placing lines of Python instructions in a file and then use the interpreter to execute the contents of the file as a whole. Such a file is often referred to as source code. For example, we used a text editor to create a source code file named firstprogram.py with the following contents:
Làm việc trực tiếp thông dịch viên rất tiện lợi cho việc kiểm tra mã ngắn vì bạn nhận được phản hồi ngay lập tức. Hãy suy nghĩ về nó như là một dấu hiệu đầu để giúp bạn giải quyết vấn đề.
Ngoài ra, bạn có thể viết toàn bộ chương trình bằng cách đặt dòng lệnh Python trong một tệp tin và sử dụng trình thông dịch để thực hiện nội dung của tệp như một chuỗi. Một tập tin như vậy thường được gọi là mã nguồn. Ví dụ: chúng ta đã sử dụng trình chỉnh sửa văn bản để tạo tệp mã nguồn có tên firstprogram.py với các nội dung sau:
print("My first program adds two numbers, 2 and 3:")
print(2 + 3)
By convention, files that contain Python programs have names that end with .py . Following this convention will help your operating system and other programs identify a file as containing python code.
Theo quy ước, các tệp chứa các chương trình Python có tên kết thúc bằng .py. Theo quy ước này sẽ giúp hệ điều hành của bạn và các chương trình khác xác định một tệp có chứa mã python.
$ python firstprogram.py
My first program adds two numbers, 2 and 3:
5
These examples show Python being run from a Unix command line. In other development environments, the details of executing programs may differ. Also, most programs are more interesting than this one.
Các ví dụ này cho thấy Python đang chạy từ một dòng lệnh Unix. Trong các môi trường phát triển khác, chi tiết về các chương trình thực hiện có thể khác nhau. Ngoài ra, hầu hết các chương trình thú vị hơn chương trình này.
Check your understanding
intr-3: Source code is another name for:
a) the instructions in a program, stored in a file.
b) the language that you are programming in (e.g., Python).
c) the environment/tool in which you are programming.
d) the number (or “code”) that you must input at the top of each program to tell the computer how to execute your program.
intr-4: What is the difference between a high-level programming language and a low-level programming language?
a) It is high-level if you are standing and low-level if you are sitting.
b) It is high-level if you are programming for a computer and low-level if you are programming for a phone or mobile device.
c) It is high-level if the program must be processed before it can run, and low-level if the computer can execute it without additional processing.
d) It is high-level if it easy to program in and is very short; it is low-level if it is really hard to program in and the programs are really long.
intr-5: Pick the best replacements for 1 and 2 in the following sentence: When comparing compilers and interpreters, a compiler is like 1 while an interpreter is like 2.
a) 1 = a process, 2 = a function
b) 1 = translating an entire book, 2 = translating a line at a time
c) 1 = software, 2 = hardware
d) 1 = object code, 2 = byte code
References:
http://interactivepython.org/runestone/static/thinkcspy/index.html
The programming language you will be learning is Python. Python is an example of a high-level language; other high-level languages you might have heard of are C++, PHP, and Java.
As you might infer from the name high-level language, there are also low-level languages, sometimes referred to as machine languages or assembly languages. Loosely speaking, computers can only execute programs written in low-level languages. Thus, programs written in a high-level language have to be processed before they can run. This extra processing takes some time, which is a small disadvantage of high-level languages. However, the advantages to high-level languages are enormous.
Ngôn ngữ lập trình mà bạn sẽ được học là Python. Python là một ví dụ của ngôn ngữ bậc cao; những ngôn ngữ bậc cao khác mình có thể nghe như C++, PHP, và Java. Như bạn có thể suy luận từ cái tên của ngôn ngữ bậc cao, ở đây cũng có những ngôn ngữ bậc thấp, đôi khi có thể biết tới như các ngôn ngữ máy móc hoặc ngôn ngữ lắp ráp. Do đó, chương trình được viết trong ngôn ngữ bậc cao cần phải được xử lý trước khi chúng có thể chạy. Quá trình thêm này có thể chút thời gian, đây là một bất lợi nhỏ của các ngôn ngữ bậc cao. Tuy nhiên, lợi thế của ngôn ngữ bậc cao là rất lớn.
First, it is much easier to program in a high-level language. Programs written in a high-level language take less time to write, they are shorter and easier to read, and they are more likely to be correct. Second, high-level languages are portable, meaning that they can run on different kinds of computers with few or no modifications. Low-level programs can run on only one kind of computer and have to be rewritten to run on another.
Đầu tiên, rất dễ dàng để làm một chương trình tại một ngôn ngữ bậc cao. Chương trình được viết bởi một ngôn ngữ bậc cao chiếm ít thời gian, chúng có thể ngắn hơn và dễ đọc hơn, và chúng càng dễ chính xác hơn. Thứ 2, các ngôn ngữ bậc cao là di động, điều ngày nghĩa là chúng có thể chạy ở nhiều loại máy tính với chút hoặc không có chỉnh sửa. Các ngôn ngữ bậc thấp có thể chạy tren 1 loại của máy tính và phải viết lại để chạy trên máy khác.
Due to these advantages, almost all programs are written in high-level languages. Low-level languages are used only for a few specialized applications.
Do những lợi thế này, hầu hết tất cả các chương trình được viết thông qua các ngôn ngữ bậc cao. Ngôn ngữ bậc hấp được sử dụng cho một vài chương trình đặc biệt.
Two kinds of programs process high-level languages into low-level languages: interpreters and compilers. An interpreter reads a high-level program and executes it, meaning that it does what the program says. It processes the program a little at a time, alternately reading lines and performing computations.
Hai loại chương trình xử lý ngôn ngữ cấp cao sang các ngôn ngữ bậc thấp: phiên dịch và trình biên dịch. Một thông dịch viên đọc một chương trình cấp cao và thực hiện nó, có nghĩa là nó thực hiện những gì chương trình đó yêu cầu. Nó xử lý chương trình một chút tại một thời điểm, luân phiên đọc các dòng và thực hiện tính toán.
Interpret illustration
A compiler reads the program and translates it completely before the program starts running. In this case, the high-level program is called the source code, and the translated program is called the object code or the executable. Once a program is compiled, you can execute it repeatedly without further translation.
Trình biên dịch sẽ đọc chương trình và dịch nó hoàn toàn trước khi chương trình bắt đầu chạy. Trong trường hợp này, chương trình cao cấp được gọi là mã nguồn, và chương trình dịch được gọi là mã đối tượng hoặc tệp thi hành. Một khi một chương trình được biên dịch, bạn có thể thực hiện nó liên tục mà không cần dịch thêm.
Compile illustration
Many modern languages use both processes. They are first compiled into a lower level language, called byte code, and then interpreted by a program called a virtual machine. Python uses both processes, but because of the way programmers interact with it, it is usually considered an interpreted language.
Nhiều ngôn ngữ hiện đại sử dụng cả hai quy trình. Chúng được biên dịch thành ngôn ngữ cấp thấp hơn, được gọi là byte code, và sau đó giải thích bởi một chương trình được gọi là máy ảo. Python sử dụng cả hai quy trình, nhưng do cách lập trình tương tác với nó, nó thường được coi là một ngôn ngữ giải thích
There are two ways to use the Python interpreter: shell mode and program mode. In shell mode, you type Python expressions into the Python shell, and the interpreter immediately shows the result. The example below shows the Python shell at work.
Có hai cách để sử dụng trình thông dịch Python: chế độ bên ngoài và chế độ chương trình. Trong chế độ ngoài chương trình, bạn gõ các biểu thức Python vào trong trình bao Python, và trình thông dịch sẽ hiển thị ngay kết quả. Ví dụ dưới đây cho thấy trình bao Python đang hoạt động.
$ python3
Python 3.2 (r32:88445, Mar 25 2011, 19:28:28)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 3
5
>>>
The >>> is called the Python prompt. The interpreter uses the prompt to indicate that it is ready for instructions. We typed 2 + 3. The interpreter evaluated our expression and replied 5. On the next line it gave a new prompt indicating that it is ready for more input.
>>> được gọi là dấu nhắc Python. Thông dịch viên sử dụng dấu nhắc để chỉ ra rằng nó đã sẵn sàng để được hướng dẫn. Chúng ta đánh máy 2 + 3. Người thông dịch đánh giá "hành động" của chúng ta và trả lời 5. Trên dòng tiếp theo, nó đưa ra một dấu nhắc mới chỉ ra rằng nó đã sẵn sàng cho đầu vào nhiều hơn.
Working directly in the interpreter is convenient for testing short bits of code because you get immediate feedback. Think of it as scratch paper used to help you work out problems.
Alternatively, you can write an entire program by placing lines of Python instructions in a file and then use the interpreter to execute the contents of the file as a whole. Such a file is often referred to as source code. For example, we used a text editor to create a source code file named firstprogram.py with the following contents:
Làm việc trực tiếp thông dịch viên rất tiện lợi cho việc kiểm tra mã ngắn vì bạn nhận được phản hồi ngay lập tức. Hãy suy nghĩ về nó như là một dấu hiệu đầu để giúp bạn giải quyết vấn đề.
Ngoài ra, bạn có thể viết toàn bộ chương trình bằng cách đặt dòng lệnh Python trong một tệp tin và sử dụng trình thông dịch để thực hiện nội dung của tệp như một chuỗi. Một tập tin như vậy thường được gọi là mã nguồn. Ví dụ: chúng ta đã sử dụng trình chỉnh sửa văn bản để tạo tệp mã nguồn có tên firstprogram.py với các nội dung sau:
print("My first program adds two numbers, 2 and 3:")
print(2 + 3)
By convention, files that contain Python programs have names that end with .py . Following this convention will help your operating system and other programs identify a file as containing python code.
Theo quy ước, các tệp chứa các chương trình Python có tên kết thúc bằng .py. Theo quy ước này sẽ giúp hệ điều hành của bạn và các chương trình khác xác định một tệp có chứa mã python.
$ python firstprogram.py
My first program adds two numbers, 2 and 3:
5
These examples show Python being run from a Unix command line. In other development environments, the details of executing programs may differ. Also, most programs are more interesting than this one.
Các ví dụ này cho thấy Python đang chạy từ một dòng lệnh Unix. Trong các môi trường phát triển khác, chi tiết về các chương trình thực hiện có thể khác nhau. Ngoài ra, hầu hết các chương trình thú vị hơn chương trình này.
Check your understanding
intr-3: Source code is another name for:
a) the instructions in a program, stored in a file.
b) the language that you are programming in (e.g., Python).
c) the environment/tool in which you are programming.
d) the number (or “code”) that you must input at the top of each program to tell the computer how to execute your program.
intr-4: What is the difference between a high-level programming language and a low-level programming language?
a) It is high-level if you are standing and low-level if you are sitting.
b) It is high-level if you are programming for a computer and low-level if you are programming for a phone or mobile device.
c) It is high-level if the program must be processed before it can run, and low-level if the computer can execute it without additional processing.
d) It is high-level if it easy to program in and is very short; it is low-level if it is really hard to program in and the programs are really long.
intr-5: Pick the best replacements for 1 and 2 in the following sentence: When comparing compilers and interpreters, a compiler is like 1 while an interpreter is like 2.
a) 1 = a process, 2 = a function
b) 1 = translating an entire book, 2 = translating a line at a time
c) 1 = software, 2 = hardware
d) 1 = object code, 2 = byte code
References:
http://interactivepython.org/runestone/static/thinkcspy/index.html
Nhận xét
Đăng nhận xét