Your First Python Program
Write a Python program in a .py file that prints lines with print(), run it with python and the file's name, leave notes in it with comments, and read a traceback from its last line up to find what went wrong and on which line.
Novice · Python · Updated
After this lesson you can
- Write and run a Python file that prints lines with print(), using text in quotes and comments that Python skips
- Read a Python traceback from its last line up to find the error's name, its message and the line number to fix
Key takeaways
- A Python program is a .py file, and python and the file's name, such as python hello.py, runs it from top to bottom.
- print() shows a line in the terminal. Text goes in quotes, called a string, and the quotes themselves aren't printed.
- A comment starts with a hash. Python skips everything after it on that line, so comments hold notes for people and switch lines off.
- A traceback is read from its last line up. The last line names the error and says what's wrong, and the File line above it gives the line number.
- A NameError means Python met a name it doesn't know, often a typo. A SyntaxError means Python couldn't read the code at all, such as a missing quote.
Every Python program you'll ever write starts the same way: you type a few lines, run them, and read what comes out, or what went wrong. This chapter's example is Maple Street Bakery, a neighborhood bakery whose owner wants the day's menu board printed by a program, so it can go on the screen by the counter.
Your first program: hello.py
In VS Code, open your python-course folder, then make a folder in it named maple-street-bakery and open that with File > Open Folder. In the Explorer on the left, click New File, name it hello.py, type this line and save it with Ctrl+S (Cmd+S on a Mac):
print("Good morning from Maple Street Bakery!")
Open the terminal (Terminal > New Terminal) and run the file: type python, a space, and the file's name, then press Enter. On a Mac, type python3 wherever a lesson says python, as Setup on a Mac: Python, VS Code and Git explains.
python hello.py
Good morning from Maple Street Bakery!
The .py at the end of the name says the file holds Python. python hello.py asks the Python interpreter to read the file and run its lines, top to bottom. print() (read "print") shows a line in the terminal. The text inside the quotes is a string: a piece of text, written between a pair of double quotes ("). The quotes tell Python where the text starts and stops; they aren't printed.
Several lines, several values
Each print() prints one line. Give it several values, separated by commas, and it prints them on one line with a space between each. A number needs no quotes. Save this as board.py, then run python board.py:
# Maple Street Bakery's menu board
print("MAPLE STREET BAKERY")
print("Blueberry muffin:", "$3.25")
print("Sourdough loaf:", "$6.50")
print("Loaves left:", 14)
MAPLE STREET BAKERY
Blueberry muffin: $3.25
Sourdough loaf: $6.50
Loaves left: 14
Comments: notes Python skips
The first line of board.py starts with # (read "hash"). That makes it a comment: Python skips everything from the # to the end of the line. Comments are notes for the people who read the code, including you next month. They're also handy for switching a line off without deleting it:
# Today's specials, printed for the screen by the counter.
print("Cranberry scone: $3.50")
# print("Lemon tart: $4.50") sold out today
print("Cinnamon roll: $3.75") # back in the oven at 10 AM
Cranberry scone: $3.50
Cinnamon roll: $3.75
The lemon tart's line is still in the file for tomorrow, but it doesn't run. A comment can also sit at the end of a line of code, as the last line shows. In VS Code, Ctrl+/ (Cmd+/ on a Mac) turns the line you're on into a comment, or back.
When it goes wrong: reading a traceback
A smoke alarm panel says which room the smoke is in. When Python stops on an error, it prints a traceback: a report that says what went wrong and where. In this file, typo.py, print lost a letter on line 2:
print("MAPLE STREET BAKERY")
prnt("Blueberry muffin: $3.25")
print("Sourdough loaf: $6.50")
MAPLE STREET BAKERY
Traceback (most recent call last):
File "C:\Users\Sam\python-course\maple-street-bakery\typo.py", line 2, in <module>
prnt("Blueberry muffin: $3.25")
^^^^
NameError: name 'prnt' is not defined. Did you mean: 'print'?
Line 1 ran, then Python stopped. Read a traceback from its last line up:
- The last line names the error and says what's wrong. A NameError means Python met a name it doesn't know, here
prnt, and it even suggests the name you probably meant. - The File line above it gives the file and the line number: line 2. On a Mac the path looks like
/Users/sam/python-course/.... - The code line under it shows that line, with
^marks (read "caret") under the part Python stopped on.
Lines after the error never run, so the sourdough loaf isn't printed. Fix the typo, save, and run again.
A missing quote is a different kind of mistake: Python can't even read the file, so nothing runs at all, not even line 1. Here's quote.py:
print("MAPLE STREET BAKERY")
print("Blueberry muffin: $3.25)
print("Sourdough loaf: $6.50")
File "C:\Users\Sam\python-course\maple-street-bakery\quote.py", line 2
print("Blueberry muffin: $3.25)
^
SyntaxError: unterminated string literal (detected at line 2)
A SyntaxError means the code breaks Python's rules of writing, its syntax. Unterminated string literal means a string that never ends: its closing quote is missing. The caret points at the quote that opened it, on line 2.
Do it: print the bakery's window sign
Maple Street Bakery shows a small sign in its window, printed by a program. Today's sign needs fixing: scones are only sold on weekends, and two lines are missing.
Your job: change the program so it prints exactly these three lines, in this order, and nothing else:
MAPLE STREET BAKERY
Today's bread: Rye
Muffins left: 18
- Keep the first line as it is.
- Turn the scone line into a comment, so it stays in the file for the weekend but doesn't print.
- Add a
print()line for the bread and one for the muffins. Every character counts: capital letters, the colons and the spaces.
Worked through: the starter prints MAPLE STREET BAKERY and Scones: $3.50. With the scone line a comment and the two new lines added, it prints the three lines above, and that is the first test.
Practiced live on the lesson page: write the Python right there and check it.
Check yourself
2 questions to try on the lesson page.
So what
print(), a .py file and python with the file's name are how every program in this course runs, and a traceback read from its last line up tells you what went wrong and which line to fix.