Cao Yi

Hello World in Python

Index

Python is one of the most popular programming languages. In this guide, we’ll walk through the simplest possible example: printing “Hello, world!”.


Install Python

Download Python from the official website and install it:

Python is cross-platform and works on Windows, Linux, and macOS.

After installation, open a terminal to verify it:

Run:

python --version
python3 --version
py --version   # Windows only

Depending on your system, you may use python, python3, or py. If Python is installed correctly, the command will print the installed version.


Say Hello in the Terminal

Start the Python interactive environment:

python

(or python3/py depending on your operating system.)

You will see a prompt like:

>>>

Then type:

print("Hello, world!")

Press Enter, and you will see:

Hello, world!

Explanation:

Two useful functions in the interactive environment:

help()
quit()

Save the Script to a File

Instead of typing code interactively, you can save it in a file.

Create a file named helloworld.py:

print("Hello, world!")

Run it in the terminal:

$ python helloworld.py
Hello, world!

Or:

$ python3 helloworld.py
Hello, world!

On Windows:

> py helloworld.py
Hello, world!

Here, python (or python3 / py) is the Python interpreter. It works because the Python executable is included in your system’s PATH, which allows the terminal to locate and run it.

You can inspect PATH using:


Shebang (Optional)

On Linux and macOS, you can make a script executable by adding a shebang line:

#!/usr/bin/env python3
print("Hello, world!")

Then make the file executable:

chmod +x helloworld.py

Run it directly:

./helloworld.py

(Run this in the same directory as the file.)

Windows will ignore the shebang line but it is usually left in for portability.

What is a shebang?

Notes:


File Name Suffix

Python files usually use the .py suffix:

helloworld.py

This is a convention, not a requirement. Python can run files with any name, but using .py is strongly recommended for:


Summary

You’ve now seen three ways to run Python code:

  1. Interactive mode (python)
  2. Script file (python helloworld.py)
  3. Executable script with shebang (Linux/macOS)

This is the simplest possible Python program—but it’s the starting point for everything else.