Write and Run Your First Python Program
How to write a first Python program and run it on Windows, macOS, and Linux — creating the file, the python vs python3 vs py command differences, making a script directly executable, and common first-run mistakes.
1.1 Writing the Program
Choosing an Editor.
Any plain-text editor works — VS Code, PyCharm, IDLE, or even Notepad — since a .py file is just text (see Installation of Python, Section 1.2, for a comparison of editors). No project scaffolding or build configuration is needed to get started.
Creating the File.
Create a new file named hello.py and save the following single line into it:
print("Hello, World!")
Why This Line Works.
print() is a built-in function that writes its argument to standard output. The text "Hello, World!" is a string literal — Python accepts both double quotes and single quotes for strings, as long as the opening and closing quote match. There’s no semicolon at the end of the line and no enclosing braces for the file — line breaks and indentation are what Python uses to separate statements, not punctuation.
Saving the File.
The .py extension itself has no effect on execution (the interpreter reads plain text either way) — it exists purely as a convention so editors, IDEs, and other tools recognize the file as Python source.
1.2 Running on Windows
Step 1 — Open a Terminal.
Open Command Prompt or PowerShell.
Step 2 — Navigate to the File.
C:\> cd C:\Users\yourname\projects
Step 3 — Run the Script.
C:\Users\yourname\projects> python hello.py
Hello, World!
The py Launcher.
Windows installs also ship a py launcher, which picks the correct installed Python version automatically — useful when multiple versions are installed side by side.
C:\Users\yourname\projects> py hello.py
Hello, World!
C:\Users\yourname\projects> py -3.12 hello.py
Hello, World!
If python isn’t recognized at all, that’s almost always the PATH issue covered in Installation of Python, Section 1.10.
1.3 Running on macOS
Step 1 — Open Terminal.
Open the Terminal app (Applications → Utilities → Terminal, or search via Spotlight).
Step 2 — Navigate to the File.
$ cd ~/projects
Step 3 — Run the Script.
$ python3 hello.py
Hello, World!
Why python3, Not python.
Current macOS releases don’t ship a python command at all out of the box — only python3, once installed via python.org or Homebrew (Installation of Python, Section 1.1). Typing python on a fresh Mac typically gives command not found, which is expected, not a broken install.
1.4 Running on Linux
Step 1 — Open a Terminal.
Open the terminal application for your distribution (varies by desktop environment).
Step 2 — Navigate to the File.
$ cd ~/projects
Step 3 — Run the Script.
$ python3 hello.py
Hello, World!
python3 vs python.
Most modern distributions provide python3 only. On older systems where both python (Python 2) and python3 are present, use python3 explicitly — relying on the bare python command risks silently running under Python 2, which reached end-of-life in January 2020.
1.5 Running Directly as an Executable (Linux/macOS)
On Linux and macOS, a script can be run without typing python3 in front of it, by telling the shell which interpreter to use and marking the file executable.
Step 1 — Add a Shebang Line.
#!/usr/bin/env python3
print("Hello, World!")
The shebang (#!) must be the very first line of the file. /usr/bin/env python3 tells the shell to locate whichever python3 is first on the current PATH, rather than hardcoding one absolute path — more portable across machines.
Step 2 — Make the File Executable.
$ chmod +x hello.py
Step 3 — Run It.
$ ./hello.py
Hello, World!
This pattern isn’t Windows-specific behavior — Windows has no concept of a Unix-style executable bit, so on Windows a script is always run by passing it to python/py as shown in Section 1.2.
1.6 Running from an IDE
Instead of switching to a terminal, most IDEs run the current file with a button click, using the same interpreter and command under the hood:
- VS Code: open
hello.py, click the ▷ Run button in the top-right, or pressCtrl+F5(Cmd+F5on macOS). Output appears in the integrated terminal panel. - PyCharm: right-click inside the editor and choose Run ‘hello’, or click the green ▷ next to the
if __name__block or file tab.
Both are running python3 hello.py behind the scenes — see Installation of Python, Section 1.3, for the other execution methods (REPL, -c flag).
1.7 Verifying the Output
Regardless of OS or method, a correctly written and executed hello.py produces identical output:
$ python3 hello.py
Hello, World!
If nothing prints, or an error appears instead, work through Section 1.8 below before assuming the code itself is wrong.
1.8 Common Mistakes When Running a First Program
Wrong Working Directory.
Running python3 hello.py from a directory that doesn’t contain the file produces can't open file 'hello.py': [Errno 2] No such file or directory. Fix: cd into the file’s directory first, or pass its full path.
Mismatched Quotes.
print("Hello, World!') — opening with " and closing with ' — raises SyntaxError: unterminated string literal. Fix: the opening and closing quote character must match.
Wrong Extension or No Extension.
Saving as hello.txt or hello (no extension) still runs fine if passed explicitly to python3, since the interpreter doesn’t require .py — but omit it and most editors won’t apply Python syntax highlighting or linting, and other tools (like import) won’t recognize the file as a module.
Using python Instead of python3.
On Linux/macOS systems where python still maps to Python 2 (or doesn’t exist at all), running python hello.py either fails outright or, worse, silently runs under the wrong major version. Always use python3 explicitly unless you’ve confirmed what python resolves to (Installation of Python, Section 1.10).
Case-Sensitive Filenames.
Hello.py and hello.py are different files on Linux and macOS (case-sensitive filesystems) but the same file on Windows by default — a script that runs fine on a developer’s Windows machine can fail with “file not found” once deployed to a Linux server.
1.9 Interview Questions
Beginner Interview Questions:
- How do you write and save a basic Python program?
- What command is used to run a Python script from the terminal?
- What is the difference between
pythonandpython3on the command line? - Why does a
.pyfile need matching quote characters around a string?
Frequently Asked Concepts:
- Why might
python hello.pyfail on macOS or Linux but work on Windows? - What does the shebang line (
#!/usr/bin/env python3) do, and why useenvinstead of a hardcoded path? - What’s the difference between running a script directly (
./hello.py) and running it via the interpreter (python3 hello.py)? - Why is the
.pyextension a convention rather than a strict requirement for execution?
Quick Interview Answer
“Writing a first Python program is just saving a
.pytext file containing valid Python, likeprint("Hello, World!"). Running it ispython3 hello.pyon macOS/Linux, orpython hello.py/py hello.pyon Windows — all three invoke the same interpreter pipeline. On Linux and macOS a script can also be made directly executable with a#!/usr/bin/env python3shebang line pluschmod +x, letting you run it as./hello.pywithout typing the interpreter name.”
Common Mistakes
- Running the command from the wrong directory and getting a “file not found” error unrelated to the code itself.
- Using
pythoninstead ofpython3on Linux/macOS and hitting a missing command or an unexpected Python 2 execution. - Forgetting
chmod +xbefore trying to run a script directly as./hello.pyon Linux/macOS. - Assuming filenames are case-insensitive everywhere — true on Windows by default, false on Linux and macOS.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form