Skip to content

1 Hello, World!

IGCSE · 0478Part 1A

Suggested time: 1 lesson · Syllabus: 0478 8.1.3 (output) · 4.2.5 (the IDE: a Paper 1 topic) · Code: code/gcse/castle_adventure/step_01

What you will build

The title screen for The Castle Adventure, the game you will build, one chapter at a time, through the whole of Part 1.

Output
        |>                                  |>
       _|_        _   _   _   _   _        _|_
      |___|      | |_| |_| |_| |_| |      |___|
      |   |______|                 |______|   |
      | # |      |      _____      |      | # |
      |   |  []  |     /     \     |  []  |   |
      |___|______|____|   |   |____|______|___|
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       T H E   C A S T L E   A D V E N T U R E

Welcome to the Castle Adventure!
Somewhere in this castle, the Dark Lord is waiting.

By the end of this chapter you will be able to:

  • write, save and run a Python program;
  • use print() to output text;
  • explain what a string is, and why it needs quotation marks;
  • explain what sequence means;
  • read and fix a syntax error.

Key vocabulary

Term Meaning Example
Program A set of instructions for a computer to carry out main.py
Statement One instruction in a program print("Hi")
Output Information a program gives out, for example on the screen print()
String A piece of text. In code it goes inside quotation marks "Welcome!"
Sequence Carrying out statements one after another, in order line 1, then line 2…
Syntax The rules for how a language must be written brackets must be closed
Syntax error A mistake that breaks those rules, so the program can't run at all a missing "
Comment A note for humans. The computer ignores it # like this

The big idea: a program is a list of instructions

A computer does exactly what it is told, in exactly the order it is told. Python starts at the top line of your program and works down, one statement at a time. That order is called sequence, and it is the first of the three building blocks that every program is made from (the others, selection and iteration, arrive in Chapters 4 and 5).

Concept: sequence

How it works: statements run one after another, top to bottom. Swap two lines and they run in the new order.
Why we use it: it makes a program predictable. You can always work out what it will do by reading it from the top.
Where else it's used: everywhere. Even the biggest programs are sequences of statements, with selection and iteration deciding which statements run and how many times.

Step 1: Your first program

Open your editor (Chapter 0.2), create a new file, type this line, and save it as hello.py:

hello.py
print("Hello, World!")

Run it (the green Run button in Thonny, or F5):

Output
Hello, World!

What's happening?

print is a function built into Python: a ready-made instruction. The brackets hold what you want it to print. "Hello, World!" is a string. The quotation marks show Python where the text starts and ends; they are not printed.

Why 'Hello, World!'?

Printing "Hello, World!" is a tradition: it's often the first program people write in a new language.

Step 2: Sequence

Add two more lines, then run the program again:

hello.py
print("Hello, World!")
print("My name is Sam.")
print("I am going to build a game.")

The lines are printed in the order they are written. Swap the last two lines and run it again to prove it.

Your turn

Add a line that prints a blank line between the second and third messages. (Hint: print() with nothing in the brackets.)

Step 3: Comments

Anything after a # is a comment. Python ignores it; it's there to explain the code to people, including you next week.

# This program introduces me
print("Hello, World!")   # a comment can also go at the end of a line

Step 4: The Castle Adventure title screen

Make a folder for the game called CastleAdventure, and in it a file called main.py. Type this program:

code/gcse/castle_adventure/step_01/main.py
"""The Castle Adventure - Step 1 (Chapter 1: Hello, World!)
0478 8.1.3: output with print()

A title screen. Run it and see the castle.
"""

# The r before the quotation marks makes this a "raw string":
# every backslash \ is printed exactly as it is typed.
print(r"""
        |>                                  |>
       _|_        _   _   _   _   _        _|_
      |___|      | |_| |_| |_| |_| |      |___|
      |   |______|                 |______|   |
      | # |      |      _____      |      | # |
      |   |  []  |     /     \     |  []  |   |
      |___|______|____|   |   |____|______|___|
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       T H E   C A S T L E   A D V E N T U R E
""")

print("Welcome to the Castle Adventure!")
print("Somewhere in this castle, the Dark Lord is waiting.")

What's happening?

The text between """ at the top is a docstring: a comment that describes the whole file. Chapter 12 explains more.
The picture is one long string that spreads over several lines. Three quotation marks, """, start and end a string that can include line breaks.
The r in r""" makes it a raw string. Normally Python treats a backslash \ as the start of a special code (for example, \n means "new line"). In a raw string every backslash is printed exactly as typed, which is what a picture needs.

Checkpoint

When you run main.py you see the castle, then the two welcome lines underneath.

Your turn

Design your own title picture for the game, or for a game of your own, using keyboard characters. Keep it under about 50 characters wide, and use a font where every character is the same width (your editor already does).

Step 5: Your first syntax error

Change the last line so it's missing its closing quotation mark, and run it:

print("Somewhere in this castle, the Dark Lord is waiting.)
Output
  File "main.py", line 22
    print("Somewhere in this castle, the Dark Lord is waiting.)
          ^
SyntaxError: unterminated string literal (detected at line 22)

Python refuses to run any of the program, not even the lines before the mistake. That's what makes it a syntax error: the program breaks the rules of the language, so Python can't understand it. The message tells you the line number, and the ^ points to where Python got confused. Put the quotation mark back and the program runs again.

The exact message depends on your Python version

The line number depends on exactly what you typed, and the wording varies a little between versions of Python. Chapter 0.4 shows how to read error messages in detail.

Python and Cambridge pseudocode

In Paper 2 of the Cambridge IGCSE, algorithms are written in pseudocode, in the style the syllabus sets out (0478 syllabus, section 4, Pseudocode). It looks quite different from Python, so each chapter shows the two side by side. (The last question on Paper 2, the 15-mark scenario question, may be answered in pseudocode or in Python.)

# this is a comment
print("Hello, World!")
// this is a comment
OUTPUT "Hello, World!"

Where the pseudocode comes from

The pseudocode in Part 1 follows the Pseudocode section of the Cambridge IGCSE Computer Science (0478) syllabus for 2026, 2027 and 2028 (version 6, section 4). Part 2 follows the 9618 Pseudocode Guide for Teachers, which is a little different. Cambridge updates its syllabuses from time to time, so if you are revising from this book, make sure your school uses the same version, or check the current one on the Cambridge International website.

Common mistakes

What you see What went wrong Fix
NameError: name 'Print' is not defined. Did you mean: 'print'? Python is case sensitive: Print isn't print Use lower case: print
SyntaxError: unterminated string literal A quotation mark is missing Every string needs one at each end
SyntaxError: '(' was never closed A bracket is missing Every ( needs a )
The picture looks wrong or lines break oddly The r before """ is missing, so backslashes are treated as codes Put r in front of the string
Nothing happens The file wasn't saved, or you ran a different file Save, and check the file name at the top of the editor

Check your understanding

  1. What is a string?
  2. What does sequence mean?
  3. What is the difference between a comment and a statement?
  4. Why does a syntax error stop the whole program from running, not just the broken line?
  5. Predict the output of this program:

    print("C")
    # print("A")
    print("B")
    
Answers
  1. A piece of text. In Python it's written inside quotation marks.
  2. Statements are carried out one after another, in the order they are written.
  3. A statement is an instruction the computer carries out; a comment is a note for people, which the computer ignores.
  4. Python checks that the whole program follows the rules before it runs any of it. If any part is broken, it can't understand the program, so none of it runs.
  5. C then B. The middle line is a comment, so it is ignored.

Exam-style question

A student writes this program:

print("Game starting")
print("Level 1")

(a) State the output of the program. [2]
(b) The student changes the first line to print(Game starting). Explain why the program no longer runs. [2]

(Answers are in the teacher edition.)

Summary

  • A program is a sequence of statements, carried out top to bottom.
  • print() outputs text. Text is a string and goes inside quotation marks.
  • # starts a comment, which Python ignores.
  • A syntax error breaks the rules of the language, so the program can't run at all. The error message gives the line number.
  • A raw string, r"""…""", prints backslashes exactly as typed, which is perfect for ASCII art.

Build it

Castle Adventure Step 1: code/gcse/castle_adventure/step_01/main.py. Next chapter: the game asks for your name.

This is a free sample: log in for every book →