Scrollable Nav Bar

First Python Program (Hello, World!)

Every programming journey starts the same way: we write a tiny program, run it successfully, and prove to ourselves that our setup works. In Python, that first win is usually Hello, World!. It may look simple, but this one exercise teaches us the real workflow we’ll use again and again—create a file, write code, save it, and run it from the terminal.

In this tutorial, we’ll create a Python file named hello.py, write our first line of code, add a comment, and execute the program correctly.

Printing the Hello, World! Message

We will create a small script that prints a message in the terminal:

Hello, World!

This might feel small, but it’s the foundation. Once we can run a file confidently, we can start building larger programs the same way.


What we need before we start

We only need two things: a text editor to write the file and a terminal/command prompt to run it. A text editor is different from a word processor—our editor should save plain text and highlight code.

If we don’t have an editor yet, we should choose one and stick with it while learning. Any of these work well:

  • Visual Studio Code (VS Code)
  • Sublime Text
  • PyCharm
  • Notepad++ (popular on Windows)
Visual Studio

Step 1: Create a new Python file

Create a new file and name it exactly:

hello.py

The .py extension tells our computer (and us) that this file contains Python code. Keeping file names simple is a good habit early on, because it reduces mistakes when we run the file from the terminal.

Open hello.py in our editor so we can start typing.


Step 2: Write our first line of Python code

Inside hello.py, write this single line:

print('Hello, World!')

This line uses Python’s built-in print() function. The job of print() is straightforward: it takes the value we provide and displays it in the terminal.

The part inside quotes—'Hello, World!'—is a string, which is just a fancy word for text. The quotes matter because they tell Python: “treat this as text, not as a command.” Without quotes, Python would assume we’re referring to a variable name or something else.

Python allows both single quotes ('...') and double quotes ("...") for strings. For now, we should pick one style and be consistent. In this example, we’re using single quotes.


Step 3: Add a comment to make our code readable

Now leave a little space and add a comment under our print statement:

# print hello, world! to the terminal

A comment is a note written for humans. Python ignores comments completely when it runs our program. Comments are useful when we want to explain what a section does, leave reminders, or make our code easier to understand when we revisit it later.

The key rule is simple: **a comment begins with **“.


Step 4: Save the file

Save hello.py in our editor. This is an easy step to forget in the beginning, and it’s the most common reason we might run an old version of our code.

A good habit is: after every change, hit save before switching to the terminal.


Step 5: Run the program

We have two ways to run our program:

Option 1: Run directly in the editor (Visual Studio Code example)

  • Open the terminal inside VS Code (View → Terminal).
  • Make sure the file hello.py is open and selected.
  • Click the Run button or type python hello.py in the terminal.
  • The output will appear in the terminal panel inside VS Code.

Option 2: Run from an external terminal

  • Open your terminal (Command Prompt, PowerShell, or macOS/Linux Terminal).
  • Navigate to the folder containing hello.py using the cd command:
cd \Users\Admin\Desktop\Python Exercises>
  • Use Tab auto-complete to avoid typing mistakes.
  • Run the program with:
cd \Users\Admin\Desktop\Python Exercises> python hello.py

or

python3 hello.pyCode language: CSS (css)

Use Tab to avoid typing long paths

Most terminals support Tab auto-complete. Start typing a folder name, press Tab, and the terminal will try to complete it for us. This is not just faster—it prevents spelling mistakes.

Step 6: Observe the output

When run correctly, we’ll see:

Hello, World!

Our file is executed top-to-bottom by Python. It reads the first line, sees the print() call, and displays the message.


Running a file vs. opening the Python prompt

Beginners often mix up these two actions:

  1. Typing python alone starts an interactive Python prompt (we can type commands one by one).
  2. Typing python hello.py runs our file as a complete program.

For building scripts and real projects, we will mostly run files.


Our complete hello.py file

print('Hello, World!')

# print hello, world! to the terminal

Common mistakes and quick fixes

If our output didn’t appear, it’s usually one of these simple issues:

  • We’re in the wrong folder: navigate again with cd and make sure hello.py is there.
  • We forgot to save: save the file and run the command again.
  • Python isn’t recognized: Python may not be installed correctly or not added to PATH (common on Windows).
  • We typed the filename wrong: use Tab auto-complete to avoid mistakes.