Programming and Data Science for Biology
(EEEB G4050)

Lecture 5: Python variables, lists, and for loops

Learning Python: Why Python?

  • Python is easy to read and easy to learn
  • Python has very active open source development community
  • Python has the most rapidly growing Data Science community
  • Python is a general purpose language (not domain specific)
  • Python is object-oriented with well developed style guides
  • But, Python is relatively slow (compared to C, C++, Fortran, etc)

Computer programs are just ways of telling a computer what to do

Everyone uses programming concepts in daily life. Think about the 'instructions' you need to follow to do simple, everyday things. If you need to do something over and over again you might be using a 'while loop', you just don't know it!


#here's some pseudocode
with open(book):
    while not done:
        read(page)
        try:
            turn(page)
        except:
            print("no more pages!")
            done == True
            

Computer programs are just ways of telling a computer what to do

If you need to decide between doing one thing or the other, like if it's sunny out you'll go to the park, and if it's rainy you'll stay in and read a book, you are using 'if statements' (or more formally 'conditional branching'). Here's another conditional branch that you might consider adopting for yourself ;)


# More python flavored pseudocode
if i want to do good in class:
    do my homework
else:
    don't do my homework
            

Computer programs are just ways of telling a computer what to do

Computer programs are composed of 'algorithms', which means they contain sets of instructions for taking inputs and doing operations on them to generate desired outputs. If that sounds quite a lot like following a recipe when baking that is because recipes are great examples of algorithms.

Variables and datatypes: The ingredients of a computer program

The most basic elements of a programming language are variables which are containers for references to values (just like in algebra class 2xy + 3x = 0; x and y are 'variables').

Variables contain information of different 'types' such as integers, strings of text, floating point numbers

Variables and datatypes: strings

String type objects are used to represent one or more characters. Strings are created by using either single or double quotes. You can perform operations on strings, such as combining them or reformatting them to accomplish very complex tasks.

To start, we need to create a string type object. We do this by storing the value of a string to a variable name. String objects have more than just their value attached to them. In addition, we can access other attributes and functions from the string object by using a dot after the object.


# create a string variable called x
x = "a string of text"

# call the .upper() function of the string object
print(x.upper())
        

A STRING OF TEXT
        

Variables and datatypes: The ingredients of a computer program

The type of a variable determines what you can do with it. Integer variables can be added and subtracted and multiplied. String variables can be indexed and concatenated.


x = 4
y = 6
print(x + y)

string_a = "wat"
string_b = "do"
print(string_a + string_b)
          

10
watdo
          

Lists: A fundamental python container class

Lists are the simplest data structure in python, they are used everywhere and they are very efficient. Lists are collections of items that are ordered, and lists can be modified (they are mutable).


lucky_numbers = [3, 7, 33, 42, 101, 314159]
print(len(lucky_numbers))

# You can use 'indexing' to select items from a list
print("I have this many lucky numbers:", lucky_numbers[0])

# And 'slicing' to select multiple elements in a range
print(lucky_numbers[1:3])
            

I have this many lucky numbers: 6
3
[7, 33]
            

Lists objects have many useful built-in functions

Lists are one of the fundamental data structures in python, so there are several useful ways to manipulate them. You can add and remove items, sort the list in different ways, and reverse the order of items.


# Add new items to a list
lucky_numbers.append(32)
print(lucky_numbers)

# We have a new lucky number but the list is out of order!
lucky_numbers.sort()
print(lucky_numbers)

# Reverse the order
lucky_numbers.reverse()
print(lucky_numbers)

# 7 isn't lucky anymore, so remove it from our list
lucky_numbers.remove(7)
print(lucky_numbers)
            

[3, 7, 33, 42, 101, 314159, 32]
[3, 7, 32, 33, 42, 101, 314159]
[314159, 101, 42, 33, 32, 7, 3]
[314159, 101, 42, 33, 32, 3]
            

For loops: Iteration over lists of items

One of the most common things to do with a list of items is 'iterate' through the list and perform some operation on each element. For instance, if we have a list of DNA sequences, we might want to know how long each sequence is:


sequences = ["ATCGCTTGCGGA", "ATCGCTGGCC", "ATCGCTGCC"]
for seq in sequences:
    print(seq, end="\t")
    print(len(seq))
          

ATCGCTTGCGGA    12
ATCGCTGGCC      10
ATCGCTGCC       9
          

List comprehension: Compact iteration over lists

Because 'for' looping over lists is such a common and useful function, python provides a compact syntax for iterating and manipulating list items called 'list comprehension'.


print([len(x) for x in sequences])
            

[12, 10, 9]