RizTech Academy logo
RizTech Academy
Getting StartedLesson 4 of 515 min

Your first program, and what happens when you run it

Time to write something that runs. We will write the traditional first program, then immediately take it apart, because the interesting part is not the program — it is understanding exactly what happened when you ran it.

Write it

In your python-course folder, create a file called hello.py. In VS Code: right-click in the file explorer on the left, choose New File, type hello.py, press Enter.

The .py extension matters. It is how your editor knows to treat this as Python, and how you know too.

Type this in — do not copy and paste it:

print("Hello, world!")

Typing it yourself sounds like pointless ceremony for one line. It is not. Typing is how you notice the quotes and the brackets; copying is how you end up three lessons later unsure why your own code does not run. Type the examples throughout this course.

Save with Ctrl + S.

Run it

Open the terminal in VS Code (Ctrl + ` , the backtick key above Tab) and run:

python hello.py

On macOS and Linux, python3 hello.py.

You should see:

Hello, world!

That is a working program. It is small, but nothing about it is fake — this is genuinely how Python programs are run, including ones that are forty thousand lines long.

What actually happened

Five things, in order:

  1. You typed python hello.py and pressed Enter.
  2. Your terminal found the Python interpreter and started it, handing it the name of your file.
  3. Python read hello.py from top to bottom.
  4. It found one instruction — print(...) — and carried it out, which meant writing text to the terminal.
  5. It reached the end of the file, decided it had nothing left to do, and exited.

Step 5 is worth holding on to. Python runs your file and then stops. It does not wait. Later, when you write a program that seems to "do nothing and close instantly", this is why — it did everything you asked, and what you asked was not much.

Taking the line apart

print("Hello, world!")

print is a function: a named piece of behaviour that already exists. Python comes with about seventy of these built in, and print writes text to the screen.

The brackets ( ) mean "run this function now". Writing print on its own does not print anything — it just refers to the function without calling it. The brackets are the instruction to actually do it.

"Hello, world!" is a string: text. The quotes are not part of the text. They are markers telling Python where the text starts and ends. Without them Python would read Hello as the name of something and complain it had never heard of it.

Single quotes work identically:

print('Hello, world!')

Pick one style and stay consistent. This course uses double quotes.

Breaking it on purpose

This is the most useful part of the lesson. Each of these is an error you will make by accident, and seeing it now — deliberately, when you know the cause — makes it recognisable later.

Try each one, run it, read the message, then fix it.

Forget the closing bracket:

print("Hello, world!"
SyntaxError: '(' was never closed

Forget the quotes:

print(Hello, world!)
SyntaxError: invalid syntax

Misspell the function:

prnt("Hello, world!")
NameError: name 'prnt' is not defined

That third message is Python being precise, not obtuse. NameError means "you used a name I do not recognise". It is the same message you get for a typo'd variable, and once you read it that way it is genuinely helpful.

Add a space at the start of the line:

 print("Hello, world!")
IndentationError: unexpected indent

In most languages that leading space is meaningless. In Python, indentation carries meaning — it shows what is inside what. At the top level of a file, nothing should be indented.

Printing more than one thing

print takes several values, separated by commas, and puts a space between them:

print("Hello,", "world!")
print("2 + 2 =", 2 + 2)
Hello, world!
2 + 2 = 4

Two things are happening in that second line. "2 + 2 =" is text and is printed exactly as written. 2 + 2 has no quotes, so Python treats it as arithmetic, works out 4, and prints the result. Quotes are the difference between text and a calculation.

Comments

A line beginning with # is ignored by Python:

# This explains something to a human.
print("Hello, world!")  # This does too.

Use comments to record why something is written the way it is. Avoid comments that restate the code — # print hello above a line that prints hello helps nobody, and goes stale the moment the code changes.

Practice

  1. Create hello.py and print your own name.
  2. Print your name and age on one line, using a comma inside print.
  3. Make Python calculate and print how many days old you are, roughly. Use 365 * 25 with your own number — you are allowed to be approximate.
  4. Cause all four errors above deliberately. Read each message. Say out loud what it is telling you before you fix it.

Step 4 is the one that matters. Errors are the main way Python communicates with you, and they only feel hostile while they are unfamiliar. Spending ten minutes now making them happen on purpose is worth hours later.

Next: the REPL — a faster way to test an idea than writing a file every time.

Stuck on this lesson?

Being stuck is part of it — but being stuck alone for three days is not. Our internship programme pairs this curriculum with code review and one-to-one help from working developers, and it is free.

About the internship