Sequential Datatypes

Sequential Datatypes

Sequential Datatypes are a sequence of elements

  • Strings: sequence of Unicode code points

  • Lists: mutable sequence of elements of any type (⟶ recursive)

  • Tuples: like lists, but immutable

  • Binary data …

    • Bytes: like strings, only binary - there is no

      encoding. Immutable

    • Byte arrays: mutable arrays of raw bytes

  • Common set of operations

    • Indexing

    • Concatenation

    • Several specialities: slicing …

  • Very powerful (albeit a bit hard to read)

Sequence Elements

Elements are numbered

  • Starting at zero

../../../../../../_images/0200-sequential-types-index.svg

Sequence Membership

The in operator …

>>> 2 in ['one', 2, 'three']
True
>>> 3 in ['one', 2, 'three']
False
>>> 'three' in ['one', 2, 'three']
True
>>> 'three' not in ['one', 2, 'three']
False
  • Cool for short sequences

  • Sequential search

  • ⟶ probably not the right datastructure for searches

Sequence Multiplication

String multiplication

>>> 'blah' * 5
'blahblahblahblahblah'

Arbitrary sequence multiplication

>>> [1, 2, 3] * 5
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> ['one', 2, 'three'] * 3
['one', 2, 'three', 'one', 2, 'three', 'one', 2, 'three']