Lesson 02
Storing Data in Python
A tour of the six built-in types you'll use most often as a digital humanist: integers, floats, strings, lists, tuples, and dictionaries.
In the video, I introduced six built-in types that you will use constantly in digital humanities work: integers, floats, strings, lists, tuples, and dictionaries. The first three hold a single piece of information. The last three hold collections of pieces. Before moving on to Lesson 03, you should be comfortable typing each of them and reading them in someone else’s code.
This is a tour, not a deep dive. Each of these types gets its own lesson later in Part 1. The goal here is just to recognize the shapes.
Variables and assignment
Everything below uses the same pattern: a name on the left, an = sign, a value on the right.
year = 1988
print(year)
year is the variable. 1988 is the value. Python keeps a reference to the value under that name until you reassign it or the program ends. Variable names are case-sensitive — year and Year are two different things — and conventionally use snake_case (lowercase with underscores).
Integers and floats — numbers
An integer is a whole number with no decimal point: 1, 2, 1988, -300. A float is a number that does have a decimal point: 1.0, 3.14159, 0.5. The decimal — even a trailing .0 — is what tells Python “this is a float, not an integer.”
an_integer = 1
a_float = 1.1
print(an_integer, a_float)
Python doesn’t care how big your integers get. 2 ** 1000 is fine — it returns the exact 302-digit number. Floats, on the other hand, follow IEEE-754 double precision, so very long decimals can be slightly imprecise (you’ll see 0.1 + 0.2 print as 0.30000000000000004). That rarely matters for humanities data, but it’s worth knowing the rule: integers are exact, floats are approximate.
Strings — text
A string is a sequence of characters wrapped in quotation marks. Either single or double works, as long as you open and close with the same kind:
greeting = "Hello"
farewell = 'Bye'
print(greeting, farewell)
Strings can hold any Unicode character — Greek, Arabic, Chinese, emoji, accented Latin — without any extra setup in Python 3. That matters for digital humanists working with non-English sources: "αρετή" and "عَرَب" are perfectly valid Python strings.
For text that spans multiple lines, use triple quotes:
passage = """The quality of mercy is not strain'd,
It droppeth as the gentle rain from heaven."""
print(passage)
For strings that mix in the value of a variable, use an f-string — a string prefixed with the letter f, with variables in curly braces. We’ll use these constantly:
name = "Ada"
print(f"Hello, {name}.") # Hello, Ada.
Lists and tuples — ordered collections
A list is an ordered collection of items, written with square brackets. A tuple is the same idea, written with parentheses. Items go in order, separated by commas, and a single collection can mix types — strings, numbers, even other collections.
a_list = [1, "one", 1.0]
a_tuple = (1, "one", 1.0)
print(a_list, a_tuple)
The big difference between them: lists are mutable (you can add, remove, and replace items after creating the list) and tuples are immutable (once created, the contents are fixed). In practice, you’ll reach for lists most of the time. Tuples come up when you want to signal that a small group of values belongs together and shouldn’t change — coordinates, a (year, place) pair, a row from a database.
You access a single item by index, starting from 0:
a_list = [1, "one", 1.0]
print(a_list[0]) # 1
print(a_list[2]) # 1.0
Negative indexes count from the end, which is handy for grabbing the last item:
a_list = [1, "one", 1.0]
print(a_list[-1]) # 1.0
Dictionaries — labeled collections
A dictionary stores key–value pairs in curly braces. The key is the label; the value is what’s stored under that label. They’re separated by a colon, and pairs are separated by commas.
person = {"name": "Ada Lovelace", "born": 1815, "field": "mathematics"}
print(person)
You access a value by its key, the same way you’d index a list — except the key is whatever you defined, not a position:
person = {"name": "Ada Lovelace", "born": 1815, "field": "mathematics"}
print(person["name"]) # "Ada Lovelace"
print(person["born"]) # 1815
Dictionaries are the workhorse data structure for humanities research. A row from a CSV becomes a dictionary. A JSON record from an API is already a dictionary. A row in a spreadsheet, a manuscript with metadata, an entry from a person’s correspondence — all natural fits.
Printing what you have
To show a value, pass it to the built-in print function:
a_list = [1, "one", 1.0]
person = {"name": "Ada Lovelace", "born": 1815, "field": "mathematics"}
print(a_list)
print(person["name"])
print accepts any of the types above, plus several at once separated by commas:
person = {"name": "Ada Lovelace", "born": 1815, "field": "mathematics"}
print("Born in:", person["born"])
That’s enough to start poking around. In Lesson 03 we’ll begin doing real work with strings — splitting, joining, replacing — and the rest of Part 1 builds out each of these types in turn.
Running the code
Save any snippet from this lesson to a file — say try.py — and run it from your project folder:
uv run try.py
uv run uses the project’s Python and dependencies automatically; no virtualenv to activate. If you haven’t set the project up yet, Lesson 01 walks through it.