Chapter 2 of 24 · Python for Beginners — Complete Free Tutorial
Installing Python and Running Code
The 24-chapter Python for Beginners series now moves from ideas to your machine. This chapter installs a current Python from python.org, explains the confusing python versus python3 command names, gets you into the REPL, runs a script from a file, and shows the one rule that replaces braces entirely: indentation is the syntax.
Install from python.org
You need the reference implementation, CPython, from python.org/downloads. Any release from 3.12 onwards runs every example in this series — the series uses no third-party packages until chapter 13, so nothing else is required.
- macOS — download the package installer, run it and accept the defaults. The installer puts
python3on yourPATHfor you. - Windows — download the installer, tick Add python.exe to PATH on the first screen, then choose Install Now.
- Linux — most distributions already ship Python 3; if not, install it with your package manager, for example
sudo apt install python3on Debian or Ubuntu.
Verify from a fresh terminal — always a fresh terminal, because a new install cannot update a shell that is already running:
python3 --version
python --version
Expected output:
Python 3.13.12
Python 3.13.12
Your first line will show whichever release you downloaded; anything from 3.12 up is correct for this series. If the second command reports that python is not found, that is normal on macOS and Linux and it is the subject of the next section — not a broken install.
python3 versus python
Two commands, one interpreter, and forty years of history. When Python 3 arrived in 2008 it broke compatibility with Python 2, so installers began shipping the new version as python3 and left the bare python name to whatever old version was already present. Python 2 reached end of life in 2021, but the two command names never merged back together.
| Command | Where it works | Notes |
|---|---|---|
python3 | macOS, Linux, CI images, most tutorials | The command to type throughout this series |
python | Windows with the python.org installer, some Linux distributions | May be missing, and on an old machine may still mean Python 2 |
py | Windows only | The launcher: it finds installed versions and lets you pick one |
python3.12 | Anywhere that exact version is installed | Useful when several releases coexist |
Rule for this series: on macOS and Linux type python3. On Windows type python or py instead — every command in the series works with that one-word substitution.
Meet the REPL
Running the interpreter with no file opens the REPL: Read, Evaluate, Print, Loop. It reads one statement, evaluates it, prints the result and waits for the next. Start it with python3 (or python on Windows); you will see a banner naming your exact build, then the primary prompt >>>:
>>> 2 + 2
4
>>> name = "Ada"
>>> print("Hello,", name)
Hello, Ada
>>> 7 / 2
3.5
>>> quit()
Three behaviours to notice. The assignment on line three prints nothing — the value went into a name rather than to the printer, so the prompt simply returns (JavaScript consoles answer undefined instead, which is a small mercy you do not get here). Expressions echo their value, always in its literal form. And quit() — or Ctrl+D on macOS and Linux — leaves the session.
Anything unfinished shows the continuation prompt .... A block of code is only released when you press Enter on an empty ... line:
>>> total = 10
>>> if total > 5:
... print("big")
...
big
>>>
Expected output: the line big, printed once the empty continuation line ends the block. The REPL is the right place to test a single expression; it forgets everything when you close it, so real programs live in files.
Running a script file
Scripts are plain text files with a .py extension. Create hello.py in a folder of your choice:
# hello.py
print("Running my first script")
print("Sum:", 6 * 7)
Open a terminal in that same folder and pass the file to the interpreter:
python3 hello.py
Expected output:
Running my first script
Sum: 42
The working directory matters: python3 hello.py looks for the file relative to where your terminal currently is, so cd into the folder first or pass the path to the file. For a single throwaway expression you can skip the file entirely: python3 -c "print('from a one-liner')" prints from a one-liner without creating anything.
Windows and the py launcher
The Windows installer also provides py, a launcher that knows which interpreters you have installed and can pick between them. It runs the same files as python:
py --version
py hello.py
py -3.12 hello.py
Expected output:
Python 3.13.12
Running my first script
Sum: 42
The first line names whichever version the launcher chose, so it matches whatever you installed. Use the explicit form (py -3.12) once you have more than one release on the machine; otherwise the plain launcher is fine. If you ticked Add python.exe to PATH, python hello.py works as well.
Choose an editor
Any text editor that can save UTF-8 will do, but syntax highlighting and a built-in terminal remove friction. The recommendation for this series is Visual Studio Code with Microsoft’s Python extension: it adds colouring, completion, inline error reporting, a test panel and one-click run — while every example here still runs from a plain terminal.
| Editor | Best for |
|---|---|
| VS Code + Python extension | Everyone in this series: free, light, excellent error messages |
| Thonny | First-hour beginners: a built-in step debugger and no project machinery |
| PyCharm | Large long-lived projects: refactoring and database tools built in |
| Sublime Text, Vim, whatever you love | Speed and habit; add a Python highlighter if it lacks one |
Install VS Code, open the folder containing your scripts, install the Python extension, then open its integrated terminal and run python3 hello.py exactly as you would in any other terminal. Nothing in this course depends on an IDE feature.
Indentation is the syntax
JavaScript reaches for braces to mark a block; Python reaches for whitespace. The indented lines under if, for, def and class are the block, and PEP 8 asks for four spaces per level, spaces only (never tabs, never a mixture of the two). This file is wrong on line two:
print("one")
print("two")
Expected output:
File "hello.py", line 2
print("two")
IndentationError: unexpected indent
Python prints the full path of your file on the File line; it is shortened to hello.py above. Note what is missing: there is no Traceback header, because a file with broken indentation never gets as far as running — Python parses the whole module before executing a single line of it.
The corrected version, with four spaces under the if and the following line back at the left margin:
total = 10
if total > 5:
print("big")
print("done")
Expected output:
big
done
That is the whole mechanism. Indentation decides what belongs to the if, and returning to column zero ends it — no closing brace to forget, and no brace to hunt for when something does not line up.
Common mistakes
- Typing
pythonon macOS or Linux — the command may be absent or may still be Python 2; usepython3(orpyon Windows). - Running the script from another folder —
can't open file ... [Errno 2] No such file or directorymeans your terminal is not where the file is; check withpwdandls. - Mixing tabs and spaces — an editor that inserts tabs while the example uses spaces produces
IndentationErrororTabError; set your editor to insert four spaces. - Pasting a whole block into the REPL — without the final empty line the block never executes; keep multi-line code in files instead.
- Following Python 2 tutorials —
print "hi",raw_input()andxrange()are two decades old and simply do not run.
Practice checklist
- Install CPython from python.org and record the output of
python3 --versionin your notes. - Open the REPL and evaluate five expressions, including one division and one string method, then leave with
quit(). - Create
hello.py, run it, change a line, and run it again until the edit-run loop feels automatic. - Indent line two of a script wrongly on purpose, read the
IndentationError, and fix it by aligning to column zero. - Install the VS Code Python extension, open your script folder, and run the file from the integrated terminal.
What's next?
The interpreter runs and your editor is ready — now the language itself begins with names and values: Variables and Data Types.
Frequently Asked Questions
Python is a general-purpose language first released in 1991, designed around one idea: code is read far more often than it is written. English keywords and indentation instead of braces make it readable, the standard library covers files, JSON, dates and testing, and pip adds thousands more packages. The same language handles scripts, web APIs, data work and machine learning.
None at all. Python is widely considered the friendliest first language: no declaration keywords, no braces, no build step, and error messages that name the problem. This series begins at your first print() call. If you already read JavaScript or TypeScript you will adjust quickly, but no previous language is assumed anywhere in these 24 chapters.
Python 3 only, any release from 3.12 onwards. Python 2 reached end of life in 2021 and its print "hi" statements and xrange() no longer run. One historical quirk remains: installers ship the interpreter as python3 on macOS and Linux, while Windows usually offers python or the py launcher, and every command in this series works with that one-word swap.
More than you might expect. Everyday automation such as renaming files, cleaning spreadsheets or scraping pages; web backends with Django, Flask or FastAPI; data analysis and machine learning with pandas, NumPy and PyTorch; DevOps tooling, desktop apps and games. Python is also the classic glue language, calling C, Rust or Fortran libraries from short readable scripts.
Pure Python can run 10 to 100 times slower than C, yet that rarely blocks you: heavy numeric work lives in C extensions such as NumPy, which Python coordinates. Dynamic typing does push errors to runtime, surfacing as a TypeError on the line that finally uses the bad value. Type hints checked by mypy move those mistakes back into your editor.
Only the interpreter. Install CPython from python.org, version 3.12 or newer, confirm it with python3 --version, and open a plain text editor; VS Code with the Python extension is the recommendation here. The early chapters use no third-party packages, so there is nothing else to set up until virtual environments and pip arrive.
A dict is a real runtime mapping: keys may be any hashable value rather than only strings, insertion order has been guaranteed since Python 3.7, and a missing key raises KeyError unless you ask with get(). JavaScript objects coerce every key to a string and carry a prototype chain. A TypeScript interface is erased at compile time and describes shape only; TypedDict is Python's checked equivalent.
Reach for a list when items will be added, removed or reordered: it is ordered, mutable and rich with methods. Pick a tuple for fixed records and function returns, because it is immutable and hashable, so it can be a dictionary key. Choose a set for unique values and fast membership checks; it has no order and de-duplicates on creation.
Yes, gradually. Annotate public function signatures first; the interpreter ignores hints at runtime, so a checker such as mypy is what gives them teeth. Add tests the same way: pytest collects any test_*.py file and plain assert statements, and a suite that finishes in a tenth of a second is one you will run after every change. Both habits cost little now and much more later.
JavaScript and TypeScript keep the browser, so anything rendered to the user stays theirs. Python takes the server: Django for full-featured applications, Flask for small ones, FastAPI for typed async APIs where your annotations become automatic documentation. Your TypeScript habits transfer directly, and asyncio mirrors the event loop you already know, just with different keywords.