Installing Python and Setting Up Your Environment
What You Need to Get Started
Python is free and runs on Windows, Mac, and Linux. This tutorial walks you through installing Python, writing your first program, and setting up a code editor.
Installing Python
Go to python.org/downloads and download the latest stable version (3.11 or 3.12 are both fine).
On Windows: Run the installer. Check the box that says "Add Python to PATH" before clicking Install. This is important. Without it, you will not be able to run Python from the command line.
On Mac: Download the installer from python.org and run it. Alternatively, if you have Homebrew installed: brew install python.
On Linux: Python is often pre-installed. Check your version with python3 --version. If you need to install it: sudo apt install python3 (Ubuntu/Debian).
Checking the Installation
Open a terminal (Command Prompt on Windows, Terminal on Mac/Linux) and type:
python --version
# or on Mac/Linux if the above does not work:
python3 --version
You should see something like Python 3.11.4. If you get an error, the PATH was not set correctly. On Windows, reinstall and check the "Add to PATH" box.
The Python Interactive Shell (REPL)
Type python (or python3) in your terminal and press Enter. You will see a >>> prompt. This is the Python REPL (Read-Eval-Print Loop). You can type Python code here and see results immediately.
>>> 2 + 2
4
>>> print("Hello, world!")
Hello, world!
>>> exit()
Type exit() or press Ctrl+D to leave.
Your First Python Script
Create a file called hello.py in any folder. Open it in a text editor and type:
print("Hello, world!")
print("My name is Python.")
Save the file. In your terminal, navigate to the folder where you saved it and run:
python hello.py
You should see:
Hello, world!
My name is Python.
Congratulations. That is a Python program.
Setting Up VS Code
Visual Studio Code (VS Code) is the most popular free code editor for Python. Download and install it, then install the Python extension:
- Open VS Code
- Click the Extensions icon (four squares) in the left sidebar
- Search for "Python" and install the extension from Microsoft
- Open your
hello.pyfile and click the Run button (triangle) in the top right
VS Code will run your script and show the output in a terminal at the bottom.
Running Code in Jupyter Notebooks
Jupyter notebooks let you run code in small cells and see the output inline. They are great for learning and experimenting.
Install Jupyter:
pip install jupyter
Start a notebook:
jupyter notebook
Your browser will open. Click "New" and choose "Python 3" to create a notebook. Type code in a cell and press Shift+Enter to run it.
Comments
Use # to write comments in your code. Python ignores everything after # on a line.
# This is a comment
print("Hello") # This prints Hello
Comments explain your code to other people (and to your future self).
Discussion
Sign in to comment. Your account must be at least 1 day old.