Python Getting Started
Learn how to install Python and set up your development environment.
Python Install
Many PCs and Macs will have Python already installed. To check if you have Python installed on Windows, search in the start bar for Python or run the following on the Command Line (cmd.exe):
C:\Users\Your Name>python --versionTo check if you have Python installed on Linux or Mac, then on Linux open the command line or on Mac open the Terminal and type:
python --versionIf you find that you do not have Python installed on your computer, then you can download it for free from the following website: https://www.python.org/
Python Quickstart
Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the Python interpreter to be executed.
The way to run a Python file is like this on the command line:
C:\Users\Your Name>python helloworld.pyWhere "helloworld.py" is the name of your Python file.
Let's write our first Python file, called helloworld.py, which can be done in any text editor.
helloworld.py
print("Hello, World!")Simple as that. Save your file. Open your command line, navigate to the directory where you saved your file, and run:
C:\Users\Your Name>python helloworld.pyThe output should read:
Hello, World!The Python Command Line
To test a short amount of code in Python sometimes it is quickest and easiest not to write the code in a file. This is made possible because Python can be run as a command line itself.
Type the following on the Windows, Mac or Linux command line:
C:\Users\Your Name>pythonOr, if the "python" command did not work, you can try "py":
C:\Users\Your Name>pyFrom there you can write any Python, including our hello world example from earlier in the tutorial:
C:\Users\Your Name>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")
Hello, World!Whenever you are done in the Python command line, you can simply type the following to quit the Python command line interface:
exit()Four Ways to Run Python
| Method | Command | Best for |
|---|---|---|
| Run a script | python app.py | Real programs |
| Interactive shell (REPL) | python | Quick experiments |
| One-liner | python -c "print(2+2)" | Tiny tasks / scripts |
| Run a module | python -m http.server | Built-in tools |
On Windows the launcher is often py instead of python (e.g. py --version). On macOS/Linux you may need python3.
The Interactive Shell
Type python to enter the REPL — it evaluates each line and prints the result immediately, perfect for testing ideas. Exit with exit() or Ctrl+Z (Windows) / Ctrl+D (Mac/Linux).
>>> 5 * 3
15
>>> name = "Sam"
>>> f"Hi {name}"
'Hi Sam'Try It Yourself
Exercise 1: Create a file hello.py that prints Hello, Python! and run it from the terminal.
Show solution
# hello.py
print("Hello, Python!")
# terminal:
# python hello.pyExercise 2: Use a one-liner (no file) to print the result of 12 * 12.
Show solution
python -c "print(12 * 12)"Key Takeaways
- Run scripts with
python file.py; experiment in the REPL withpython. - Use
pyon Windows andpython3where needed. python -mruns installed modules as tools.
📘 Real-World Deep Dive
Picking the right Python interpreter, virtualenv, and package manager up front prevents the classic "works on my machine" bug. The two-line real-life example below is what almost every production repo starts with.
Real-Life Scenario
Setting up a brand-new project with an isolated environment and a pinned dependency list.
Real-Life Example
# From a fresh checkout, the canonical start-up sequence is:
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install --upgrade pip
pip install -r requirements.txt
python -c "import sys; print(sys.version)"Expected Output
3.12.4 (main, Jun 6 2024, 18:26:37) [Clang 15.0.0]Common mistakes
- Committing the
.venvdirectory blows up the repo — always add it to.gitignore. - Using the system Python instead of the venv's interpreter silently imports the wrong packages.
pip install packagewithout a pinned version produces non-reproducible deploys — pin or useuv/poetry.
🚀 Performance & Best Practices
uv(from Astral) is 10–100× faster thanpipfor resolution and install; consider it for new projects.- Use
python -m pip …rather than barepip …to guarantee the right interpreter is used. - A
requirements.txtwith hashes blocks supply-chain attacks.
🧪 Try It Yourself
- Bootstrap a project with
uv initand compare timings against the venv path above. - Add a
Makefiletargetsetupthat runs the four commands idempotently. - Generate a
requirements.txtfrom an active env withpip freeze.