Special Ways to Execute Python in this Book
This book provides two additional ways to execute Python programs. Both techniques are designed to assist you as you learn the Python programming language. They will help you increase your understanding of how Python programs work.
Quyển sách này cung cấp cách để thực hành chương trình Python. Cả 2 kỹ thuật đều được thiết kế để giúp bạn vì bạn học ngôn ngữ lập trình Python. Chúng sẽ giúp bạn cải thiện sự hiểu biết của bạn về cách làm việc của các chương trình Python.
First, you can write, modify, and execute programs using a unique activecode interpreter that allows you to execute Python code right in the text itself (right from the web browser). Although this is certainly not the way real programs are written, it provides an excellent environment for learning a programming language like Python since you can experiment with the language as you are reading.
Đầu tiên, bạn có thể viết, chỉnh sửa và thực hành sử dụng một mã kích hoạt thông dịch viên duy nhất mà cho phép bạn thi hành dòng mã Python phía bên phải của của nó (phía bên phải từ trình duyệt web). Mặc dù diều này không thực sự là cách mà chương trình được viết, nó cung cấp một môi trường tuyệt hảo cho học một ngôn ngữ như Python từ khi bạn có thể thì nghiệm nó như bạn đang đọc.
Take a look at the activecode interpreter in action. If we use the Python code from the previous example and make it active, you will see that it can be executed directly by pressing the run button. Try pressing the run button below.
tập trung vào trình dịch viên mã kích hoạt trong hoạt động. Nếu chúng ta sử dụng mã Python từ ví dụ được cung cấp và kích hoạt chúng hoạt động, bạn sẽ thấy rằng chúng có thể được thực thi ngay lập tức bằng cách bấm nút chạy. Thử ấn nút chạy bên dưới.
1
2
3
print("My first program adds two numbers, 2 and 3:")
print(2 + 3)
(ch01_1)
Run
Save Load
Now try modifying the activecode program shown above. First, modify the string in the first print statement by changing the word adds to the word multiplies. Now press run. You can see that the result of the program has changed. However, it still prints “5” as the answer. Modify the second print statement by changing the addition symbol, the “+”, to the multiplication symbol, “*”. Press run to see the new results.
Bay giờ thử chỉnh sửa cái chương trình mã đã được hiển thị. Đầu tiên, chỉnh sửa chỗi trong lần in đầu tiên bằng cách thay đổi từ khóa "adds" thành từ khóa "multiplies". Giờ ấn chạy. Bạn có thể thấy kết quả của chương trình đã được tha yđổi. Tuy nhiên, nó vẫn in câu trả lời là "5". Chỉnh sửa vị trí in lần thứ 2 bằng cách thay đổi dấu, "+" cho các dấu, "*". bám chạy và xem tiếp kết quả.
You can also make changes and save them for reloading later. Save and Load allow you to keep one copy of the program you are working on. For example, press the Save button now. You have just saved the current contents of the activecode window. Now make a few changes and press the Run button. You have changed the current program. Press Load to return your program to its previously saved state.
Bạn cũng có thể làm những thay đổi khác và lưu chúng lại cho lần hoạt động sau. Lưu và nạp cho phép bạn có thể giữ 1 bản sao của chương trình mà bạn đang làm. Ví dụ, bấm vào nút Save bây giờ, bạn sẽ lưu trữ nội dung hiện tại của cửa sổ mã kích hoạt. bây giờ hãy làm một vài thay đổi và bấm nút chạy. Mày đã thay đổi chương trình hiện tại. Bấn nút nạp để trở lại trương trình trước đó mà mình đã lưu.
In addition to activecode, you can also execute Python code with the assistance of a unique visualization tool. This tool, known as codelens, allows you to control the step by step execution of a program. It also lets you see the values of all variables as they are created and modified. The following example shows codelens in action on the same program as we saw above. Note that in activecode, the source code executes from beginning to end and you can see the final result. In codelens you can see and control the step by step progress. Note that the red arrow always points to the next line of code that is going to be executed. The light green arrow points to the line that was just executed.
Ngoài activecode, bạn cũng có thể thực thi mã Python với sự trợ giúp của một công cụ trực quan duy nhất. Công cụ này, được gọi là codelens, cho phép bạn kiểm soát từng bước thực hiện một chương trình. Nó cũng cho phép bạn xem các giá trị của tất cả các biến khi chúng được tạo và sửa đổi. Ví dụ sau cho thấy codelens đang hoạt động trên cùng một chương trình như chúng ta đã thấy ở trên. Lưu ý rằng trong activecode, mã nguồn thực thi từ đầu đến cuối và bạn có thể thấy kết quả cuối cùng. Trong codelens bạn có thể xem và kiểm soát từng bước tiến bộ. Lưu ý rằng mũi tên màu đỏ luôn trỏ đến dòng tiếp theo của mã sẽ được thực hiện. Mũi tên màu xanh nhạt chỉ vào dòng đã được thực thi.
1
print("My first program adds two numbers, 2 and 3:")
2
print(2 + 3)
<< First < Back Step 1 of 2 Forward > Last >>
line that has just executed
next line to execute
Frames
Objects
(firstexample)
The examples in this book use a mixture of the standard Python interpreter, source code, activecode, and codelens. You will be able to tell which is which by looking for either the Python prompt in the case of a shell mode program, the run button for the activecode, or the forward/backward buttons for codelens.
Các ví dụ trong cuốn sách này sử dụng một hỗn hợp của thông dịch Python tiêu chuẩn, mã nguồn, activecode, và codelens. Bạn sẽ có thể cho biết đó là bằng cách tìm kiếm dấu nhắc Python trong trường hợp của một chương trình chế độ trình bao, nút chạy cho activecode, hoặc các nút chuyển tiếp / lùi cho các codelens.
Check your understanding
intr-6: The activecode interpreter allows you to (select all that apply):
a) save programs and reload saved programs.
b) type in Python source code.
c) execute Python code right in the text itself within the web browser.
d) receive a yes/no answer about whether your code is correct or not.
intr-7: Codelens allows you to (select all that apply):
a) measure the speed of a program’s execution.
b) control the step by step execution of a program.
c) write and execute your own Python code.
d) execute the Python code that is in codelens.
References:
http://interactivepython.org/runestone/static/thinkcspy/index.html
Nhận xét
Đăng nhận xét