Chapter 1, Lesson 2 Text URL

Chapter One: Fundamentals of Python

Lesson Two: Running Python Programs

 

Computer programs written in Python start out as source files or source code. These files, normally ending with a ".py" extension, contain the human-readable Python code that makes up a program. In this lesson, we will discuss how Python source code can be executed or run to see the program results.

Interpreted Languages

Python is an example of an interpreted language. This means the source files are read by a Python interpreter program and converted to machine commands "on the fly" or as the program is running. Therefore, in order to run a Python program, you must have a Python interpreter to translate the source code to a working program.

Interpreter translates source code to Python program

The Python interpreter will read the source code and translate each Python statement to one or more meaningful computer commands. The resulting collection of computer commands is your fully functional Python program, which actually runs within the interpreter.

Running Python on Your Computer

You can, of course, create Python code and run Python programs directly on your computer. A Python interpreter program is available for Windows, Mac OS, Linux and other operating systems. If you want to run Python on your computer, then you would follow these steps:

  1. Ensure you have a Python interpreter installed on your computer
  2. Create the Python ".py" source code files
  3. Run the Python interpreter with the source files to produce the program output

For example, here is a one-line Python source file named "alive.py" that will print a message to the screen.

print("I'm Alive!")

Once you have installed your Python interpreter and created a Python source file, you can run the program from the Windows Command Prompt or Mac OS Terminal. To run the source code in your Python interpreter, from the command prompt or terminal you would type "python" and then the name of your source file, including the ".py" file extension.

/users/student> python alive.py
I'm Alive!

In many cases, your Python source code will be stored in some other directory on your computer. In that case, you would include the full or relative path to the file after "python", as demonstrated below.

/users/student> python "Documents/My Projects/alive.py"
I'm Alive!

In this example, the Command Prompt started out in the "/users/student" directory, and we saved the "alive.py" source file in "/users/student/Documents/My Projects" directory. So, we passed in the path "Documents/My Projects/alive.py" to help Python find the right source file.

Over time, software engineers have developed many useful tools to help them write code and run programs. An Integrated Development Environment or IDE is a software application that lets programmers create and run code in one graphical interface. When using an IDE, you don't have to use separate text editors to create source code or open a command line to run the program. Python comes with an easy-to-use IDE (shown below) if you choose to write Python code on your own computer.

Python IDE Example

Instructions for installing Python on your computer, creating Python source files, and running a Python program can be found in the first Supplemental chapter near the end of the course. However, running Python on your computer is not required in most cases! Instead, you can use our online Python engine as described below.

Running Python in Our Online Course

In order to run Python successfully on your local computer, you need to install the Python interpreter, create and save source files, successfully open a Command Prompt or Terminal and understand the exact path to your Python source file. These steps can be somewhat complex for a new programmer, especially if you have never used a command-line interface before.

Fortunately, there is an easier option! We have created a special online Python interpreter that you can use directly from your course lesson pages! You don't need to install any software, create and save text files or work with a command-line interface. Simply type your Python source code into one window, click a button, and see the results in the output panel.

Give our online Python interpreter a try below! Click the "Run Code" button to see the results in the Console panel. Some exercises will have hints or solutions that you can see by clicking those buttons as well.

Try It Now

  

 

You can change the "I'm Alive!" text to something else and click "Run Code" again to see the new output. Just be sure to leave the rest of the source code alone. We'll explain how the print() statement works in the next lesson.

If you need to start over from the default code on any of these in-lesson Python panels, simply reload your web page in your browser. Press "CTRL+F5" on Windows or "Command+R" on Mac OS to force your lesson page to reload.

 

Work with Me: Exploring Our Online Python Engine

 

To get comfortable with our online Python interpreter, spend some time working with the code panel below.

  1. Try running the default code as-is to see the results.
  2. Then, change the quoted text that will be printed to the screen on the first line.
  3. Add your name after "is" inside the quoted text on the second line.
  4. Click "Run code" again to see your new output.

Try It Now

  

 

Click on the "Show Hint" and "Show Solution" buttons to see how those extra pieces of information will normally appear. If you receive an error message instead of the expected output, carefully compare your source code to the solution code and see if you can spot any differences.

End of Lesson


Last modified: Wednesday, 8 May 2019, 7:48 AM