Installing Python and running your first program
Install Python on Windows, macOS or Linux, confirm from the terminal that it actually works, set up VS Code, and run your first Python file on your own machine.
How do you want to learn?
The problem we are solving
You want to learn Python. There are plenty of sites where you can type code into a browser, but nobody writes code in a browser at work. At work, Python is installed on your own machine, you write code in an editor, you open a terminal and run commands, and you save files and run them.
So we will work that way from day one. By the end of this chapter you will have real Python on your computer, an editor to write code in, and you will have run a program with it.
By the end of this chapter you can
- Install the latest Python (3.12 or newer) on Windows, macOS or Linux
- Check from the terminal that the installation actually works
- Install VS Code, write a
.pyfile in it and run the file - Recognise and fix the most common installation mistakes
Prerequisites: none. This is the first chapter.
Step 1 — Install Python
Install the latest Python 3 — any version from 3.12 upwards is fine. The examples in this course were run on Python 3.12, and they work the same way on newer versions. Read the part for your operating system; you can skip the other two.
Windows
Go to python.org/downloads. The large yellow button at the top of the page gives you the installer for the latest Python 3 release. Download that one.
Do not install from the Microsoft Store. On Windows, typing python often opens the Store instead. The Store version runs in a restricted environment, which causes strange failures later when you install packages or write files next to your code. Use python.org.When the installer opens, there is a small checkbox at the bottom of the very first screen:
[ ] Add python.exe to PATHTick it. On Windows this is the most important step in the chapter.
PATH is the list Windows searches to decide which programs can be run from anywhere. Without it Python still installs, but typing python in a terminal gets you an error, because Windows does not know where Python is.
Then press Install Now.
macOS
Go to python.org/downloads. The large yellow button gives you the .pkg installer of the latest Python 3 for macOS. Open the file and press Continue and Install until it finishes. If it asks for a password, enter your Mac login password.
On macOS the command is called python3, not python. Wherever this course shows python, type python3 on a Mac.
Linux
Most Linux distributions (Ubuntu, Debian, Fedora) already come with Python 3. Check it in Step 2 below. If it is missing, install it with the package manager — on Ubuntu or Debian:
sudo apt install python3On Fedora:
sudo dnf install python3If your version is older than 3.12, a newer release of your distribution (for example Ubuntu 24.04 or later) has 3.12 or newer.
On Linux the command is also called python3. Wherever this course shows python, type python3 on Linux.
Step 2 — Verify
When the installation finishes, open a new terminal:
- Windows: press Start, type
powershell, and open PowerShell - macOS: press Cmd + Space, type
Terminal, and open Terminal - Linux: open Terminal from the app menu (Ctrl + Alt + T on Ubuntu)
Opening a new terminal matters. Any terminal that was already running still remembers the old PATH and will not see the new installation.
On Windows, type this command and press Enter:
python --versionOn macOS or Linux, type:
python3 --versionYou should see something like:
Python 3.12.3Your number may be different, for example starting with 3.13 or 3.14 — that is fine. As long as the first two parts are 3.12 or higher, you are ready.
Step 3 — Install VS Code
You need a code editor to write code. Plain editors such as Notepad or TextEdit can quietly add .txt to the end of a file name, and they do not colour your code or show mistakes. We will use VS Code: it is free, it runs on all three operating systems, and it is one of the editors used most at work.
- Go to code.visualstudio.com, click the download button for your operating system, and run the installer
- Open VS Code. Click the Extensions icon on the left (or press Ctrl + Shift + X on Windows and Linux, Cmd + Shift + X on macOS)
- Type
Pythonin the search box and install the Python extension made by Microsoft
Python runs without the extension, but with it VS Code colours your code and points out mistakes while you type.
Step 4 — Your first program
- Make a folder called
python-labin your home folder. On Windows the home folder isC:\Users\<name>, on macOS/Users/<name>, and on Linux/home/<name>—<name>stands for your user name - In VS Code, open the
python-labfolder with File → Open Folder - Press File → New File, name it
hello.py, and type this inside:
language = "Python"
print("Hello from", language)
print("2 + 3 =", 2 + 3)Save the file (Ctrl + S on Windows and Linux, Cmd + S on macOS). Then open a terminal inside VS Code: Terminal → New Terminal. This terminal opens in the python-lab folder by itself.
Now run the file. On Windows:
python hello.pyOn macOS or Linux:
python3 hello.pyOutput:
Hello from Python
2 + 3 = 5Your computer just ran a Python program. Not a browser, not a website — your own machine.
When it breaks
Typing python opens the Microsoft Store (Windows) Python is not on PATH, so Windows runs its own shortcut instead. Run the installer again, choose Modify, and tick "Add python.exe to PATH". Then open a new terminal.
python : The term 'python' is not recognized... (Windows) Same cause, same fix. You can also check whether py --version works — py is the Windows Python launcher and works even without PATH.
command not found: python (macOS or Linux) On macOS and Linux the command is called python3. Type python3 --version and python3 hello.py. If python3 is not found either, Python is not installed — go back to Step 1.
python hello.py says can't open file The terminal is in the wrong folder. Type ls and check that hello.py is in the list (ls works in Windows PowerShell too). If it is not, cd to the right folder, or open a new terminal in VS Code with Terminal → New Terminal.
The file is actually named hello.py.txt This happens when you save from Notepad or TextEdit, and because Windows hides file extensions by default, you do not see it. Create the file in VS Code. To see extensions on Windows, turn on View → Show → File name extensions in File Explorer, then rename the file.
Step 4 of 6 — Predict
Check your understanding
What appears in the terminal when this file is run with python hello.py?
language = "Python"
print("Hello from", language)
print("2 + 3 =", 2 + 3)- AHello from Python 2 + 3 = 5
- BHello from, Python 2 + 3 =, 5
- CHello from Python 2 + 3 = 2 + 3
- DHello from language 2 + 3 = 5
On Windows you installed Python, but python --version gives an error in PowerShell. What is the most likely cause?
- A"Add python.exe to PATH" was not ticked during installation
- BPython 3.12 does not run on Windows
- CPython does not work until the computer is restarted
- DYou must create a `.py` file first
One thing is missing compared with the chapter's example. What happens when this file runs?
language = Python
print("Hello from", language)- AHello from — followed by a blank
- BHello from language
- CAn error — without quotes Python reads `Python` as a variable name, and no such variable exists
- DHello from Python
Answering needs an account
Sign in to check your answers
The questions are above, and working them out in your head is the part that matters. Sign in to see the answers, the explanations and the three-level hints.
Your turn
Change hello.py so that it prints your own name and shows the result of 7 * 6. Save it and run it again — python hello.py on Windows, python3 hello.py on macOS or Linux.
The output should look something like:
Hello from <your name>
7 * 6 = 42There is more in this mission
What you read above is the complete mission — nothing was cut out of it. Pro adds:
- What actually happens underneath — how Python runs this
- The full solution to the exercise, worked step by step
- Harder extra exercises
- The interview question on this topic, with a model answer
Step 6 of 6
Stretch — the chapter quiz
Ten questions from easy to hard. The last ones are difficult on purpose.
Sign in to take the quiz