Python · Chapter 1 of 21
Install Python, run a script and set up a project
14 min study · Examples checked with Python 3.14 and 3.15 ·
Python is an interpreted language: you write plain-text source files, and a program called the interpreter reads and executes them. The same file runs on Windows, macOS and Linux as long as an interpreter is installed. This chapter gets that interpreter in place, runs a first program, shows how to read the error report Python prints when something goes wrong, and gives a project its own set of third-party packages.
Install an interpreter and confirm which one you run
The course uses Python 3.14 or newer, and the examples were checked with 3.14 and 3.15. Download a current release from python.org. On Windows, the Python install manager provides the py launcher; on macOS, use the official installer or a version manager. Linux distributions ship their own Python, which system tools depend on, so leave it alone and install a newer version next to it when you need one.
Open a terminal and ask two questions: which version runs, and where it lives.
# macOS or Linux
python3 --version
python3 -c "import sys; print(sys.executable)"
# Windows
py --version
py -c "import sys; print(sys.executable)"
The version tells you which language features are available. The path tells you which installation answered, and that matters more often than beginners expect. A computer can have several interpreters, and two terminals, or a terminal and an editor, can pick different ones. When a package you just installed “isn’t there”, the cause is almost always that it went into a different interpreter than the one running your code.
Write and run a first program
Make a folder called learning-python and save this as hello.py, in UTF-8:
learner = "Ada"
lessons_done = 3
lessons_total = 21
print(f"Hello, {learner}!")
print(f"You have finished {lessons_done} of {lessons_total} chapters.")
print("Progress:", round(lessons_done / lessons_total * 100), "%")
Run it from that folder with python3 hello.py (macOS, Linux) or py hello.py (Windows). Python executes the statements from top to bottom and exits after the last one. The f before a string turns it into a formatted string literal: expressions inside braces are evaluated and inserted. print accepts several values and separates them with spaces.
A few rules apply to every program you will write:
- Names are case-sensitive.
learner,LearnerandLEARNERare three different names. - Indentation is syntax, not decoration. A block (the body of an
if, a loop or a function) is the run of lines indented under it. Use four spaces per level and let your editor insert them. #starts a comment that runs to the end of the line. Comments should explain why, since the code already says what.- Statements end at the end of the line. A long expression can continue inside parentheses, brackets or braces without any special character.
Python’s style guide, PEP 8, adds conventions that most projects follow: snake_case for variables and functions, CapWords for classes, UPPER_CASE for constants, and lines that stay readable without horizontal scrolling. A formatter such as Ruff or Black applies the layout rules automatically.
Experiment in the interactive shell
Run python3 (or py) with no file name and you get an interactive prompt, >>>. Type an expression and Python prints its value right away:
>>> 7 * 6
42
>>> "python".upper()
'PYTHON'
>>> help(len)
The shell is the fastest way to check how something behaves. help(obj) shows documentation, dir(obj) lists an object’s attributes, and type(obj) tells you what kind of object you have. Exit with exit() or Ctrl+D (Ctrl+Z then Enter on Windows). Two differences from a script are worth remembering: the shell echoes the value of every expression, while a script prints only what you pass to print; and the >>> prompt is not part of the code, so never paste it into a file.
Read a traceback from the bottom up
Sooner or later a program stops with an error. Save this as broken.py:
def average(scores):
return sum(scores) / len(scores)
print(average([80, 90, 100]))
print(average([]))
Running it prints 90.0, and then a traceback:
Traceback (most recent call last):
File "broken.py", line 5, in <module>
print(average([]))
~~~~~~~^^^^
File "broken.py", line 2, in average
return sum(scores) / len(scores)
~~~~~~~~~~~~^~~~~~~~~~~~~
ZeroDivisionError: division by zero
Read it from the last line upwards. The last line names the exception type and a message: dividing by zero. The frames above it show the chain of calls that led there, with the innermost call last, so line 2 inside average is where it failed and line 5 is the call that passed the empty list. The markers under the code point at the exact operation. Most errors you meet early on are a NameError (a misspelled name), a TypeError (an operation on the wrong kind of value) or an IndentationError. Chapter 9 covers handling errors in code.
Give each project its own packages
Python’s standard library is large, but sooner or later you install third-party packages from PyPI, the Python Package Index. Installing them into the interpreter you downloaded mixes every project’s dependencies together. A virtual environment avoids that: it is a folder with its own site-packages directory that points at a base interpreter.
# macOS or Linux, inside the project folder
python3 -m venv .venv
source .venv/bin/activate
python -m pip install httpx
python -m pip freeze > requirements.txt
deactivate
# Windows PowerShell
py -m venv .venv
.venv\Scripts\Activate.ps1
python -m pip install httpx
Activation only changes which python your terminal finds first. You can skip it and call .venv/bin/python (or .venv\Scripts\python.exe) directly, which is handy in scripts and scheduled jobs. Always run pip as python -m pip: that guarantees the package lands in the interpreter you are about to use. Keep .venv out of version control. It is generated state, and anyone can recreate it with python -m pip install -r requirements.txt.
Manage projects with uv
uv is a fast tool that combines installing Python versions, creating environments and locking dependencies. Once it is installed (see its documentation), a project takes a few commands:
uv init study-tracker
cd study-tracker
uv python pin 3.14
uv add httpx
uv run python -c "import httpx; print(httpx.__version__)"
uv init writes pyproject.toml, the standard file that names the project and its dependencies. uv python pin records the interpreter version in .python-version. uv add installs a package and writes the exact resolved versions of everything to uv.lock. uv run runs a command inside the project’s environment, creating or updating it first, so you never activate anything by hand. Commit pyproject.toml, .python-version and uv.lock; a teammate then runs uv sync to get the same environment. Pick one workflow per project, either pip with requirements.txt or uv, so the lockfile and the installed packages never disagree.
Choose an editor and check it uses your environment
Any editor that saves plain text will do. VS Code with the Python extension and PyCharm are common choices: both run code, show errors as you type and include a debugger. Whatever you choose, point it at the project’s .venv interpreter, then confirm by running import sys; print(sys.executable) from the editor and from the terminal. If the two paths match, packages and debugging behave the same in both places.
When something does not work, collect four facts before changing anything: the exact command, the interpreter version and path, the current folder, and the full error message. command -v python3 (macOS, Linux) or where.exe python (Windows) shows which executable the terminal finds. A missing file is often a working-directory problem, or an editor that saved hello.py.txt and hid the extension. These facts also make a good question for a colleague, because they separate environment problems from code problems.
Practice
Write about.py that prints your name, the Python version (sys.version) and the interpreter path (sys.executable). Run it from the terminal and from your editor, then create a virtual environment, run it again with the environment’s interpreter, and compare the paths.
Answer check: start the file with import sys, then print the three values. Inside the environment, sys.executable points into .venv, while sys.version is the same as the base interpreter’s, because a virtual environment reuses that interpreter and only changes where packages come from.
Summary
The interpreter runs your code; check its version and path whenever behaviour surprises you. Tracebacks are read from the last line up. A virtual environment, or a uv project, gives each project its own packages, and the files that describe it (requirements.txt, or pyproject.toml with uv.lock) are what you commit.