Sub topic 9
Writing your first script file
This chapter assumes Python 3 is installed and python3 --version (or py --version on Windows) works in your terminal. It also assumes you have tried the REPL and have a code editor. Running scripts in depth comes in the next chapter.
Why a script file
The REPL (the interactive interpreter) forgets everything when you exit. A script is a plain text file of Python statements that you can save, edit, and run again. Python reads it from top to bottom and executes each statement in order.
Create the file
- Make a folder for your practice files, for example
python-practice. - Open that folder in your editor.
- Create a new file named
hello.py. - Type this single line:
print("Hello, world!")- Save the file.
The .py extension is a convention. Python does not require it when you run a file by name, but editors use it to turn on syntax highlighting, and tools such as import rely on it.
To check that it works, open a terminal in the same folder and run it:
python3 hello.py
# On Windows, use: py hello.pyYou should see Hello, world! printed.
Scripts versus the REPL
In the REPL, a bare expression such as 2 + 3 prints its value automatically. In a script, it does not. Only print() produces output.
2 + 3
print(2 + 3)If you run this file, it prints 5 once. The first line is evaluated and its result is discarded.
Build it up a little
A script can hold several statements, variables, and comments. A comment starts with # and is ignored by Python.
# greeting.py
name = "Ada"
language = "Python"
print("Hello, " + name)
print(name + " is learning " + language)Each line runs in order, so name must be assigned before it is used. Move the print lines above the assignments and you will get a NameError. The traceback chapter covers how to read that.
Habits that prevent early problems
- Use plain-text encoding. Save as UTF-8, which is the default in most editors. Do not use a word processor.
- Use 4 spaces per indentation level. Python uses indentation to define blocks. Mixing tabs and spaces causes errors.
- Avoid names that shadow modules. Do not name your file
random.pyorjson.py. When you later writeimport random, Python may load your file instead of the standard library module. - Avoid spaces and hyphens in file names. Use
my_script.py. A name likemy-script.pycannot be imported as a module. - Check the extension. Some systems hide extensions. A file saved as
hello.py.txtwill not behave as you expect.
Quick reference
| Element | Example | Purpose |
|---|---|---|
| Comment | `# note` | Explains code, ignored by Python |
| Variable | `name = "Ada"` | Stores a value under a name |
| Output | `print(name)` | Shows a value in the terminal |
| File name | `hello.py` | Lowercase, no spaces, ends in `.py` |
Exercise
You save a file as hello.py containing the lines message = "Hi" and message. You run it and see no output. Why, and how do you fix it?
Show answer
A bare expression is only echoed automatically in the REPL. In a script, nothing is printed unless you call print(). Change the second line to print(message).
Exercise
Why is random.py a bad name for your own script?
Show answer
A later import random in your project could load your file instead of Python's standard library random module, which causes confusing errors. Choose a different name such as random_demo.py.