Level 1 · First Words

Hello, World! 👋

Tradition demands that your first program says hello to the world. Tradition is right, and it will take you about four seconds.

The program

Press ▶ run.

print("Hello, world!")
Hello, world!

That is a complete, real Python program. Not a toy version, not a simplified teaching dialect. If you put that line in a file called hello.py and ran it on a server in a data centre, it would do exactly the same thing.

Taking it apart

Four things are happening in those twenty-one characters:

PieceNameWhat it does
printA functionA named action that already exists. Somebody wrote it so you do not have to
( )Brackets, or 'parens'How you call a function: 'do it now, with this'
"Hello, world!"A stringText. The quotes mark where it starts and stops
The whole lineA statementOne complete instruction

print means "show this to the human". It does not mean paper. Nobody has printed anything on paper from a program on purpose since about 1994, but the name stuck, the way "dialling" a phone stuck.

INTERFACING[Easy: Success]

The quotes are a fence, not decoration. Inside the fence, Python does not think, it just carries the characters through. Outside the fence, every word has to mean something. Confusing the inside for the outside is the single most common mistake of week one.

Print more than one thing

print("Guybrush Threepwood")
print("Mighty programmer")
print("Also: mighty pirate")
Guybrush Threepwood
Mighty programmer
Also: mighty pirate

Each print ends with a new line. Three prints, three lines. Python runs your file strictly top to bottom, like reading a recipe, and never skips ahead.

You can also hand print several things at once, separated by commas:

print("Grog level:", 7, "out of", 10)
Grog level: 7 out of 10

Notice Python put spaces between them for free. Notice also that 7 has no quotes: it is a number, not text, and Lesson 3 is about why that distinction matters.

Single or double quotes?

print("Both of these work.")
print('There is no difference.')
print("Use doubles when the text has an apostrophe: it's easier.")
print('Use singles when the text has "quotes" in it.')
Both of these work.
There is no difference.
Use doubles when the text has an apostrophe: it's easier.
Use singles when the text has "quotes" in it.

Python genuinely does not care. Pick doubles as your default (that is what the autoformatter in Lesson 30 will do anyway) and switch when it saves you an escape.

Comments: notes to humans

Anything after a # is ignored by Python entirely. It exists for the next person to read the file, who is usually you, six months from now, having forgotten everything.

# This line does nothing at all. It is for you.
print("Insult the swordsman")   # notes can also sit after code

# print("This line is switched off.")
print("This line is not.")
Insult the swordsman
This line is not.

That third trick, putting a # in front of a line to disable it, is called commenting out, and you will do it fifty times a day while hunting bugs. In your editor the shortcut is Ctrl+/ (Cmd+/ on a Mac).

✍️ What makes a good comment

Bad: # add one to x. We can see that. Good: # the API counts from 1, not 0. Comments should explain why, because the what is already sitting right there in the code.

Your first error, on purpose

Errors are not failures. They are Python telling you, in detail, what it could not understand. Here is a broken line:

print("Look behind you, a three-headed monkey!)

Python says:

  File "hello.py", line 1
    print("Look behind you, a three-headed monkey!)
          ^
SyntaxError: unterminated string literal (detected at line 1)

Read it like a form:

Modern Python error messages are genuinely excellent, and they got dramatically better in 3.10 and 3.11. Lesson 10 is devoted entirely to reading them. For now, absorb the one habit that matters: read the last line first. It names the problem.

VOLITION[Medium: Success]

You are going to see hundreds of these. Thousands. Experienced programmers do not see fewer errors than you, they see more, because they write more code. What changes is the reaction time: from twenty minutes of despair, down to four seconds of 'ah, a missing quote'.

Two ways to run Python

WayHowGood for
A fileWrite hello.py, run python3 hello.pyReal programs you want to keep and re-run
The REPLType python3 with no filenameTrying one thing quickly. It prints the answer to every line automatically
>>> print("Hello")
Hello
>>> 2 + 2
4
>>> "grog " * 3
'grog grog grog '

Notice that in the REPL, 2 + 2 shows 4 without any print. That convenience exists only in the REPL. In a file, a line that just says 2 + 2 computes 4 and throws it away in silence. Beginners lose an afternoon to this at least once, so now you will not.

Exercise 1

Introduce yourself

Write a program that prints three lines: your name, one thing you want to build, and how many years you have been meaning to learn this.

Reveal solution
print("Chris")
print("I want to build a tool that renames my photo library.")
print("I have been meaning to do this for 3 years.")
Chris
I want to build a tool that renames my photo library.
I have been meaning to do this for 3 years.
Exercise 2

Fix the broken program

Three separate mistakes. Find and fix all of them.

Print("The first mistake is on this line.")
print(Second mistake here.)
print("Third mistake is at the end."
Reveal solution
  1. Print with a capital P. Python is case sensitive; the function is print. You would get NameError: name 'Print' is not defined.
  2. No quotes around the text, so Python tries to read Second mistake here. as code and gives up.
  3. The closing bracket is missing.
print("The first mistake is on this line.")
print("Second mistake here.")
print("Third mistake is at the end.")
The first mistake is on this line.
Second mistake here.
Third mistake is at the end.
Exercise 3

Draw something

Print a small picture using text. A house, a ship, a crab, anything. Multiple print lines stack up, so you have a canvas.

Reveal solution
print("      |>>>")
print("      |")
print("  __ _|__")
print("  \\      /")
print("~~~\\~~~~/~~~~~~~")
      |>>>
      |
  __ _|__
  \      /
~~~\~~~~/~~~~~~~

The doubled backslashes are not a typo. A backslash has a special meaning inside a string (Lesson 4 explains it), so to print one you write two. Or you can put an r in front of the quote, like r"\_/", which means 'raw: take this literally'.

+100 XP