Python basics, part 2 of 6
Python Data Types
Almost every Python program is built from five types: strings, lists, tuples, dictionaries and sets. This page shows how to create each one, how to read and change it, and when to pick it. It is a rewrite of five Pythofrank lessons from February 2017. The original examples are kept where they still fit, updated from Python 2.7 to Python 3, and the gaps are filled with examples from a small family bank.
The five types at a glance
A string holds text. The other four are collections: they hold several values in one variable. They differ in three ways: whether the items keep their order, whether the value can be changed after it is made (mutable) or not (immutable), and whether the same item may appear twice.
| Type | Holds | Ordered? | Mutable? | Duplicates? | Written as |
|---|---|---|---|---|---|
String str | Characters | Yes | No | Yes | "savings" |
List list | Any values | Yes | Yes | Yes | [5000, 800, 800] |
Tuple tuple | Any values | Yes | No | Yes | ("Rishikesh", 5000) |
Dictionary dict | Key and value pairs | Yes, in the order added | Yes | Keys no, values yes | {"balance": 5000} |
Set set | Unique values | No | Yes | No | {"savings", "current"} |
Concept: len() works on all five. It counts characters in a string, items in a list, tuple or set, and keys in a dictionary.
Strings
Type 1 of 5 · text
A string is a sequence of characters: letters, digits, spaces, symbols. Names, account numbers, messages and file contents are all strings. A string keeps its characters in order and can never be changed once made.
- Creating strings: quotes, joining, escape characters
- Indexing and slicing: one character or a piece
- Common string methods: clean, split, search, replace
- f-strings and formatting: put values into text
- Strings cannot be changed: every method returns a new string
Creating strings
Put the text in single or double quotes. Both make the same type. + joins two strings and * repeats one.
A backslash starts an escape character, a code for something you cannot type directly. \n is a new line and \t is a tab. Three quotes let a string run over several lines.
Indexing and slicing
Each character has a position called its index, starting at 0. Negative indexes count from the end. A slice s[start:stop] takes the characters from start up to, but not including, stop.
Concept: an index that does not exist, such as s[10] here, raises an IndexError. A slice never does. s[5:100] simply gives what is there: FGHIJ.
Common string methods
A method is a function that belongs to a value and is called with a dot. These clean up text and change its case.
split() cuts a string into a list of pieces and join() glues a list back into one string. The rest of these search inside the text.
f-strings and formatting
Put f before the opening quote and write any value or expression inside {}. After a colon you can say how to show it: .2f means 2 digits after the decimal point, and , adds thousands separators.
Two older styles do the same job. You will meet them in existing code, and the 2017 lesson used both.
Concept: < pads on the right so the text sits on the left, and > pads on the left. The number after it is the total width.
Strings cannot be changed
A string is immutable. You cannot replace one character in place, and no method changes the original. Every method returns a new string, so keep the result in a variable.
Lists
Type 2 of 5 · an ordered collection you can change
A list holds any number of values in order. You can add items, remove them, replace them and sort them. It is the collection you will use most.
- Creating and reading lists: brackets, indexes, slices
- Adding and removing items: append, insert, extend, remove, pop, del
- Sorting, searching and counting: sort, sorted, reverse, in, len
- Copying a list: two names for one list, and how to avoid it
Creating and reading lists
Write the items between square brackets, separated by commas. Indexing and slicing work exactly as they do for strings. Unlike a string, a list lets you assign to an index.
Adding and removing items
append() adds one item at the end, insert() adds one at a given index, and extend() adds every item of another list.
remove() deletes by value, pop() deletes by index and hands the item back, and del deletes by index or slice without returning anything.
Watch out: append(), remove() and the other methods that change a list return None. Writing l = l.append(5) loses the list.
Sorting, searching and counting
sort() reorders the list itself. sorted() leaves the list alone and returns a new sorted one. Both accept reverse=True and a key, a function that says what to sort by.
in tests whether an item is present. This is called a membership test. A handful of built-in functions answer the other common questions.
Copying a list
b = a does not copy a list. It gives the same list a second name, so a change through one name shows up through the other. This is called aliasing. Use copy() or a full slice [:] to get a separate list.
copy() is a shallow copy: it copies the outer list only. If the items are lists themselves, both copies still share them. copy.deepcopy() copies every level.
Concept: == asks whether two values are equal. is asks whether two names point at the very same object.
Tuples
Type 3 of 5 · an ordered collection that stays fixed
A tuple is like a list that cannot be changed after it is made. Use it for a small group of values that belong together, such as an owner with an account type and a balance.
- Creating tuples: parentheses, commas and the one-item tuple
- Unpacking and swapping: one name for each item
- Why tuples are immutable: safe to share, usable as keys
- Named tuples: read the items by name
Creating tuples
It is the commas that make a tuple. The parentheses are optional but make it easier to see. Reading works as it does for a list.
A tuple with one item needs a trailing comma. Without it, the parentheses are just ordinary brackets around a value.
Unpacking and swapping
Unpacking assigns each item of a tuple to its own name in one statement. The number of names must match the number of items. A name with a * collects whatever is left over into a list.
Why tuples are immutable
A tuple has no append, no remove and no item assignment. That is the point. A value that cannot change is safe to pass around, and it can be used as a dictionary key or stored in a set, which a list cannot.
Watch out: a tuple fixes which items it holds, not what is inside them. In t = ([1, 2], 3) the list t[0] can still grow with t[0].append(9).
Named tuples
account[2] does not say what the third item means. A named tuple is a tuple whose items also have names, so you can write account.balance. It is still immutable and still unpacks.
Dictionaries
Type 4 of 5 · values looked up by key
A dictionary stores pairs. Each pair has a key, which you look things up by, and a value, which is what you get back. A list answers "what is at position 2?". A dictionary answers "what is Rishikesh's balance?".
- Creating and reading dictionaries: braces,
[]andget - Adding, updating and deleting: assignment, update, setdefault, pop, del
- Looping over a dictionary: keys, values and items
- Dictionary comprehension: build a dictionary in one line
- Nested dictionaries: dictionaries inside dictionaries, and pretty printing
Creating and reading dictionaries
Write key: value pairs between braces. Square brackets read a value by its key and raise a KeyError if the key is missing. get() returns None, or a default you choose, instead.
There are other ways to build one. All three below give the same dictionary.
Concept: a key must be an immutable value such as a string, a number or a tuple. A value can be anything. Each key appears once: assigning to a key that exists replaces its value.
Adding, updating and deleting
Assigning to a key adds it if it is new and replaces the value if it exists. update() does the same for many keys at once.
pop() removes a key and returns its value. del removes a key and returns nothing.
Looping over a dictionary
Looping over a dictionary gives its keys. values() gives the values and items() gives each key together with its value. A dictionary remembers the order in which keys were added.
Concept: keys(), values() and items() return views, live windows onto the dictionary, not lists. Wrap one in list() when you need a real list.
Dictionary comprehension
A dictionary comprehension builds a dictionary from a loop in one line. It has the shape {key: value for item in collection}, with an optional if at the end.
Nested dictionaries
A value can itself be a list or another dictionary. Read a nested value by chaining the brackets, one level at a time.
A large nested dictionary is hard to read on one line. json.dumps() from the standard library turns it into indented text. sort_keys=True prints the keys in alphabetical order.
Concept: the output is JSON, a text format that many languages share. That is why Python's True is printed as true and every string gets double quotes.
Sets
Type 5 of 5 · unique values with no order
A set holds each value at most once and keeps no order. It is the right tool for removing duplicates, for asking "is this value in here?" and for comparing two groups.
- Creating sets: braces,
set()and uniqueness - Union, intersection and difference: combine and compare two sets
- Fast membership tests: why
inis quick on a set - frozenset: a set that cannot change
Creating sets
Write the items between braces, or pass any collection to set(). Duplicates are dropped. An empty set must be written set(), because {} is an empty dictionary.
Watch out: a set has no order, so there is no unique[0], and printing a set of strings can show a different order each time the program runs. The examples here print sorted(...), which returns a list in a fixed order.
Union, intersection and difference
These are the operations from school maths. Each has an operator and a method that do the same thing. All of them return a new set.
The same operations answer real questions about two groups of customers.
Fast membership tests
To answer x in some_list, Python checks the items one by one, so a longer list takes longer. A set stores its items by their hash, a number worked out from the value, and can jump almost straight to the right place. The time barely grows with the size of the set.
Concept: the exact times change from run to run and from machine to machine, so the example prints only the comparison. Dictionary keys are found the same way, which is why key in some_dict is fast too.
frozenset
A frozenset is an immutable set. It supports every operation that reads, but has no add or discard. Because it cannot change, it can be a dictionary key or an item of another set, which an ordinary set cannot.
Concept: the order you write the items in does not matter. Two sets are equal when they hold the same items, so the lookup with the names swapped finds the same key.
What changed since Python 2
The original lessons were written for Python 2.7, which reached its end of life in 2020. If you read them, these are the differences that matter for the five types.
| Python 2.7 (2017 lessons) | Python 3 (this page) |
|---|---|
print name, city | print(name, city) |
type("a") shows <type 'str'> | <class 'str'> |
u"abcd" for Unicode text | Every string is Unicode, no u needed |
"%s has %.2f" % (name, balance) | f"{name} has {balance:.2f}" |
range(7, 16) returns a list, xrange is the lazy form | range is lazy, wrap it in list(); xrange is gone |
d.keys() and d.values() return lists | They return views; use list(d.keys()) for a list |
| Dictionaries print in no fixed order | Dictionaries keep the order in which keys were added |
A set prints as set([1, 2, 3]) | {1, 2, 3} |
raw_input() | input() |
The 2017 pages are still online as they were written: Strings, Lists, Tuples, Dictionaries and Sets.
Which type to use
Start from what you need to do with the data, not from the type.
| You need to | Use | Example |
|---|---|---|
| Hold text | String | "SB-2017-000123" |
| Keep items in order and add, remove or sort them | List | The deposits made today |
| Group a few fixed values that belong together | Tuple | ("Sandhya", "savings", 12000) |
| Do the same, but read the values by name | Named tuple | account.balance |
| Look a value up by a name or an id | Dictionary | Balance for each owner |
| Describe one thing with several named fields | Dictionary | {"owner": ..., "balance": ...} |
| Remove duplicates | Set | The cities our customers live in |
| Test many times whether a value is present | Set | Is this account id blocked? |
| Compare two groups | Set | Customers with savings but no loan |
| Use a group of values as a dictionary key | Tuple or frozenset | ("savings", "senior") |
Next, learn how to walk through all of these one item at a time in Python Loops.