07. Hello, World
The traditional first program in any language prints
Hello, world! and exits. It is tiny, but writing it proves
the language is installed, the editor is set up, and the terminal can
run files. This chapter writes it, breaks it to see an error, then fixes
it.
The whole program
Open exercises/07/01-hello.py in VS Code. The file
contains one line:
print("Hello, world!")In the VS Code terminal, run it:
python exercises/07/01-hello.py
The terminal prints:
Hello, world!
That is the whole program. The rest of this chapter explains each part of that line.
What print does
print is a function — a named piece of
code that does a job when you ask for it. The job of print
is to write whatever you give it to the terminal, followed by a new
line.
The parentheses ( and ) after the name are
how you ask the function to run. Anything between them is what you give
it.
print("Hello, world!")
# ^^^^^^^^^^^^^^^
# this is what we are giving to printIn Python, the things you give a function are called
arguments. The program above gives print
one: the string "Hello, world!".
Change the text inside the quotes to your own name. Save and run it again. The terminal should now print your name.
Strings
The text "Hello, world!" is a string —
a sequence of characters. In Python, strings are written between
double quotes " or single
quotes '. Both work:
print("Hello, world!")
print('Hello, world!')The quotes are not part of the string. They mark where the string starts and where it ends. When the string prints, only what is between the quotes appears.
Printing more than one thing
print can take more than one argument. Separate them
with commas:
print("Hello,", "world!")The terminal prints Hello,, a space, then
world!, all on one line. print puts a space
between each argument and a new line at the end.
For a different separator, build one string yourself with
+, which sticks strings together:
print("Hello, " + "world!")Now the output is Hello, world! with one space between
the words, because the space is inside the first string.
A cleaner way to combine text and values is an
f-string. Put f before the opening quote
and use {} curly braces to drop a value in:
name = "world"
print(f"Hello, {name}!")Output:
Hello, world!
Make print show three things on one
line: the word python, the number 3, and the
word is_fast. Run it. Notice the number appears even though
it has no quotes around it.
Comments
A comment is text Python ignores when it runs the
program. Comments are for the humans reading the code. A single-line
comment starts with #:
# This line is a comment. Python does not run it.
print("Hello, world!") # A comment can also sit at the end of a line.Python has no special multi-line comment syntax. To write a longer
block of notes, just use several # lines in a row:
# Anything in here is ignored.
# Useful for long explanations
# or for temporarily turning off
# a block of code.
print("Still runs.")Use comments to explain why the code does something, not what each line does. Good code already says what.
Making and reading errors
Now break the program on purpose. In 01-hello.py, remove
the last quote so the line reads:
print("Hello, world!)Save and run it. Python refuses to run the file and prints an error like this:
File "exercises/07/01-hello.py", line 1
print("Hello, world!)
^
SyntaxError: unterminated string literal (detected at line 1)
Read errors in this order:
- The file name and line number
(
line 1). This is where the problem is. Look here first. - The error type (
SyntaxError). This is the category of problem. - The short message
(
unterminated string literal). This is what went wrong. "Unterminated string literal" means Python saw the opening quote and started a string but never found the closing one.
Errors are not punishments. They tell you exactly which line to look at and hint at what is wrong.
Fix the missing quote so the program runs again. Then try another
mistake: change print to prnt and run it. Read
the new error. What does the wording suggest? (No need to write down
an answer. Just notice.)
A note on whitespace and case
Python does care about indentation (the spaces at the start of a line). You will learn why in a later chapter. For now, do not add spaces at the start of any line unless the book tells you to.
Python does care about capital letters.
print is a different word from Print and
PRINT. The built-in name is lower case.
Homework
The homework files are in exercises/07/homework/. Open
each starter in VS Code, read the comment at the top, and finish the
file so it runs and produces the expected output.
Problem 1 — Greet yourself
Open exercises/07/homework/01-greet-yourself.py. The
file is empty except for a comment. Write three print calls
so that running the file shows, on three separate lines:
- a greeting (your choice of words),
- your first name,
- the name of your favourite game.
Problem 2 — Print a quote
Open exercises/07/homework/02-print-quote.py. Print a
famous quote on the first line and the author's name on the second line
in this format:
"The best way to predict the future is to invent it."
— Alan Kay
(The dash before the author can be a regular hyphen -.
Exact spacing does not matter, as long as the quote is on one line and
the author is on the next.)
Problem 3 — Fix the error
Open exercises/07/homework/03-fix-the-error.py. The file
does not run. Read the error Python prints, fix the file, and run it
until you see:
All good now.
There is more than one mistake in the file. Find them all.
Challenge — Three-line story
Open exercises/07/homework/04-three-line-story.py. Write
a three-line micro-story where:
- the first line sets the scene,
- the second line introduces a problem,
- the third line resolves the problem.
Each line should be printed by its own print call. Add a
block of # comment lines at the top explaining, in plain
English, what your story is about.
Stuck or finished? Open the homework solutions page for a walkthrough.