LearnContact
Lesson 418 min read

Your First Python Program

Write, run, and understand your very first complete Python program, and learn how to read the error messages Python shows when something goes wrong.

Introduction

With Python installed and an editor ready, it is time for a rite of passage every programmer goes through: writing your first program. By tradition, that program simply displays the words "Hello, World!" on the screen.

This lesson walks through writing that program, running it, understanding what actually happens when Python executes it, and — just as importantly — learning to read the error messages you will inevitably see as a beginner.

What You Will Learn
  • How to write and save a Python program.
  • How to run a .py file from the terminal.
  • What happens internally when Python runs your code.
  • How to print multiple values and do simple math.
  • How to read and understand a Python traceback.

Writing hello.py

Open your editor, create a new file named hello.py, and type the following single line exactly as shown, including the quotation marks and parentheses.

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

print() is a built-in function that displays whatever is inside its parentheses on the screen. The text "Hello, World!" is called a string — text data surrounded by quotation marks.

Save the file in a folder you can easily find from the terminal, such as a "python-practice" folder on your Desktop.

Running Your Program

Open a terminal, navigate to the folder where hello.py is saved, and run it using the python command.

Terminal
cd python-practice
python hello.py
Output
Hello, World!
Try It Yourself
  • Create hello.py with the exact code shown above.
  • Open a terminal in the same folder.
  • Run python hello.py (or python3 hello.py).
  • Confirm you see "Hello, World!" printed.

What Happens When You Run It

When you type python hello.py, the Python interpreter opens the file, reads it from the top, and executes each line in order. For a one-line program, it simply runs that single print() statement and then stops.

For programs with multiple lines, Python executes them one at a time, from top to bottom, unless a specific instruction (like a loop, function call, or condition — covered in later lessons) tells it to do otherwise.

Read the File

The interpreter opens and reads hello.py as plain text.

Parse the Code

Python checks the code follows valid syntax before running anything.

Execute Top to Bottom

Each line runs in order, one at a time.

Produce Output

Anything passed to print() appears in the terminal.

Example: Printing Multiple Things

A program is not limited to one print() call — you can call it as many times as you like, and each call appears on its own new line by default.

greetings.py
print("Hello, World!")
print("Welcome to Python.")
print("Let's start coding!")
Output
Hello, World!
Welcome to Python.
Let's start coding!

Example: Simple Math

print() is not limited to text — it can also display the result of a calculation directly.

math.py
print(10 + 5)
print(10 - 5)
print(10 * 5)
print(10 / 5)
Output
15
5
50
2.0

Notice that division (/) produces a decimal result (2.0) even when the numbers divide evenly — this is normal, expected behavior in Python 3.

Common First-Run Errors

Beginners almost always hit a handful of predictable errors in their first few programs. Seeing them now, on purpose, makes them far less scary later.

Missing quotes
print(Hello, World!)
Output
SyntaxError: invalid syntax

Without quotation marks, Python tries to treat Hello as the name of a variable, but the punctuation around it makes the line invalid — resulting in a SyntaxError.

Using an undefined name
print(message)
Output
NameError: name 'message' is not defined

This happens when you reference a variable that was never created. Python has no idea what message refers to, so it raises a NameError.

Reading a Traceback

When something goes wrong, Python does not just fail silently — it prints a traceback, a report showing exactly where and why the error happened. Learning to read tracebacks is one of the most valuable early skills in programming.

Example traceback
Traceback (most recent call last):
  File "hello.py", line 1, in <module>
    print(message)
NameError: name 'message' is not defined

Read a traceback from the bottom up: the last line names the error type (NameError) and gives a short explanation. The lines above show the file and line number where it happened, so you always know exactly where to look first.

File & Line Number

Tells you exactly which file and line caused the problem.

Error Type

The last line names the specific kind of error, like SyntaxError or NameError.

Error Message

A short, usually helpful explanation follows the error type.

Common Mistakes

Avoid These Mistakes
  • Forgetting quotation marks around text passed to print().
  • Mismatched or missing parentheses.
  • Referring to a variable name that was never created (NameError).
  • Inconsistent indentation causing an IndentationError (covered fully in the next lesson).
  • Ignoring the traceback instead of reading what it says.

Best Practices

  • Type code out yourself rather than copy-pasting, to build muscle memory.
  • Read every error message fully before assuming what went wrong.
  • Run your program after every small change, not just at the end.
  • Save files with a .py extension and a clear, descriptive name.

Frequently Asked Questions

Why is "Hello, World!" the traditional first program?

It is a simple way to confirm your setup works end-to-end — writing, saving, and running code — before learning more complex concepts.

What is the difference between a SyntaxError and a NameError?

A SyntaxError means Python cannot even understand the structure of your code. A NameError means the code is structurally valid but refers to something (like a variable) that does not exist.

Do I always need to read the whole traceback?

Not always, but the last line (error type and message) and the line number are the most important parts to check first.

Can a Python program have more than one error?

Python normally stops and reports the very first error it finds; you fix that one, run again, and repeat until the program runs cleanly.

Is it normal to see errors constantly as a beginner?

Yes, completely normal. Every programmer, no matter how experienced, sees errors regularly — reading them carefully is a core skill, not a sign of failure.

Key Takeaways

  • print() displays text or values on the screen.
  • Python executes a program's lines in order, from top to bottom.
  • Text values (strings) must be wrapped in quotation marks.
  • A NameError means you used a name Python does not recognize.
  • Tracebacks show the error type, message, file, and line number — read them from the bottom up.

Summary

Writing your first Python program is a small step technically, but an important milestone — it proves your entire setup works, from writing code, to saving it, to running it and reading whatever Python tells you back.

In this lesson, you wrote and ran hello.py, printed multiple values and simple calculations, and learned to read common beginner errors and tracebacks. Next, you will learn the rules that define how Python code must be structured.

Lesson 4 Completed
  • You have written and run your first Python program.
  • You understand how Python executes code top to bottom.
  • You have seen common beginner errors on purpose.
  • You can read a basic Python traceback.
Next Lesson →

Python Syntax