Skip to main content

Foundations of Python Programming: Functions First

Section 1.4 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.
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. Try pressing the Save & Run button below. (If you are not logged in, it will just say Run.)
Now try modifying the program shown above. First, modify the text in the first line by changing the word adds to the word multiplies. Now press Save & Run again. 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, +, to the multiplication symbol, *. Press Save & Run again to see the new results.
As the name suggests, Save & Run also saves your latest version of the code, and you can recover it even in later sessions when logged in. If not logged in, Run saves versions only until your browser leaves the current web page, and then you lose all modifications.
If you are logged in, when a page first loads, each ActiveCode window will have a Load History button, to the right of the Save & Run button. If you click on it, or if you run any code, that button turns into a slider. If you click on the slider location box, you can use your left and right arrow buttons to switch to other versions you ran. Alternately you can drag the box on the slider. Now move the slider to see a previously saved version of your code. You can edit or run any version.
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. 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. Click on the “Show in CodeLens” button to make the CodeLens window show up, and then click on the Forward button a few times to step through the execution.
Sometimes, we will present code examples explicitly in a CodeLens window in the textbook, as below. When we do, think of it as an encouragement to use the CodeLens features to step through the execution of the program.
Check your understanding

Checkpoint 1.4.1.

    The activecode interpreter allows you to (select all that apply):
  • save programs and reload saved programs.
  • You can (and should) save the contents of the activecode window.
  • 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.
  • 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.
  • 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.

Checkpoint 1.4.2.

    CodeLens allows you to (select all that apply):
  • 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.
  • 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!
  • write and execute your own Python code.
  • CodeLens works only for the pre-programmed examples.
  • execute the Python code that is in CodeLens.
  • By stepping forward through the Python code in CodeLens, you are executing the Python program.
You have attempted of activities on this page.