Sub topic 1
What Python Is and How a Python Program Runs
Prerequisites
You should be comfortable opening a terminal, navigating folders, and installing software. This chapter assumes you are using Python 3, not Python 2.
Python and Program Execution
Python is an interpreted programming language. You write instructions in a .py file, and the Python interpreter reads and executes those instructions. The interpreter may compile parts of the program to bytecode internally, but you normally run the source file directly.
Python can also run statements interactively in a read-evaluate-print loop (REPL). It reads your statement, evaluates it, prints the result, and waits for the next statement.
Choosing a Python Version
Install the latest stable Python 3 release supported by your project. Check a project's documentation before choosing a minor version, such as 3.12 or 3.13. Avoid relying on the system Python for project dependencies.
Terminal Basics
A terminal runs commands in your current directory. Use cd to change directories and list files with ls on macOS or Linux, or dir in Windows Command Prompt.
Installing Python
- Windows: Download the installer from python.org. Select “Add Python to PATH” before choosing the default installation.
- macOS: Install the official package from python.org or use Homebrew. The command is usually
python3. - Linux: Use your distribution's package manager, such as
apt,dnf, orpacman. Follow your distribution's Python 3 guidance.
PATH and Command Names
PATH is the list of folders that the shell searches for executable programs. Operating systems commonly use these command names:
python3usually selects Python 3 on macOS and Linux.pythonmay select Python 3, an older Python, or nothing.pyis the Python launcher commonly installed on Windows.
Checking the Installation
Run the command appropriate for your system:
python3 --version
# Windows:
py --version
# If configured on your system:
python --versionThe output should show Python 3 and a version number. If the command is not found, Python is missing from PATH or was not installed. If it shows Python 2 or an unexpected installation, use the explicit launcher for Python 3 and inspect your PATH.
Exercise
Which command should you try first on a typical Linux system to confirm Python 3 is available?
Show answer
Run python3 --version. It should print a Python 3 version.