Your First Program File
The prompt forgets everything the moment you leave it. A program you want to keep, run again tomorrow, or improve next week has to live somewhere permanent. That somewhere is a file. A Python program is nothing more than a plain text file whose name ends in .py: you write it in an editor, save it, and run it from the terminal.
If the prompt was a phone call, a program file is a letter. The call is gone when you hang up. The letter can be read again, corrected, and sent as many times as you like. This page writes your first one, and walks once round the loop you will repeat for the rest of the book: edit, save, run, read.
A Text File, Nothing More
Any editor that saves plain text will do. On Windows that is Notepad. On a Mac it is TextEdit, with two settings changed first. In TextEdit's settings, choose Plain text as the format for new documents, and switch off Smart quotes. Topic 4 of Chapter 12 introduces a proper code editor later; for now, the simplest tool keeps the focus on Python.
A word processor such as Word is the wrong tool, even though it edits text. It stores formatting inside the file, invisible to you but not to Python. The smart-quotes setting matters for a related reason. Python needs straight quotation marks. An editor that quietly curls them produces a file Python refuses to read, as the example below shows.
Your First Program: hello.py
Open your editor and type one line: the word print, an opening bracket, a sentence inside straight double quotes, and a closing bracket. The instruction print tells Python to show something in the terminal. The quotation marks tell Python that what is between them is text to show, not a name to look up, which is exactly what went wrong with hello at the prompt in the last topic.
print("Hello! This is my first program.")
Save the file as hello.py inside the python-course folder you made in Topic 3. On Windows, in Notepad's save window, set the type to All files so Notepad does not add .txt to the end. On a Mac, if TextEdit asks whether to use the extension .py, choose to use .py.
Now run it. In the terminal, make sure you are in python-course; the pwd command tells you. Then type the Python command followed by a space and the file name: python hello.py on Windows, python3 hello.py on a Mac. Python reads the file, carries out its one instruction, and the sentence appears. Then the terminal's prompt returns, because the program has finished.
you@mac python-course % python3 hello.py Hello! This is my first program.
Top to Bottom
Add two more print lines under the first one, save, and run again. Three sentences appear, in the same order as the lines in the file. That is the most basic rule of a program: Python starts at the first line and works down, one instruction at a time. The order of the lines is the order of events.
Comments: Notes for People
A line that starts with a hash sign is a comment. Python skips it completely; it is there for people reading the file, including you in a month's time. The version below adds one comment at the top. A good comment explains why the code is there or what it is for. It does not repeat what the line already says, because the code itself says that.
# My first program: three lines, printed top to bottom. print("Hello! This is my first program.") print("Python runs each line in order.") print("This line comes last.")
Hello! This is my first program. Python runs each line in order. This line comes last.
Change, Save, Run
Now change the last sentence, and run the program again without saving first. The old sentence appears. Nothing is wrong with Python: it runs what is saved on the disk, and an edit that has not been saved does not exist as far as Python is concerned. Save, run again, and the new sentence appears. This is the single most common "it did not change" moment in programming, and now you have met it on purpose.
One more error is worth seeing once. If an editor curled the quotation marks, Python stops before running anything and prints a syntax error, meaning it could not read the line at all. The message names the exact character it rejected: an opening curly quote. Retype the quotes as straight ones, or switch off smart quotes, and the program runs.
File "/Users/you/python-course/hello.py", line 1
print(“Hello! This is my first program.”)
^
SyntaxError: invalid character '“' (U+201C)
- "I can write Python in Word." A word processor hides formatting inside the file and curls quotation marks. Python needs plain text with straight quotes.
- "The program ran, so my changes were saved." Python runs whatever is saved on the disk. An unsaved edit does not exist until you save it.
- "Comments should explain every line." Good comments explain why; the code already says what. A comment that repeats the line is one more thing to keep up to date.
- "The terminal can run hello.py from anywhere." Written this way, the file name is looked up in the current folder. Run it from python-course, or the terminal will say the file cannot be found.
- Edit, save, run, read is the loop the rest of this book repeats thousands of times, and so do professional programmers every day.
- A file is something you can keep, share, improve and, in Chapter 12, test automatically.
Knowledge Check
Why is a word processor the wrong tool for writing Python?
- Its files are too large for Python to open in a reasonable time
- It stores hidden formatting and curls the quotation marks
- Python can only read files written in a programming editor
- Its spellchecker changes Python's words into English words
You edit hello.py, run it without saving, and see the old output. Why?
- Python keeps a copy of every program and runs the older one first
- The terminal has to be closed and reopened after each change you make
- Python runs what is saved on disk, and the edit was not saved yet
- The file name must change every time the program is edited again
What does Python do with a line that starts with a hash sign?
- It prints the line in the terminal as a heading
- It skips the line; the comment is a note for people
- It stops the program because the line is not valid
- It runs that line first, before all of the others
Python reports SyntaxError: invalid character '“'. What is the likely cause?
- The file was saved with a name that Python does not allow
- The sentence inside the quotes contained too many words
- The word print is not an instruction that Python knows
- The editor replaced straight quotation marks with curly ones
You got correct