This lesson is being piloted (Beta version)

Creating and Running Python Scripts

Overview

Teaching: 10 min
Exercises: 15 min
Questions
  • How can I run Python scripts and see results from them?

  • How do you write comments?

Objectives
  • Create a Python script and save it with an appropriate name.

  • Run the Python script from the command-line.

  • Write comments in the script, and use them to disable a line of code.

Python programs are plain text files

Write your first Python program

The traditional first program outputs “Hello World”. So, open up your text editor with a new file. Enter the following Python code:

print("Hello world!")

Now save the file as hello-world.py.

Next, open up a command-line terminal, and change to the directory that you saved hello-world.py to. At the command-line, call the Python interpreter and tell it to run your script:

$ python3 hello-world.py

Solution

You should see this output:

Hello world!

Why is the command python3 instead of just python?

This lesson uses Python 3, which is the current major version of Python. However, a lot of systems still have Python 2 as the default version which will be called if you use the python command. If python3 doesn’t work, then try python. On your system this may be Python 3. You can always check the versions by trying the following at the command-line:

$ python --version
$ python2 --version
$ python3 --version

Comments

Identify the good and bad comments

In the following code, try to identify which comment is useful and which is not. Why?

# Filter out missing values, because they break my analysis
filter_missing_values(my_data)

y = 2 * x + 7  # Multiply x by 2, add 7, then store in y

Solution

  1. The first comment is useful because it tells us something of the programmer’s intent that is not obvious from the code itself.
  2. The second comment simply rephrases what the code does. It does not add value.

Modify your first program with some comments

FIXME: Is this too easy?

Open up hello-world.py with your editor again, and comment out the print line. Then run the file once more. What happens? Why?

Solution

There should be no output from your program, since you commented out the only line of code.

Experiment

FIXME: Is this too easy?

Spend a few minutes experimenting with your hello world program.

Suggestions:

  • Uncomment the print statement.
  • Add a comment to the end of the line.
  • Change the progam to print a different message.

Key Points

  • Python scripts are plain text files, usually with the .py extension.

  • You execute Python scripts at the command-line with the Python interpreter.

  • Values are displayed using the print function.

  • Comments are indicated with #. Anything between the # and the end of the line is ignored.