Setting Up Python on Your Computer
Install Python and a code editor on Windows, Mac, or Linux so you are ready to write and run your first programs.
Two things you need
To write Python you need two things. First, Python itself, so your computer can understand and run Python code. Second, a code editor, which is the program where you write your code comfortably.
Go to python.org, click the big yellow Download Python button, and run the installer. The website suggests the right version for your computer, so you do not have to guess.
On a Mac you can also run brew install python3 in the terminal if you use Homebrew. On Linux, Python 3 is usually already there. You can check with python3 --version, which we cover in the next card.
Did it install correctly?
Never assume an install worked. Check it. Open a terminal (Mac or Linux) or Command Prompt (Windows) and type:
python --version
On Mac or Linux, try this instead, because the command is often named python3:
python3 --version
You should see something like this:
Python 3.12.0
Your number may be different, and that is fine as long as it starts with 3. If a version number shows up, Python is installed and working.
A terminal is just a place where you type commands instead of clicking buttons. It looks plain, but it is how programmers talk to the computer. You will use it every time you run a Python file.
Pick a code editor
An editor colors your code, catches mistakes, and offers auto-complete. You can write Python in Notepad, but a proper editor makes life much easier. For example, if you forget to close a bracket, the editor can underline the problem before you even run the code. Two free options:
- VS Code: free, fast, works on Windows, Mac and Linux, and the most popular editor right now. Download it from code.visualstudio.com, then open the Extensions icon on the left, search for Python and install the one by Microsoft.
- PyCharm Community Edition: free and made just for Python, with more Python features built in. Get it from jetbrains.com/pycharm and choose Community.
Not sure which one? Go with VS Code. It is lighter, and you can use it later for other languages too. Switching editors later is easy because your code files are just plain text.
Your first program
Open your editor, create a new file and save it as hello.py. The .py at the end tells the computer it is a Python file. Type this:
print("Hello, World!")
Save it, open a terminal, go to the folder where you saved it, and run:
python hello.py
You should see:
Hello, World!
That is it. You just wrote and ran your first Python program. Welcome to programming.
Here is what happened. You asked Python (python) to read your file (hello.py). Python found one instruction, print(...), and did what it said: it showed the text between the quotes on the screen.
The interactive shell
Python also has an interactive mode for testing small things without making a file. Type python (or python3 on Mac and Linux) in a terminal and you get a prompt that looks like >>>. Type code, press Enter, and it runs right away.
>>> print("Hello from the shell")
Hello from the shell
>>> 2 + 2
4
Notice that you did not need print for 2 + 2. In the shell, Python shows the answer of anything you type. That only works in the shell. Inside a .py file you must use print if you want to see a result.
To leave the shell, type exit() and press Enter.
When something goes wrong
Almost everyone hits a small problem on day one. Here are the most common ones and how to fix them:
- "python is not recognized" on Windows: you probably missed Add Python to PATH. Run the installer again, choose Modify or Repair, and tick that box. You can also try the
pycommand instead ofpython. - "command not found: python" on Mac or Linux: use
python3instead ofpython. - "No such file or directory" or "can't open file": your terminal is in a different folder than your file. Move into the right folder, then try again.
- You see a version starting with 2: an old Python is installed. Use
python3or install the newest version from python.org.
Do not worry if you get stuck. Copy the error message, paste it into a search engine, and you will almost always find the answer. That is what working programmers do too.
Finding your file in the terminal
To run hello.py, the terminal has to be inside the folder that holds it. Two commands help with that:
cd folder_namemoves into a folder.cd ..goes back up one level.ls(Mac and Linux) ordir(Windows) lists the files in the current folder, so you can check thathello.pyis really there.
cd Documents
cd python-course
ls
python3 hello.py
Many editors, including VS Code, have a built-in terminal that already opens in your project folder. That is the easiest way for a beginner, because you skip the cd steps.
Key takeaways
- You need Python and a code editor. VS Code is a great free choice
- Windows users must tick Add Python to PATH during install
python --version(orpython3 --version) checks that the install worked- Save code in a
.pyfile and run it withpython hello.py - The shell (
>>>) runs one line at a time. Leave it withexit() - If something fails, read the error message. It is usually a PATH problem or the wrong folder