Skip to content
BENIBIX Learn

Hello World in Python

Level 1 · What a program is

Your first Python script, the Hello World program. Why you plan in pseudocode first, and why syntax has to be exactly right.

Course contentsLesson 2 of 18

After this lesson you can

  • Write and run a Hello World script
  • Plan a program in pseudocode
  • Explain why syntax must be exact

10 mincovers 4 glossary entries

Worth reading first: Programs and Source Code

How to write and run your first Python script

The first program most people write is Hello World. It prints the words Hello, world! and stops. In Python it is one line: print("Hello, world!"). A 1978 book on the C language made it the usual first program. It proves your tools work before you try anything harder.

You save that line in a text file whose name ends in .py, such as hello.py. That file is a script: a program that runs straight from its text, with no build step first. You run it by typing python hello.py, and the words appear underneath.

Before you write code, it helps to write the plan in plain words. That plan is called pseudocode. It might say: ask for a name, then say hello to that name. No computer runs pseudocode. It is for you, so you can think about the steps before the rules of a language get in the way.

Code, unlike pseudocode, has strict rules. Those rules are the syntax: which signs go where, and in what order. In Python, text goes inside quote marks and the brackets after print must open and close. Miss one quote mark and Python stops before it runs a single line.

That strictness surprises people. A person reading print("Hello) can guess what you meant. Python cannot guess. It reads the text exactly as written, and it tells you the line where the rules broke. Reading that message is a skill you will use every day.

Python itself is free, and you can get it from python.org. Its creator first shared it in February 1991. The name comes from a British comedy show, not the snake. Today it runs on Windows, macOS and Linux.

The common mistake is to skip the first run. Type the one line, run it, and see the words. When something later goes wrong, you then know the tools were fine and the problem is in your new code.

python
# hello.py
print("Hello, world!")

# In the terminal:
# $ python hello.py
# Hello, world!
One line of code, saved as hello.py and run. The lines that start with # are notes Python skips.
Plan it in words, write it as a script, then run it. The printed line proves the tools work.

Worked example

Jack's first script will not run

Jack types print("Hello, world!) and runs it. Python stops at line 1 and reports a problem with the text.

He compares it with the example and finds the missing quote mark after the !. He adds it, runs the file again, and the words appear. The fix took 1 character.

The words for this lesson4

Check yourself

Why does print("Hello) not run in Python?

Finished this lesson?

Marking it complete also marks its 4 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.