Installing Python 3.12 and meeting the REPL
Marrowby Market sells groceries online, and every morning its owner exports the night's orders into a file called orders.csv. Opening that file in a spreadsheet takes a minute, cleaning it by hand takes an hour, and the same mistakes come back every day. A Python script can do the hour in about a second. Before it can, Python has to exist on your machine.
What you are installing
Python is two things with one name: a language you write, and a program called the interpreter that reads what you wrote and carries it out. You write plain text. The interpreter does the work.
This course uses Python 3.12. The last number (3.12.3, 3.12.15) is a bug-fix release and does not matter for anything you do here.
Installing on your operating system
On Windows 11, download the installer for Python 3.12 from the python.org downloads page. On the first screen, tick Add python.exe to PATH before you click Install. That box is unticked by default, and forgetting it is the most common reason python "doesn't work" afterwards. If you type python on a Windows machine that has none, Windows opens the Microsoft Store instead of running anything. That is a shortcut to the store, not Python.
On macOS 14, use the installer from python.org as well. It gives you a command named python3, not python, so every command in this course that says python becomes python3 for you.
On Ubuntu 24.04, Python 3.12 is already there, again as python3. The venv part will need one extra package, and we deal with it in the third lesson of this module.
Now check what you have. On Windows the launcher called py is the safest entry point, and python works once PATH is set:
PS> py -3.12 --version
Python 3.12.2On macOS or Linux:
$ python --version
Python 3.12.15Your last number will differ, and that is fine. Every output in the console blocks of this course was produced by running the commands on Python 3.12.15 in a Linux container; the Windows-only commands were run on Windows 11 with Python 3.12.2.
Talking to the interpreter directly
Run python with no arguments and you land in the REPL, short for read, evaluate, print, loop. You type one line, Python answers at once, and the >>> prompt returns. It is the quickest way to test a thought.
>>> 2 + 3
5
>>> print("Marrowby Market")
Marrowby Market
>>> shop_name = "Marrowby Market"
>>> shop_name
'Marrowby Market'Look at the last two lines. print(...) shows text as a person would read it. Typing a bare name in the REPL shows its representation, which is why the quotes appear. In a script file, a bare name on its own line shows nothing at all.
Reading your first error
Typos will happen in the next minute, so meet them now. Spell the variable wrongly:
>>> print(shop_nam)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'shop_nam' is not defined. Did you mean: 'shop_name'?Read an error from the bottom. The last line names the kind of error (NameError) and says what went wrong. Python even guesses the fix. The lines above it say where: the file (<stdin> means "typed at the prompt") and the line number.
Now forget a closing quote:
>>> print("Marrowby Market)
File "<stdin>", line 1
print("Marrowby Market)
^
SyntaxError: unterminated string literal (detected at line 1)A SyntaxError means Python could not even understand the line, so nothing ran. The caret points at where the trouble starts, here the opening quote that never closed.
To leave the REPL, type exit() and press Enter. Ctrl+Z then Enter works on Windows, Ctrl+D on macOS and Linux.
- Python is the language and also the program, the interpreter, that runs it.
- Windows needs the PATH box ticked; macOS and Linux use
python3. - The REPL runs one line at a time and is for experiments, not for keeping work.
- Read errors from the last line upward.
The REPL forgets everything when you close it. To keep work, you need a file and an editor, which is the next lesson.
أسئلة ونقاش
لا توجد رسائل بعد. هل لديك سؤال عن هذا الدرس؟ اطرحه هنا.
سجّل الدخول للمشاركة في النقاش. تسجيل الدخول