Python is one of the most popular programming languages. In this guide, we’ll walk through the simplest possible example: printing “Hello, world!”.
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.
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:
print() is a built-in function used to display output"Hello, world!" is a string (text)() are used to pass arguments to the functionTwo useful functions in the interactive environment:
help()
quit()
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:
Linux/macOS:
echo $PATH
Windows (PowerShell):
$env:PATH
Windows (Command Prompt):
echo %PATH%
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.
#! tells the operating system this is a script/usr/bin/env python3 locates the python3 interpreter and runs the scriptNotes:
chmod +x)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:
You’ve now seen three ways to run Python code:
python)python helloworld.py)This is the simplest possible Python program—but it’s the starting point for everything else.