1: A Good First Program
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 go on. 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:
print("I like typing this.")
print("This is fun.")
print("I'd much rather you 'not'.")
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
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
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:
Cell In, line 3
print("I like typing this.
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
- 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 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.
The Study Drills contain things you should try to do. If you can't, skip it and come back later.
For this exercise, try these things:
- Make your script print another line.
- Make your script print only one of the lines.
- Put a
#(octothorpe) character at the beginning of a line. What did it do? Try to find out what this character does.
From now on, I won't explain how each exercise works unless an exercise is different.
Common Student Questions
These are actual questions that real students have asked when doing this exercise.
- Can I use IDLE? No, for now just use Jupyter and later we'll use a regular text editor for extra super powers.
- Editing the code in Jupyter is annoying. Can I use a text editor? Totally, you can also create a
pythonfile in Jupyter and get a "good enough" editor. In the left panel where you see all your files, click the blue
[+]button on the top left. That will bring you to the first screen you saw when you started Jupyter. Down at the bottom under
$_ Otheryou'll see a button for
Python Filewith the Python logo. Click that and you'll get an editor to work on your file (see screenshot below).
- My code doesn't run; I just get the prompt back with no output. You most likely took the code in my cell literally and thought that
print("Hello World!")meant to type only
"Hello World!"into the cell, without the
The Blue Plus
If you ever want to create a file with Jupyter and use its editor you can use the "blue plus" as shown in this image which I've placed a giant red circle around:
If you don't see this then you most likely started Jupyter wrong by accidentally starting the Jupyter Notebook in your start menu instead of typing
Register for Learn Python the Hard Way, 5th Edition (2023-2024)
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.