Lists

A list is a numbered row, and the numbering starts at 0

servers'web-01'servers[0]or servers[-4]'web-02'servers[1]or servers[-3]'db-01'servers[2]or servers[-2]'cache-01'servers[3]or servers[-1]servers[1:3] is ['web-02', 'db-01']len(servers)4'db-01' in serversTrueservers.append('web-03')adds at the endA list is a numbered row, and the numbering starts at 0.So the last of 4 items is at index 3, and asking for servers[4] raises IndexError. Negative indexes countfrom the end. A slice [1:3] starts at index 1 and stops before index 3. Lists keep their order, cangrow and shrink, and may hold anything, including other lists.

Items are reached by index, from 0. A slice takes a run of them.

Containers · Going deeper

List, tuple, set, dict: choose by order, change and lookup

TypeWrittenKeeps orderCan changeDuplicatesReach an item byReach for it whenlist['a', 'b']yesyesyesindex: x[0]a sequence that growstuple('a', 80)yesnoyesindex: x[0]a fixed record; a dict keyset{'a', 'b'}noyesnomembership: inunique items, fast "in"dict{'a': 80}as insertedyeskeys: nokey: x['a']looking up by nameFour that catch people out{}an empty dict; the empty set is set()(1)the int 1; a one-item tuple is (1,)x.sort()sorts x in place and returns Nonesorted(x)returns a new list; x is unchangedChoose by what you need: order, change, uniqueness, or lookup by key."in" on a list checks item by item; on a set or a dict it is one hash lookup, however large. Sets also compare:{80, 443} & {443, 8080} is {443}. A dict key or a set item must be hashable, so a list cannot be one(TypeError: unhashable type: 'list') and a tuple can. All results are from Python 3.13.

A tuple cannot change; a set holds each item once; a dict looks up by key.

Dictionaries

A dict finds a value by its key, not its position

server = { 'name': 'web-01', 'port': 8080, 'tags': ['prod', 'eu'],}'name''port''tags''web-01'8080'prod''eu'server['name']server['port']server['tags'][0]✗server['owner']KeyError: no such key✓server.get('owner')None, and no errorA dict finds a value by its key, not by its position.Keys are usually strings and each appears once. Values can be anything: here a string, a number and alist. server['port'] = 443 changes a value, or adds the key if it is new. Loop over a dict with"for key, value in server.items():".

Each key leads to one value. A missing key is a KeyError.

Dictionaries · Going deeper

A list of dicts is a table

'name''port''env'servers[0]'web-01'8080'prod'servers[1]'web-02'443'prod'servers[2]'db-01'5432'test'servers[1]['port'] is 443A list of dicts is a table: each dict a row, each key a column.This is the shape that nearly every API returns and every JSON file holds. Read the expression fromleft to right: the record at index 1, then the value under its port key.

servers[1] is a row. 'port' picks the column.

Comprehensions

A comprehension filters and transforms in one line

s['name']what to keepfor s in serversfrom each itemif s['env'] == 'prod'only where this is true{'name': 'web-01', 'env': 'prod'}✓env is 'prod''web-01'{'name': 'web-02', 'env': 'prod'}✓env is 'prod''web-02'{'name': 'db-01', 'env': 'test'}✗env is 'test'droppedprod is ['web-01', 'web-02']A comprehension builds a new list in one line: filter, then transform.The full line is prod = [s['name'] for s in servers if s['env'] == 'prod']. It replaces a four-lineloop that appends to an empty list. The original list is not changed. Keep it to one filter and oneexpression; anything longer reads better as an ordinary loop.

What to keep, from where, and which ones.

Files

with opens a file and guarantees it is closed

with open('servers.txt') as f: for line in f: print(line.strip())servers.txton diskopenfan open fileone line per pass of the loopclosedas the block endsModeMeansWhat happens to an existing file'r'readthe default; the file must exist'w'writecreates the file, or erases everything in it first'a'appendcreates the file, or adds to the end of itwith opens a file and guarantees that it is closed again.The file closes when the indented block ends, even if an error interrupts it. The second argument toopen() is the mode: open('app.log', 'a'). For paths, pathlib.Path works the same on every operating system.

with closes the file for you. Mode 'w' erases first.

JSON and CSV files · Going deeper

json and csv do the parsing: never split the text by hand

servers.json[{"name": "web-01", "port": 8080, "owner": null}]import jsonwith open('servers.json') as f: servers = json.load(f)servers[0]['port'] + 1 # 8081[{'name': 'web-01', 'port': 8080, 'owner': None}]Types arrive intact: 8080 is an int, null became None.servers.csvname,port,noteweb-01,8080,"eu, main"import csvwith open('servers.csv', newline='') as f: rows = list(csv.DictReader(f))rows[0]['port'] + 1 # TypeError{'name': 'web-01', 'port': '8080', 'note': 'eu, main'}Every cell is a str: '8080' stays text until you convert it.Let json and csv do the parsing. JSON keeps its types; CSV hands you strings.line.split(',') would cut the note column in two at its quoted comma. The csv module handles quotes, and newline=''lets it handle a line break inside a quoted cell. json.dump(data, f, indent=2) writes a file back. Passencoding='utf-8' to open() as well: in Python 3.13 the default still comes from the operating system.

json.load returns typed data. csv returns strings, every time.

Errors

An exception climbs the call stack until it is caught

def load(path): return open(path).read()def main(): try: data = load('missing.txt') except FileNotFoundError: data = ''main()has a try blockload()no try blockopen()the file is missingcalls go downraisesFileNotFoundErrornothing here catches it:the call is abandonedexcept matches: caughtthe program carries onAn exception climbs the call stack until an except clause matches it.If nothing catches it, the program stops and prints a traceback: the stack at the moment of failure,with the error itself on the last line. Catch only errors you can handle, and name them; a bare excepthides real bugs. A finally block runs either way, for cleanup.

An error raised deep down travels up to the nearest matching except.

Errors · Going deeper · 1 of 2

Modules, pip and virtual environments

Built inalways thereprint len openint str rangenothing to importStandard libraryinstalled with Pythonjson os pathlibdatetime re csvimport jsonPyPIover 500,000 projectsrequests boto3pyyaml pytestpip install requestsproject-a/.venvrequests 2.31, boto3 1.34project-b/.venvrequests 2.25, an older projectimport brings in code that someone else wrote. A virtual environment keeps it per project.python -m venv .venv creates one, and pip then installs into it rather than into the system. So twoprojects can need different versions of one package. requirements.txt lists what to install.

import what exists. pip install what does not. One venv per project.

try blocks · Going deeper · 2 of 2

try has four parts: except on error, else on success, finally always

try: f = open(path)except FileNotFoundError: print('no file')except OSError as e: print('cannot open:', e)else: data = f.read()finally: print('done')What happens inside tryBlocks that run, in orderno errortry, else, finallyFileNotFoundErrortry, first except, finallyPermissionErrortry, second except, finallyany other errortry, finally; then it climbs onPart of the built-in family: a class is caught by any class above itBaseException KeyboardInterrupt, SystemExit Exception OSError FileNotFoundError, PermissionError LookupError KeyError, IndexErrorexcept runs on a matching error, else only when there was none, finally in every case.Clauses are tried from the top and the first match wins, so name the specific class first: with except OSError aboveexcept FileNotFoundError, the second could never run. "except Exception" catches nearly everything and still letsCtrl+C (KeyboardInterrupt) through; a bare "except:" does not. Keep the try block short, so you know which line it guards.

except clauses are tried in order, so the specific class goes first.

Lesson complete

You finished “Python: data, files and errors”

Nicely done.

Open in a new tab