A Good First Program
- What You Should See
- Study Drills
- Common Student Questions
- The Blue Plus
You should have spent a good amount of time in Exercise 0 learning how to install Jupyter, run Jupyter, run the Terminal, and work with both of them. If you haven’t done that, then do not proceed. You will not have a good time. This is the only time I’ll start an exercise with a warning that you should not skip or get ahead of yourself.
Type the following text into a Jupyter cell:
Listing 1.1: ex1.py
1 print("Hello World!") 2 print("Hello Again") 3 print("I like typing this.") 4 print("This is fun.") 5 print('Yay! Printing.') 6 print("I'd much rather you 'not'.") 7 print('I "said" do not touch this.')
Your Jupyter cell should look something like this:
Don’t worry if your Jupyter window doesn’t look exactly the same; it should be close though. You may have a slightly different window header, maybe slightly different colors, and the left side of your Jupyter window won’t be the same, but will instead show the directory you used for saving your files. All of those differences are fine.
When you create this cell, keep in mind these points:
I did not type the line numbers on the left. Those are printed in the book so I can talk about specific lines by saying, “See line 5…” You do not type line numbers into Python scripts.
I have the print at the beginning of the line, and it looks exactly the same as what I have in the cell. Exactly means exactly, not kind of sort of the same. Every single character has to match for it to work. Color doesn’t matter, only the characters you type.
Once it is exactly the same, you can hit SHIFT-ENTER to run the code. If you did it right, then you should see the same output as I in the What You Should See section of this exercise. If not, you have done something wrong. No, the computer is not wrong.
What You Should See
The Jupyter output will look like this after you hold SHIFT and hit ENTER (which I’ll write as SHIFT-ENTER):
You may see different window appearance and layout, but the important part is that you type the command and see the output is the same as mine.
If you have an error, it will look like this:
1 Cell In[1], line 3 2 print("I like typing this. 3 ^ 4 SyntaxError: unterminated string literal (detected at line 1)
It’s important that you can read these error messages because you will be making many of these mistakes. Even I make many of these mistakes. Let’s look at this line by line.
We ran our command in the Jupyter cell with SHIFT-ENTER.
Python tells us that the cell has an error on line 3.
It prints this line of code for us to see it.
Then it puts a ^ (caret) character to point at where the problem is. Notice the missing " (double-quote) character at the end though?
Finally, it prints out a “SyntaxError” and tells us something about what might be the error. Usually these errors are very cryptic, but if you copy that text into a search engine, you will find someone else who’s had that error, and you can probably figure out how to fix it.