INF1511 - Chapter 3 - Sequences
INF1511 - Chapter 3 - Sequences
1. Sequences
There are three types of sequences;
i. Strings which are sequences of Unicode characters, presented in Python in quotes i.e.
'Hello', "Hello", '"Hello"',"""Hello"". Strings are immutable—they cannot be modified after
they are created.
ii. Lists are ordered sequences of values, presented in Python as ["John", "Kelly", 1, 2, [Sugar,
Butter, 10]]. A list is mutable; you can append elements, remove existing elements, or
rearrange elements.
iii. Tuples are ordered sequences of values, presented in Python in brackets i.e. (Tiger, Coffee,
1,'a',10.50, 'b'). Tuples are immutable—they cannot be modified after they are created.
2. Sequence operators
+ concatenates sequences.
* repeats sequences.
[ ] fetches a particular element from the sequence (indexing) or
a subset of elements from the sequence (slicing). The index is a number that represents an element
in a string. All sequences start with an index of 0, i.e. the first element has index equal to zero.
A function is a part of the code that is called by name, e.g. min (), max () and len (), which does some
computation on the attributes of an object and returns a value.
A method is a section of the code called by name that returns the object (or part of it) to which it is
associated, e.g. str (), sorted () and capitalise ().
4. Arrays
Arrays are type of variable used to store numerical values. The location of values in the array is
determined by indices (starting with index 0 for the first element).
One dimensional arrays consist of a list of values, similar to the strings and lists discussed above.
Two dimensional arrays consist of rows and columns. Each element in the array is identified by the
number of the row and the number of the column. Thus element [0] [0] is the first element in the
array, element [1][0] is the second element in the first column. Note the first index always refers to
the row and the second index refers to the column.
5. Dictionary
A dictionary is a mutable combination of key/value pairs in which every key has to be unique.
The dictionary is used in Python to store and retrieve values quickly and efficiently, e.g. you may you
it to store the exchange rate for currencies, where your key is values “Euro”, “Rand”, “Pound” etc.
The value would then be the exchange rates of the above currencies against the dollar for example.
6. Sets
A set is a collection of values on which the following operations can be performed;
union (|) - an element appears in the union if it exists in one set or the other.
intersection (&) - the elements that appear in both sets appear in the intersection.
difference (-) - all the elements that are in the left set but not in the right set
will appear in the difference operation.
Sets are used in Python to manipulate groups of data, e.g. students’ marks from two modules. You
would use the intersection operation to find students who wrote both module tests (all students
appearing in both sets), the union operation to find all students who wrote both module tests (all
students in the two sets).