Getting started


Variables, Types, and Operations


Figure 1

Explanation of a function call
Explanation of a function call

Figure 2

Terminal window on a Linux computer
Terminal window on a Linux computer

Figure 3

Terminal window on a Mac
Terminal window on a Mac

Figure 4


Figure 5

A comprehensive (but non-exhaustive) reference of built-in (native) types in Python 3.  * Not discussed in this course — included for reference only.   $ dict is not an iterable by default, however, it is possible to iterate through its keys.    Mutability is an important concept in programming. A mutable object is an object whose value(s) may be altered. This will become clearer once we study list and tuple. Find out more about mutability in Python from the documentations}.     Complex numbers refer to a set of numbers that have a real part, and an imaginary part; where the imaginary part is defined as \sqrt{-1}. These numbers are very useful in the study of oscillatory behaviours and flow (e.g. heat, fluid, electricity). To learn more about complex numbers, watch this Khan Academy video tutorial.
A comprehensive (but non-exhaustive) reference of built-in (native) types in Python 3.
* Not discussed in this course — included for reference only.
$dict is not an iterable by default, however, it is possible to iterate through its keys.

Mutability is an important concept in programming. A mutable object is an object whose value(s) may be altered. This will become clearer once we study list and tuple. Find out more about mutability in Python from the documentations}.

Complex numbers refer to a set of numbers that have a real part, and an imaginary part; where the imaginary part is defined as \(\sqrt{-1}\). These numbers are very useful in the study of oscillatory behaviours and flow (e.g. heat, fluid, electricity). To learn more about complex numbers, watch this Khan Academy video tutorial.

Figure 6

Variable scopes in Python with respect to scripts and functions.
Variable scopes in Python with respect to scripts and functions.

Figure 7


Figure 8

Routine mathematical operations in Python
Routine mathematical operations in Python

Figure 9


Figure 10

Routine logical operations in Python.
Routine logical operations in Python.

Figure 11

Negations in Python.
Negations in Python.

Figure 12

Disjunctions and Conjunctions in Python.
Disjunctions and Conjunctions in Python.

Figure 13

Complex Logical Operations in Python.
Complex Logical Operations in Python.

Conditional Statements


Figure 1

Flowchart
Flowchart

Figure 2

In previous chapter, Do it Yourself, we explored the implication of CAG repeats in Huntington’s disease. We also created a polynucleotide chain containing 36 repetition of the CAG codons.

Write a conditional statement that tests the length of a polyQ tract to determine the classification and the disease status based on the following Table:

Using the technique you used in Do it Yourself, create 5 polyQ tracts containing 26, 15, 39, 32, 36, and 54 codons. Use these polynucleotide chains to test your conditional statement.

Display the result for each chain in the following format:

PolyQ chain with XXX number of CAG codons:
Status: XXX
Classification: XXX

Hint: The length of a polyQ tract represents the number of nucleotides, not the number of CAG codons. See task 4 of Do it Yourself for additional information.


Introduction to Arrays


Figure 1


Figure 2

Indexing

In arrays, an index is an integer number that corresponds to a specific item.

You can think of an index as a unique reference or a key that corresponds to a specific row in a table. We don’t always write the row number when we create a table. However, we always know that the 3rd row of a table means that we start from the first row (row #1), count 3 rows down and there we find the 3rd row.

The only difference in Python is that we don’t take the first row as row #1; instead, we consider it to be row #0. As a consequence of starting from #0, we count rows in our table down to row #2 instead of #3 to find the 3rd row. So our table may in essence be visualised as follows:

Remember

Python uses a zero-based indexing system. This means that the first row of an array, regardless of its type, is always #0.

With that in mind, we can use the index for each value to retrieve it from a list.

Given a list of 4 members stored in a variable called table:

table = [5, 21, 5, -1]
we can visualise the referencing protocol in Python as follows:

As demonstrated in the diagram; to retrieve a member of an array through its index, we write the name of the variable immediately followed by the index value inside a pair of square brackets — e.g. table[2].

PYTHON

print(table[2])

OUTPUT

5

PYTHON

print(table[0])

OUTPUT

5

PYTHON

item = table[3]

print(item)

OUTPUT

-1

Do it Yourself

Retrieve and display the 5th Fibonacci number from the list you created in previous DIY.

PYTHON

print(fibonacci[4])

OUTPUT

5

It is sometimes more convenient to index an array backwards — that is, to reference the members from the bottom of the array. This is called negative indexing and is particularly useful when we are dealing with very lengthy arrays. The indexing system in Python support both positive and negative indexing systems.

The table above therefore may also be represented as follows:

Remember

Unlike the normal indexing system, which starts from #0, negative indexes start from #-1 so that it will always be clear which indexing system is being used.

If the index is a negative number, the indices are counted from the end of the list. We can implement negative indices the same way we do positive ones:

PYTHON

print(table[-1])

OUTPUT

-1

PYTHON

print(table[-2])

OUTPUT

5

PYTHON

print(table[-3])

OUTPUT

21

We know that in table, index #-3 refers the same value as index #1. So let us go ahead and test this:

PYTHON

equivalence = table[-3] == table[1]

print(equivalence)

OUTPUT

True

If the index requested is larger than the length of the list minus one, an IndexError will be raised:

PYTHON

print(table[4])

OUTPUT

IndexError: list index out of range

Remember

The values stored in a list may be referred to as the members of that list.

Do it Yourself

Retrieve and display the last Fibonacci number from the list you created in DIY.

PYTHON

print(fibonacci[-1])

OUTPUT

21

Figure 3


Figure 4


Figure 5


Figure 6


Figure 7

Commons operations for list, tuple, and set arrays in Python.
Commons operations for list, tuple, and set arrays in Python.

Figure 8


Figure 9


Figure 10

Give then following of pathogens and their corresponding diseases:

  1. Substituting N/A for None, create an array to represent the table in the original order. Retain the array in a variable and display the result.

  1. Modify the array you created so that the members are sorted descendingly and display the result.

Figure 11

A two dimensional arrays may be visualised as follows:


Figure 12

Computers see images as multidimensional arrays (matrices). In its simplest form, an image is a two-dimensional array containing only 2 colours.

Given the following black and white image:

  1. Considering that black and white squares represent zeros and ones respectively, create a two-dimensional array to represent the above image. Display the results.

  1. Create a new array, but this time use False and True to represent black and white respectively.

Display the results.


Figure 13


Figure 14

where each row in the matrix represents a node of origin in the graph, and each column a node of destination:


Figure 15

If the graph maintains a connection (edge) between 2 nodes (e.g. between nodes A and B in the graph above), the corresponding value between those nodes would be #1 in the matrix, and if there are no connections, the corresponding value would #0.


Figure 16

Given the following graph:


Iterations


Figure 1

Flowchart of a for–loop workflow applied to a list array.
Flowchart of a for–loop workflow applied to a list array.

Figure 2


Figure 3


Figure 4


Figure 5


Figure 6


Dictionaries


Figure 1

Illustrative diagram of associative arrays, showing the sets of keys and their association with some of the values. One way to associate the proteins with their definitions would be to use nested arrays. However, it would make it difficult to retrieve the values at a later time. This is because to retrieve the values, we would need to know the index at which a given protein is stored.


Figure 2


Functions


Figure 1


Figure 2