Learnings

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.

TypeHoldsOrdered?Mutable?Duplicates?Written as
String strCharactersYesNoYes"savings"
List listAny valuesYesYesYes[5000, 800, 800]
Tuple tupleAny valuesYesNoYes("Rishikesh", 5000)
Dictionary dictKey and value pairsYes, in the order addedYesKeys no, values yes{"balance": 5000}
Set setUnique valuesNoYesNo{"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

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

Strings

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

Strings

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

Strings

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

Strings

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

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

Lists

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

Lists

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

Lists

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

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

Tuples

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

Tuples

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

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

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

Dictionaries

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

Dictionaries

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

Dictionaries

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

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

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

Sets

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

Sets

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

Sets

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, cityprint(name, city)
type("a") shows <type 'str'><class 'str'>
u"abcd" for Unicode textEvery 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 formrange is lazy, wrap it in list(); xrange is gone
d.keys() and d.values() return listsThey return views; use list(d.keys()) for a list
Dictionaries print in no fixed orderDictionaries 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 toUseExample
Hold textString"SB-2017-000123"
Keep items in order and add, remove or sort themListThe deposits made today
Group a few fixed values that belong togetherTuple("Sandhya", "savings", 12000)
Do the same, but read the values by nameNamed tupleaccount.balance
Look a value up by a name or an idDictionaryBalance for each owner
Describe one thing with several named fieldsDictionary{"owner": ..., "balance": ...}
Remove duplicatesSetThe cities our customers live in
Test many times whether a value is presentSetIs this account id blocked?
Compare two groupsSetCustomers with savings but no loan
Use a group of values as a dictionary keyTuple or frozenset("savings", "senior")

Next, learn how to walk through all of these one item at a time in Python Loops.