An introduction to solving biological problems with Python

Day 1 - Session 2: Simple data types

Simple data types

Python (and computers in general) treats different types of data differently. Python has 4 main basic data types. Types are useful to constrain some operations to a certain category of variables. For example it doesn't really make sense to try to divide a string.

We will see some examples of these in use shortly, but for now let's see all of the basic types available in python.

Integers

Integers represent whole numbers, as you would use when counting items, and can be positive or negative.


In [ ]:
i = -7
j = 123
print(i, j)

Floats

Floating point numbers, often simply referred to as floats, are numbers expressed in the decimal system, i.e. 2.1, 999.998, -0.000004 etc. The value 2.0 would also be interpreted as a floating point number, but the value 2, without the decimal point will not; it will be interpreted as an integer.


In [ ]:
x = 3.14159
y = -42.3
print(x * y)

Floating point numbers can also carry an e suffix that states which power of ten they operate at.


In [ ]:
k = 1.5e3
l = 3e-2
print(k)
print(l)

Strings

Strings represent text, i.e. "strings" of characters. They can be delimited by single quotes or double quotes , but you have to use the same delimiter at both ends. Unlike some programming languages, such as Perl, there is no difference between the two types of quote, although using one type does allow the other type to appear inside the string as a regular character.

Normally a python statement ends at the end of the line, but if you want to type a string over several lines you can enclose it in triple quotation marks.


In [ ]:
s = "ATGTCGTCTACAACACT"
t = 'Serine'
u = "It's a string with apostrophes"
v = """A string that extends
over multiple lines"""
print(v)

Booleans

Boolean values represent truth or falsehood, as used in logical operations, for example. Not surprisingly, there are only two values, and in Python they are called True and False.


In [ ]:
a = True
b = False
print(a, b)

The None object

The None object is special built-in value which can be thought of as representing nothingness or that something is undefined. For example, it can be used to indicate that a variable exists, but has not yet been set to anything specific.


In [ ]:
z = None
print(z)

Object type

You can check what type python thinks an expression is with the type function, which you can call with the name type immediately followed by parentheses enclosing the expression you want to check (either a variable or a value), e.g. type(3). (This is the general form for calling functions, we'll see lots more examples of functions later...)


In [ ]:
a = True
print(a, "is of", type(a))

In [ ]:
i = -7
print(i, "is of", type(i))

In [ ]:
x = 12.7893
print(x, "is of", type(x))

In [ ]:
s = "ATGTCGTCTACAACACT"
print(s, "is of", type(s))

In [ ]:
z = None
print(z, "is of", type(z))

Comments

When you are writing a program it is often convenient to annotate your code to remind you what you were (intending) it to do. In programming these annotations are known as comments. You can include a comment in python by prefixing some text with a # character. All text following the # will then be ignored by the interpreter. You can start a comment on its own line, or you can include it at the end of a line of code.

It is also often useful to temporarily remove some code from a script without deleting it. This is known as commenting out some code.


In [ ]:
print("Hi") # this will be ignored
# as will this
print("Bye")
# print "Never seen"

Arithmetic

Python supports all the standard arithmetical operations on numerical types, and mostly uses a similar syntax to several other computer languages:


In [ ]:
x = 4.5
y = 2

print('x', x, 'y', y)
print('addition x + y =', x + y) 
print('subtraction x - y =', x - y) 
print('multiplication x * y =', x * y) 
print('division x / y =', x / y)

In [ ]:
x = 4.5
y = 2

print('x', x, 'y', y)
print('division x / y =', x / y)
print('floored division x // y =', x // y) 
print('modulus (remainder of x/y) x % y =', x % y) 
print('exponentiation x ** y =', x ** y)

As usual in maths, division and multiplication have higher precedence than addition and subtraction, but arithmetic expressions can be grouped using parentheses to override the default precedence


In [ ]:
x = 13
y = 5

print('x * (2 + y) =', x * (2 + y))
print('(x * 2) + y =', (x * 2) + y)
print('x * 2 + y =', x * 2 + y)

You can mix (some) types in arithmetic expressions and python will apply rules as to the type of the result


In [ ]:
13 + 5.0

You can force python to use a particular type by converting an expression explicitly, using helpful named functions: float, int, str etc.


In [ ]:
float(3) + float(7)

In [ ]:
int(3.14159) + 1

The addition operator + allows you also to concatenate strings together.


In [ ]:
print('number' + str(3))

Division in Python 2 sometimes trips up new (and experienced!) programmers. If you divide 2 integers you will only get an integer result. If you want a floating point result you should explicitly cast at least one of the arguments to a float.


In [ ]:
print("3/4 =", 3/4) # in Python 2, you would get 0
print("3.0/4 =", 3.0/4)
print("float(3)/4 =", float(3)/4)

There are a few shortcut assignment statements to make modifying variables directly faster to type


In [ ]:
x = 3
x += 1 # equivalent to x = x + 1
x

In [ ]:
x = 2
y = 10
y *= x
y

These shortcut operators are available for all arithmetic and logical operators.

Exercises 1.2.1

In the interpreter:

Assign numerical values to 2 variables, calculate the mean of these two variables and store the result in another variable. Print out the result to the screen.

Saving code in files

Excecute code in files

As we mentioned earlier, you can also save python code in a file and then execute it later. We typically save python code in a file ending with the extension .py. The file, or script, can then be executed simply by supplying the name of the file as an argument to the python command in the terminal.

The first thing you may want to do, before going any further, is to verify the version of python you are running. To do so, open a terminal window and execute this command:

python --version

You should be running Python 3.5.x.

The first file we will be looking at is located in the scripts directory and it is called hello.py. To execute the script, open a terminal window, navigate to the scripts directory and execute the code in the script hello.py by running python hello.py in your terminal:

ls
cd scripts
python hello.py

Shell commands:

  • ls: to list directory contents
  • pwd: to return working directory name
  • cd to/this/directory/: to change directory

Edit code in files

You can use any text editor you know to edit your file, but the file should be saved as plain text, so programs like Microsoft Word aren't the best choice. Many text editors will highlight python syntax for you which can help avoid syntax errors.

To open any Python scripts in a text editor, open Gedit or Atom and use the File menu, navigate to the scripts directory and open hello.py.

You can now modify the print statement, save the file and go back to the terminal window you've just opened to run the code again by using the command python hello.py.

Exercises 1.2.2

Create a new Python file to solve these exercises. It is good practice to create a new file each time you solve a new problem.

  1. Look up the genetic code. Create four string variables that store possible DNA encodings of serine (S), leucine (L), tyrosine (Y) and cysteine (C). Where multiple codings are available, just pick one for now.
  2. Create a variable containing a possible DNA sequence for the protein sequence SYLYC. (Note that the addition operator + allows you to concatenate strings together.) Print the DNA sequence.
  3. Include a comment in your file to remind you the purpose of the script.

Next session

Go to our next notebook: python_basic_1_3