The number of spaces used for indentation is variable, but must remain the same within a block of code

Variables

In [5]:

x=1x

Out[5]:

1

In [6]:

y=2y

Out[6]:

2

In [7]:

x=yx

Out[7]:

2

Variables can be assigned any value

In [8]:

x,y=1,2printxprinty

1
2

multiple assignment is possible if the number of variables = number of values

In [9]:

y=y+1y

Out[9]:

3

In [10]:

x+=1x

Out[10]:

2

x += 1 is shorthand for x = x + 1

+= is the autoincrement operator

Python as a calculator

In [11]:

1+1

Out[11]:

2

Addition

In [12]:

4-1

Out[12]:

3

Subtraction

In [13]:

5*2

Out[13]:

10

Multiplication

In [14]:

1/5

Out[14]:

0

Divis... WHAT?1 / 5 = 0.2, right?

Object types

Python (and any other programming language) will only follow your instructions literally. When you type 1 / 5 in to the interpreter, Python guesses that both 1 and 5 are numeric integer numbers and that you want the result of the division to also be an integer.

In [15]:

int(1)/int(5)

Out[15]:

0

We actually expect that the answer will be a floating point number and not an integer, so we have to give Python better instructions.

Exercise 2: Palindromic sequences

Q: How would you test whether the sequence 'ATGCATGATTAGTACGTA' is palindromic (reads the same forward and backward)?

In [48]:

seq='ATGCATGATTAGTACGTA'seq==seq[::-1]

Out[48]:

True

Tuples

In [49]:

names=('Fred','Ted','Ned')names

Out[49]:

('Fred', 'Ted', 'Ned')

Tuples are constructed using () and ,

Commas separate each element, and the entire tuple is enclosed in parentheses.

You can access the first name in names by specifying the 0-based index position of that element.

In [50]:

names[0]

Out[50]:

'Fred'

To access the last name in names, you can use either the 0-based index of that element [2], or use a negative index [-1].

In [51]:

printnames

('Fred', 'Ted', 'Ned')

In [52]:

names[2]

Out[52]:

'Ned'

In [53]:

names[-1]

Out[53]:

'Ned'

Tuples can be "sliced" using [:].

In [54]:

names[0:3]

Out[54]:

('Fred', 'Ted', 'Ned')

In [55]:

names[1:3]

Out[55]:

('Ted', 'Ned')

In [56]:

names[:2]

Out[56]:

('Fred', 'Ted')

In [57]:

names[0]='Zed'## This will result in a TypeError

---------------------------------------------------------------------------TypeError Traceback (most recent call last)
<ipython-input-57-c7309bd65052> in <module>()----> 1names[0]='Zed'## This will result in a TypeErrorTypeError: 'tuple' object does not support item assignment

Tuples are immutable.

Sometimes you want modify the contents of a set

Sounds like a job for lists!

Lists

In [58]:

names=['Fred','Ted','Ned']names

Out[58]:

['Fred', 'Ted', 'Ned']

Notice that the list construction uses [] and not ()

List are mutable, meaning that we can change the contents after creation

In [59]:

names[2]='Zed'names

Out[59]:

['Fred', 'Ted', 'Zed']

In [60]:

names.append('Mike')names

Out[60]:

['Fred', 'Ted', 'Zed', 'Mike']

In [61]:

names+=['Obama']names

Out[61]:

['Fred', 'Ted', 'Zed', 'Mike', 'Obama']

In [62]:

names.extend(['Craig'])names

Out[62]:

['Fred', 'Ted', 'Zed', 'Mike', 'Obama', 'Craig']

There are several ways to add objects to a list.

In [63]:

names.pop()

Out[63]:

'Craig'

In [64]:

names.insert(3,'Ned')names

Out[64]:

['Fred', 'Ted', 'Zed', 'Ned', 'Mike', 'Obama']

In [65]:

names.index('Obama')

Out[65]:

5

We can remove and insert elements, as well as find the numeric index by element name.

In [66]:

sorted(names)

Out[66]:

['Fred', 'Mike', 'Ned', 'Obama', 'Ted', 'Zed']

In [67]:

names.sort()names

Out[67]:

['Fred', 'Mike', 'Ned', 'Obama', 'Ted', 'Zed']

In [68]:

names.reverse()names

Out[68]:

['Zed', 'Ted', 'Obama', 'Ned', 'Mike', 'Fred']

Here are a few methods to manipulate the order of lists.

Note that names.sort() and names.reverse() actually modified the list instead of returning a new list

In [69]:

gtca=list("GATACA")gtca

Out[69]:

['G', 'A', 'T', 'A', 'C', 'A']

In [70]:

"".join(gtca)

Out[70]:

'GATACA'

In [71]:

"-".join(gtca)

Out[71]:

'G-A-T-A-C-A'

Lists can be constructed from strings

Lists can be joined to form a string

List constructions

In [72]:

'G-A-T-A-C-A'.split('-')

Out[72]:

['G', 'A', 'T', 'A', 'C', 'A']

In [73]:

range(0,10)

Out[73]:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [74]:

range(0,10,2)

Out[74]:

[0, 2, 4, 6, 8]

The split method for strings creates a list from elements separated by a character