How#

Two important data structures have already been seen in previous chapters:

How to define a function#

See: Probability: How to define a function.

How to write a docstring#

A docstring is an attribute of a function that describes what it is. This can describe what it does, how it does it and/or why it does it. Here is how to write a docstring for a function that takes variables and returns a value.

Tip

def name(parameter1, parameter2, ...):
    """
    <A description of what the function is.>

    Parameters
    ----------
    parameter1 : <type of parameter1>
        <description of parameter1>
    parameter2 : <type of parameter2>
        <description of parameter2>
    ...

    Returns
    -------
    <type of what the function returns>
        <description of what the function returns>

    """
    INDENTED BLOCK OF CODE
    return output

For example, here is how to write a function that returns \(x ^ 3\) for a given \(x\):

def x_cubed(x):
    """
    Calculates and returns the cube of x. Does this by using Python
    exponentiation.

    Parameters
    ----------
    x : float
        The value of x to be raised to the power 3

    Returns
    -------
    float
        The cube.
    """
    return x ** 3

Create a tuple#

See Combinatorics: How to create a tuple.

Create a list#

See Probability: How to create a list.

Create a list using a list comprehension#

See Probability: Create a list using a list comprehension.

Combine lists#

Given two lists it is possible to combine them to create a new list using the + operator:

Tip

first_list + other_list

Here is an example a single list from two separate lists:

first_list = [1, 2, 3]
other_list = [5, 6, 100]
combined_list = first_list + other_list
combined_list
[1, 2, 3, 5, 6, 100]

Append an element to a list#

Appending an element to a list is done using the append method.

Tip

a_list.append(element)

Here is an example where we append a new string to a list of strings:

names = ["Vince", "Zoe", "Julien", "Kaitlynn"]
names.append("Riggins")
names
['Vince', 'Zoe', 'Julien', 'Kaitlynn', 'Riggins']

Attention

It is not possible to do this with a tuple as a tuple immutable](difference_between_a_list_and_a_tuple).

Remove an element from a list#

To remove a given element from a list use the remove method.

Tip

a_list.remove(element)

Here is an example where we remove a number from a list of numbers:

numbers = [1, 94, 23, 202, 5]
numbers.remove(23)
numbers
[1, 94, 202, 5]

Attention

It is not possible to remove an element from a tuple as a tuple immutable](difference_between_a_list_and_a_tuple).

Sort a list#

To sort a list use the sort method.

Tip

a_list.sort()

Here is an example:

names = ["Vince", "Zoe", "Kaitlynn", "Julien"]
names.sort()
names
['Julien', 'Kaitlynn', 'Vince', 'Zoe']

To sort a list in reverse order use the sort method with the reverse=True parameter.

names.sort(reverse=True)
names
['Zoe', 'Vince', 'Kaitlynn', 'Julien']

Attention

It is not possible to sort a tuple as a tuple is immutable.

Create a sorted list from an iterable#

To create a sorted list from an iterable use the sorted function.

Tip

sorted(iterable)

Here is an example:

tuple_of_numbers = (20, 50, 10, 6, 1, 50, 105)
sorted(tuple_of_numbers)
[1, 6, 10, 20, 50, 50, 105]

Access an element of an iterable#

See: Combinatorics: How to access particular elements in a tuple.

Find the index of an element in an iterable#

To identify the position of an element in an iterable use the index method.

Tip

iterable.index(element)

Here is an example:

numbers = [1, 94, 23, 202, 5]
numbers.index(23)
2

Attention

Recall that python uses 0-based indexing. The first element in an iterable has index 0.

Access an element of an iterable using negative indexing#

It is possible to access an element of an iterable by counting from the end of the iterable using negative indexing.

Tip

iterable[-index_from_end]

Here is an example showing how to access the penultimate element in a tuple:

basket = ("Carrots", "Potatoes", "Strawberries", "Juice", "Ice cream")
basket[-2]
'Juice'

Slice an iterable#

To create a new iterable from an iterable use [] and specify a start (inclusive) and end (exclusive) pair of indices.

Tip

iterable[include_start_index: exclusive_end_index]

For example:

basket = ("Carrots", "Potatoes", "Strawberries", "Juice", "Ice cream")
basket[2: 5]
('Strawberries', 'Juice', 'Ice cream')

Find the number of elements in an iterable#

To count the number of elements in an iterable use len:

Tip

len(iterable)

For example:

basket = ("Carrots", "Potatoes", "Strawberries", "Juice", "Ice cream")
len(basket)
5

Create a set#

A set is a collection of distinct objects. This can be created in Python using the set command on any iterable. If there are non distinct objects in the iterable then this is an efficient way to remove duplicates.

Tip

set(iterable)

Here is an example of creating a set:

iterable = (1, 1, 3, 4, 4, 3, 2, 1, 10)
unique_values = set(iterable)
unique_values
{1, 2, 3, 4, 10}

Do set operations#

Set operations between two sets can be done using Python:

  • \(S_1 \cup S_2\): set_1 | set_2

  • \(S_1 \cap S_2\): set_1 & set_2

  • \(S_1 \setminus S_2\): set_1 - set_2

  • \(S_1 \subseteq S_2\) (checking if \(S_1\) is a subset of \(S_2\)): set_1 <= set_2

Here are some examples of carrying out the above:

set_1 = set((1, 2, 3, 4, 5))
set_2 = set((4, 5, 6, 7, 8, 9))

set_1 | set_2
{1, 2, 3, 4, 5, 6, 7, 8, 9}
set_1 & set_2
{4, 5}
set_1 - set_2
{1, 2, 3}
set_1 <= set_2
False

(create_hash_tables) =

Create hash tables#

Lists and tuples allow us to immediately recover a value given its position. Hash tables allow us to create arbitrary key value pairs so that given any key we can immediately recover the value. This is called a dictionary in Python and is created using {} which takes a collection of key: value pairs.

Tip

{key_1: value, key_2: value, …}

For example the following dictionary maps pet names to their ages:

ages = {"Riggins": 4, "Chick": 7, "Duck": 7}
ages
{'Riggins': 4, 'Chick': 7, 'Duck': 7}

To recover a value we pass the key to the dictionary using [].

For example:

ages["Riggins"]
4

Attention

If a key is used to recover the value with [] but the key is not in the dictionary then an error will be raised.

Access element in a hash table#

As described here to access the value of a key in a hash table use [].

Tip

dictionary[key]

It is also possible to use the get method. The get method can also be passed the value of a default variable to return when the key is not in the hash table:

Tip

dictionary.get(key, default)

For example:

ages = {"Riggins": 4, "Chick": 7, "Duck": 7}
ages.get("Vince", -1)
-1

Iterate over keys in a hash table#

To iterate over the keys in a hash table use the keys() method:

Tip

dictionary.keys()

For example:

ages = {"Riggins": 4, "Chick": 7, "Duck": 7}
ages.items()
dict_items([('Riggins', 4), ('Chick', 7), ('Duck', 7)])

Iterate over values in a hash table#

To iterate over the values in a hash table use the values() method:

Tip

dictionary.values()

For example:

ages = {"Riggins": 4, "Chick": 7, "Duck": 7}
ages.values()
dict_values([4, 7, 7])

Iterate over pairs of keys and value in a hash table#

To iterate over pairs of keys and values in a hash table use the items() method:

Tip

dictionary.items()

For example:

ages = {"Riggins": 4, "Chick": 7, "Duck": 7}
ages.items()
dict_items([('Riggins', 4), ('Chick', 7), ('Duck', 7)])