03. Sequences

Sequences are an ordered collection that may contain duplicate elements. This is quite the opposite from Python sets, which is an unordered collection that does not contain duplicate elements.

Six sequence types

There are six sequence types - str, bytes, bytearray, range, tuple and list. We have already seen the str primitive data type earlier. All sequences are immutable except for bytearray and lists.

Referencing by indices - Splicing

Since sequences are ordered, you may reference them by indices. Recall splicing, which we used for strings. Suppose we have a sequence s. We may pull a subsequence with the following notations.

From i to but not including j.
From i to end of sequence.
From beginning up to but not including j.
Up to but not including last element.
All elements.
Every kth element starting from i up to but not including j.

Bytes and Bytearray

Bytes and bytearrays are used to store raw data storing bytes (numbers 0 to 255). Bytearrays are mutable, but bytes are not.

To construct a bytes sequences, use either the bytes() command. The b followed by a string converts each character to its ASCII equivalent.

>>> testBytes = bytes(b'hello')
>>> testBytes[1]
>>> testBytes[1:4]
>>> testBytes[1:]
Traceback (most recent call last):
  File "<stdin>", line 1, in <modul>e
TypeError: 'bytes' object does not support item assignment

Note that for the first call, we have 101, which is the ASCII equivalent of the letter 'e', which is what we have here. For the second example, we call a spliced region and get back a bytes sequence. The last example shows the immutable nature of bytes.

>>> testBytearray = bytearray(b'hello')
>>> testBytearray[1:] = b'i'
>>> testBytearray

In this example, we can see that our bytearray is mutable.

Sequence operations

There are a set of sequence operations that can be used across all the types of sequences. For an element x in sequence s and t:

x in s
Check if x exists in the sequence.
x not in s
Check if x does not exist in the sequence.
s + t
Concatenate two or more sequences together.
2 * s
Join two sequences together.
Length of sequence.
Return smallest item.
Return largest item.
Return first occurrence of i.
Return total occurrences of i.

Let's now move onto the more interesting sequences - Ranges, Tuples and Lists.

Learn to be a Pythonista!

Programming Python

Learn to be a Pythonista! Try Python

Programming Python shows in-depth tutorials on the language's number of application domains including: system administration, GUIs, the Web, networking, front-end scripting layers, and more. This book focuses on commonly used tools and libraries to give you a comprehensive understanding of Python’s many roles in practical, real-world programming.

$ Check price
64.9964.99Amazon 4 logo(56+ reviews)

More Python resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources