By the end you'll know
- What Python is good at, and when a shell script is still the better choice
- How to install Python and check which version you have
- How to use Python's interactive prompt as a scratchpad
- How to write and run a script, the first piece of the tool you'll build in this track
- How to read an error message, called a traceback
The problem: when a shell script stops being enough
If you've done the other tracks, you've typed a lot of commands: git, docker, kubectl. Put a few of them in a file, and you have a shell script: an easy way to repeat a job. For short jobs, that's perfect.
Then the job grows. Now you need to check 200 websites, skip the ones that are fine, retry the ones that time out, read a list from a spreadsheet, and save the results somewhere another tool can read. In a shell script, that turns into a tangle of quotes, brackets and pipes that's hard to read and easy to break.
Python is a programming language built to stay readable as programs grow. It reads close to plain English, it comes with a large set of ready-made tools (its standard library) for files, dates, the web and much more, and almost every service you'll use, from GitHub to AWS, offers a Python library for talking to it. That's why it's the language most infrastructure engineers reach for once a task outgrows a few commands.
| Use a shell script when | Use Python when |
|---|---|
| You're running a few commands in order | You're making decisions about lots of data |
| It fits on one screen | It's growing, and others will need to read it |
The work is mostly other programs (git, docker) | The work is reading files, calling APIs, or handling errors carefully |
What you'll build in this track
Each lesson adds one piece to a single real tool: a website checker. By the end, it reads a list of sites from a file, checks each one over the internet, saves the results, runs on a schedule, alerts you when something breaks, and even has its own small API. Today, it says hello.
Install Python
First, check whether you already have it:
python3 --version
If you see Python 3.12 or anything newer, you're set. If you see an older version, like Python 3.9, or command not found, install a current one:
- macOS: download the installer from python.org (opens in a new tab), or with Homebrew,
brew install python. (Macs often come with an older Python as part of Apple's developer tools; the newer one takes over once installed.) - Windows: download the installer from python.org (opens in a new tab), and on its first screen tick Add python.exe to PATH. On Windows, the command is usually
pythonorpyinstead ofpython3. - Linux: it's usually already there; if not, use your package manager, for example
sudo apt install python3.
Run python3 --version again to confirm. This track works with Python 3.12 and newer.
The interactive prompt: a scratchpad
Run Python with no file, and it waits for you to type:
python3
The prompt changes to >>>. That's the interactive prompt (also called the REPL, for read, evaluate, print, loop). Whatever you type, Python runs straight away and shows the result:
>>> 2 + 2
4
>>> 10 / 4
2.5
>>> "hello".upper()
'HELLO'
It's the quickest way to try something out. Notice "hello".upper(): the text in quotes is a value, and .upper() asks it to do something, here, turn itself into capital letters. Lesson 2 explains values properly. Leave the prompt with exit(), or Ctrl+D on macOS and Linux.
Your first script
The prompt forgets everything when you close it. A script is a file of Python that you can run again and again. Make a folder for the track, and open a new file called checker.py in your code editor (if you don't have one, Visual Studio Code (opens in a new tab) is free and works well for Python):
mkdir site-checker
cd site-checker
Put this in checker.py:
# A website checker, built one piece at a time.
print("Checking sites...")
print("2 sites to check")
Lines starting with # are comments: notes for people, which Python ignores. print() shows whatever's between its brackets. Save the file, and run it:
python3 checker.py
Checking sites...
2 sites to check
That's a Python program. Python read the file from top to bottom, ran each line in order, and stopped at the end. Every script you write, however large, works that way.
In my real setup
Most of CivicGrid is Python. Its API, the service people call to look up city officials, is a Python program built with FastAPI (Lesson 10 builds a small one). The weekly job that checks every city's mayor against their official website is a Python script, and so is the quality check that runs after every change to the data and stops the moment something looks wrong.
But I don't use Python for everything. My uptime check and my database backup are short shell scripts: a handful of commands, run in order, that fit on one screen. That's the table above in practice. I reach for Python when a job needs decisions, data, or careful handling of failures, and the website checker you're about to build needs all three.
Break it on purpose
Errors are how Python tells you what went wrong, and they're more helpful than they look. Make two different mistakes.
A missing quote. Change the last line of checker.py so its closing quote is gone, print("2 sites to check), and run it:
File "checker.py", line 3
print("2 sites to check)
^
SyntaxError: unterminated string literal (detected at line 3)
(Your File line shows the full path to your file.) A SyntaxError means Python couldn't even understand the file. Notice that nothing printed, not even "Checking sites...", although that line was fine: Python checks the whole file before running any of it, and stops if it can't read it. The message points at the line, and the ^ points at where it got confused. "Unterminated string literal" means a piece of text was opened with a quote and never closed. Put the quote back.
A misspelled name. Now change print on the last line to prnt, and run it again:
Checking sites...
Traceback (most recent call last):
File "checker.py", line 3, in <module>
prnt("2 sites to check")
^^^^
NameError: name 'prnt' is not defined. Did you mean: 'print'?
This time the first line did run. The file was readable, so Python started running it, and only hit the problem when it reached line 3. This report is a traceback: read it from the bottom up. The last line says what went wrong (NameError: Python doesn't know any prnt) and even suggests the fix. The lines above it say where: which file, which line, and the code itself.
Fix the spelling. Those two habits, reading the last line first and then finding the line number, will solve most errors you ever see.
Check yourself
When is Python a better choice than a shell script?
You type 10 / 4 at the >>> prompt. What happens?
What does a line starting with # do in a Python script?
In what order does Python run the lines of a script?
Your script has a SyntaxError on line 3. Lines 1 and 2 are fine. What do you see?
Where do you start reading a traceback?
Key terms
- Python: a programming language known for being readable, with a large standard library.
- Shell script: a file of terminal commands run in order. Great for short jobs.
- Interactive prompt (REPL): the
>>>prompt, where Python runs each line as you type it. - Script: a file of Python code, usually ending in
.py, run withpython3 file.py. - Comment: text after
#that Python ignores. - print(): shows a value in the terminal.
- SyntaxError: Python couldn't understand the file, so nothing runs.
- Traceback: Python's error report. Read it from the bottom up.
Go deeper
- The official Python tutorial (opens in a new tab), especially sections 2 and 3, covers the interpreter and first steps in more depth.
- Errors and exceptions (opens in a new tab) in the same tutorial explains tracebacks, ahead of Lesson 4.