1.4. Executing Python in this BookΒΆ
This book provides two special 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.
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.
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.
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.
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.
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.
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.
Check your understanding
-
intro-4-1: The activecode interpreter allows you to (select all that apply):
- (A) save programs and reload saved programs.
- You can (and should) save the contents of the activecode window.
- (B) type in Python source code.
- You are not limited to running the examples that are already there. Try adding to them and creating your own.
- (C) execute Python code right in the text itself within the web browser.
- The activecode interpreter will allow you type Python code into the textbox and then you can see it execute as the interpreter interprets and executes the source code.
- (D) receive a yes/no answer about whether your code is correct or not.
- Although you can (and should) verify that your code is correct by examining its output, activecode will not directly tell you whether you have correctly implemented your program.
-
intro-4-2: Codelens allows you to (select all that apply):
- (A) measure the speed of a program's execution.
- In fact, codelens steps through each line one by one as you click, which is MUCH slower than the Python interpreter.
- (B) control the step by step execution of a program.
- By using codelens, you can control the execution of a program step by step. You can even go backwards!
- (C) write and execute your own Python code.
- Codelens works only for the pre-programmed examples.
- (D) execute the Python code that is in codelens.
- By stepping forward through the Python code in codelens, you are executing the Python program.