CodeRigor

LESSON

Write and run your first Python program

Write a program that prints lines of text, follow the order its statements run in, add comments and read a first error message.

A program is a list of instructions for a computer, written as text. Python is a programming language: a set of rules for writing those instructions so that a computer can follow them. To run a program is to ask the computer to carry the instructions out.

You do not need to install anything for these lessons. Every example with an Edit & run button runs right here on the page, and you can change it and run it again. Nothing you type can break anything.

One instruction, one line on the screen

Here is a complete Python program. Run it.

Python
print("Hello, world!")

It shows Hello, world! on the screen. The text a program shows is called its output.

Three things in that one line have names you will meet in every lesson:

  • print is a function: a named action that Python already knows how to do. This one shows text on the screen.
  • The round brackets after the name call the function, which means "do that action now".
  • "Hello, world!" is a string: a piece of text. The quotation marks tell Python where the text starts and stops. They are not part of the text, so they are not shown.

The value you put between the brackets is called an argument. It is what the function works on: here, the text to show.

From text you write to text on the screen
start / 3
You write the program as text. Python reads it one statement at a time and carries each one out. print puts a line on the screen.
From text you write to text on the screen
You write the program as text. Python reads it one statement at a time and carries each one out. print puts a line on the screen.

Statements run from top to bottom

A statement is one complete instruction. In Python a statement normally takes one line. A program with three statements does three things, always in the order they are written: first line first.

Python
print("Fill the kettle")
print("Boil the water")
print("Make the tea")

This prints three lines, in the same order as the statements:

Text
Fill the kettle
Boil the water
Make the tea

Step through the diagram to watch each line run. Then swap two lines in the program above and run it again: the output changes order too, because Python never looks ahead or rearranges your statements.

One statement at a time, top to bottom
start / 3 ⇄ code
Python finishes each statement before it starts the next one. Every print starts a new line on the screen.
One statement at a time, top to bottom
Python finishes each statement before it starts the next one. Every print starts a new line on the screen.

print() with nothing between the brackets prints an empty line. That is useful for spacing output out.

Comments are notes for people

A comment starts with #. Python ignores everything from the # to the end of that line. Comments are for the person reading the program, including you next week.

Python
# Show a two-line welcome.
print("Welcome!")
print()            # an empty line
print("Come in.")

This prints Welcome!, then an empty line, then Come in.. The comments print nothing.

A # inside the quotation marks of a string is ordinary text, not a comment: print("Room #4") prints Room #4.

Exactness matters

Python follows your text exactly, so small differences change the result:

  • Capital letters count. print works; Print is a different name that Python does not know.
  • Everything between the quotation marks is printed exactly, including spaces and punctuation. "Hello,world!" and "Hello, world!" are different strings.
  • Each string needs a quotation mark at both ends, and each call needs both brackets.

Read your first error message

Everyone who programs sees error messages every day. An error message is not a telling-off. It is Python saying which line it could not follow, and why. Learning to read one is a skill in its own right.

This program has a mistake: the closing quotation mark is missing.

Python
print("Hello, world!)

Python refuses to run it and reports:

Text
  File "main.py", line 1
    print("Hello, world!)
          ^
SyntaxError: unterminated string literal (detected at line 1)

Read it from the bottom up:

  1. The last line names the problem. SyntaxError means the text breaks Python's writing rules, so Python could not even start. "Unterminated string literal" means a string that was started but never ended.
  2. The ^ points at the place. Here it points at the opening quotation mark of the string that never ends.
  3. The first line gives the line number. line 1 tells you which line of your program to look at.

A second very common mistake is a misspelt name:

Python
prnt("Hello, world!")

This time the text follows the writing rules, so Python starts running it and stops at the line it cannot carry out:

Text
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    prnt("Hello, world!")
    ^^^^
NameError: name 'prnt' is not defined. Did you mean: 'print'?

NameError means Python met a name it does not know. Again the last line says what is wrong, the ^ marks show where, and the line number says which line. Python even suggests the fix.

When you see an error: read the last line, go to the line number, look where the ^ points, and compare that spot with a working example.

Running Python on your own computer

This is optional; the lessons do not need it. If you install Python from python.org, you can save a program in a text file named hello.py, open a terminal (a window where you type commands) in the same folder and type python3 hello.py (on Windows, python hello.py). The output appears in the terminal, the same as it does on this page.

What you can do now

You can write a program of several print statements, predict the order of its output, add comments, and find the line an error message is pointing at. In the exercises you will predict what small programs print, repair two broken ones and then write a three-line program of your own.

Next: name values with variables, so that a program can remember and calculate things.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Starts at the hello.py chapter: writing a first program, running it and what print does. Watch the first ten minutes (to 'VS Code and IDEs', 9:54) for this lesson; the rest of the lecture covers lessons 2, 3 and 6 of this unit.